OR-Tools  9.6
quadratic/storage.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <cstdint>
17 #include <string>
18 #include <vector>
19 
20 #include "absl/container/flat_hash_set.h"
22 #include "ortools/math_opt/sparse_containers.pb.h"
27 
29 
31  const ProtoType& in_proto) {
33  data.lower_bound = in_proto.lower_bound();
34  data.upper_bound = in_proto.upper_bound();
35  data.name = in_proto.name();
36  for (int i = 0; i < in_proto.linear_terms().ids_size(); ++i) {
37  data.linear_terms.set(VariableId(in_proto.linear_terms().ids(i)),
38  in_proto.linear_terms().values(i));
39  }
40  for (int i = 0; i < in_proto.quadratic_terms().row_ids_size(); ++i) {
41  data.quadratic_terms.set(
42  VariableId(in_proto.quadratic_terms().row_ids(i)),
43  VariableId(in_proto.quadratic_terms().column_ids(i)),
44  in_proto.quadratic_terms().coefficients(i));
45  }
46  return data;
47 }
48 
50  const {
51  ProtoType constraint;
52  constraint.set_lower_bound(lower_bound);
53  constraint.set_upper_bound(upper_bound);
54  *constraint.mutable_linear_terms() = linear_terms.Proto();
55  *constraint.mutable_quadratic_terms() = quadratic_terms.Proto();
56  constraint.set_name(name);
57  return constraint;
58 }
59 
60 std::vector<VariableId> QuadraticConstraintData::RelatedVariables() const {
61  std::vector<VariableId> quad_terms = quadratic_terms.Variables();
62  absl::flat_hash_set<VariableId> vars(quad_terms.begin(), quad_terms.end());
63  for (const auto& [var, coef] : linear_terms.terms()) {
64  vars.insert(var);
65  }
66  return std::vector<VariableId>(vars.begin(), vars.end());
67 }
68 
70  linear_terms.set(var, 0.0);
72 }
73 
74 } // namespace operations_research::math_opt
const absl::flat_hash_map< VariableId, double > & terms() const
bool set(const VariableId id, const double coeff)
std::vector< VariableId > Variables() const
bool set(VariableId first, VariableId second, double value)
IntVar * var
Definition: expr_array.cc:1874
int64_t coef
Definition: expr_array.cc:1875
static QuadraticConstraintData FromProto(const ProtoType &in_proto)