OR-Tools  9.6
indicator/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 <optional>
18 #include <string>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_set.h"
23 #include "ortools/math_opt/model.pb.h"
24 #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  data.activate_on_zero = in_proto.activate_on_zero();
37  if (in_proto.has_indicator_id()) {
38  data.indicator = VariableId(in_proto.indicator_id());
39  }
40  for (int i = 0; i < in_proto.expression().ids_size(); ++i) {
41  data.linear_terms.set(VariableId(in_proto.expression().ids(i)),
42  in_proto.expression().values(i));
43  }
44  return data;
45 }
46 
48  const {
49  ProtoType constraint;
50  constraint.set_lower_bound(lower_bound);
51  constraint.set_upper_bound(upper_bound);
52  constraint.set_name(name);
53  constraint.set_activate_on_zero(activate_on_zero);
54  if (indicator.has_value()) {
55  constraint.set_indicator_id(indicator->value());
56  }
57  for (const VariableId var : SortedMapKeys(linear_terms.terms())) {
58  constraint.mutable_expression()->add_ids(var.value());
59  constraint.mutable_expression()->add_values(linear_terms.get(var));
60  }
61  return constraint;
62 }
63 
64 std::vector<VariableId> IndicatorConstraintData::RelatedVariables() const {
65  absl::flat_hash_set<VariableId> vars;
66  if (indicator.has_value()) {
67  vars.insert(*indicator);
68  }
69  for (const auto [var, coef] : linear_terms.terms()) {
70  vars.insert(var);
71  }
72  return std::vector<VariableId>(vars.begin(), vars.end());
73 }
74 
77  if (indicator.has_value() && *indicator == var) {
78  indicator.reset();
79  }
80 }
81 
82 } // namespace operations_research::math_opt
const absl::flat_hash_map< VariableId, double > & terms() const
bool set(const VariableId id, const double coeff)
IntVar * var
Definition: expr_array.cc:1874
int64_t coef
Definition: expr_array.cc:1875
std::vector< K > SortedMapKeys(const absl::flat_hash_map< K, V > &in_map)
Definition: sorted.h:55
static IndicatorConstraintData FromProto(const ProtoType &in_proto)