Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
storage.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <optional>
17#include <string>
18#include <vector>
19
20#include "absl/container/flat_hash_set.h"
23#include "ortools/math_opt/model.pb.h"
24#include "ortools/math_opt/sparse_containers.pb.h"
26
28
30 const ProtoType& in_proto) {
32 data.lower_bound = in_proto.lower_bound();
33 data.upper_bound = in_proto.upper_bound();
34 data.name = in_proto.name();
35 data.activate_on_zero = in_proto.activate_on_zero();
36 if (in_proto.has_indicator_id()) {
37 data.indicator = VariableId(in_proto.indicator_id());
38 }
39 for (int i = 0; i < in_proto.expression().ids_size(); ++i) {
40 data.linear_terms.set(VariableId(in_proto.expression().ids(i)),
41 in_proto.expression().values(i));
42 }
43 return data;
44}
45
47 const {
48 ProtoType constraint;
49 constraint.set_lower_bound(lower_bound);
50 constraint.set_upper_bound(upper_bound);
51 constraint.set_name(name);
52 constraint.set_activate_on_zero(activate_on_zero);
53 if (indicator.has_value()) {
54 constraint.set_indicator_id(indicator->value());
55 }
56 for (const VariableId var : SortedMapKeys(linear_terms.terms())) {
57 constraint.mutable_expression()->add_ids(var.value());
58 constraint.mutable_expression()->add_values(linear_terms.get(var));
59 }
60 return constraint;
61}
62
63std::vector<VariableId> IndicatorConstraintData::RelatedVariables() const {
64 absl::flat_hash_set<VariableId> vars;
65 if (indicator.has_value()) {
66 vars.insert(*indicator);
67 }
68 for (const auto [var, coef] : linear_terms.terms()) {
69 vars.insert(var);
70 }
71 return std::vector<VariableId>(vars.begin(), vars.end());
72}
73
76 if (indicator.has_value() && *indicator == var) {
77 indicator.reset();
78 }
79}
80
81} // namespace operations_research::math_opt
bool set(VariableId id, double coeff)
Returns true if the stored value changes.
const absl::flat_hash_map< VariableId, double > & terms() const
double get(VariableId id) const
Returns 0.0 by default if no value is set.
void erase(VariableId id)
Has no effect if id is not set.
IntVar * var
int64_t coef
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::vector< K > SortedMapKeys(const absl::flat_hash_map< K, V > &in_map)
Definition sorted.h:55
static IndicatorConstraintData FromProto(const ProtoType &in_proto)
Definition storage.cc:29
std::vector< VariableId > RelatedVariables() const
Definition storage.cc:63