Google OR-Tools
v9.12
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-2025 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
14
#include "
ortools/math_opt/constraints/indicator/storage.h
"
15
16
#include <optional>
17
#include <string>
18
#include <vector>
19
20
#include "absl/container/flat_hash_set.h"
21
#include "
ortools/base/strong_int.h
"
22
#include "
ortools/math_opt/core/sorted.h
"
23
#include "ortools/math_opt/model.pb.h"
24
#include "ortools/math_opt/sparse_containers.pb.h"
25
#include "
ortools/math_opt/storage/sparse_coefficient_map.h
"
26
27
namespace
operations_research::math_opt
{
28
29
IndicatorConstraintData
IndicatorConstraintData::FromProto
(
30
const
ProtoType
& in_proto) {
31
IndicatorConstraintData
data;
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
46
typename
IndicatorConstraintData::ProtoType
IndicatorConstraintData::Proto
()
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
63
std::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
74
void
IndicatorConstraintData::DeleteVariable
(
const
VariableId var) {
75
linear_terms
.erase(var);
76
if
(
indicator
.has_value() && *
indicator
== var) {
77
indicator
.reset();
78
}
79
}
80
81
}
// namespace operations_research::math_opt
operations_research::math_opt::SparseCoefficientMap::set
bool set(VariableId id, double coeff)
Returns true if the stored value changes.
Definition
sparse_coefficient_map.h:81
storage.h
operations_research::math_opt
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition
gurobi_isv.cc:28
operations_research::math_opt::SortedMapKeys
std::vector< K > SortedMapKeys(const absl::flat_hash_map< K, V > &in_map)
Definition
sorted.h:55
sorted.h
sparse_coefficient_map.h
strong_int.h
operations_research::math_opt::IndicatorConstraintData
Definition
storage.h:33
operations_research::math_opt::IndicatorConstraintData::activate_on_zero
bool activate_on_zero
Definition
storage.h:51
operations_research::math_opt::IndicatorConstraintData::Proto
ProtoType Proto() const
Definition
storage.cc:46
operations_research::math_opt::IndicatorConstraintData::upper_bound
double upper_bound
Definition
storage.h:46
operations_research::math_opt::IndicatorConstraintData::lower_bound
double lower_bound
Definition
storage.h:45
operations_research::math_opt::IndicatorConstraintData::linear_terms
SparseCoefficientMap linear_terms
Definition
storage.h:47
operations_research::math_opt::IndicatorConstraintData::ProtoType
IndicatorConstraintProto ProtoType
Definition
storage.h:35
operations_research::math_opt::IndicatorConstraintData::indicator
std::optional< VariableId > indicator
Definition
storage.h:50
operations_research::math_opt::IndicatorConstraintData::FromProto
static IndicatorConstraintData FromProto(const ProtoType &in_proto)
Definition
storage.cc:29
operations_research::math_opt::IndicatorConstraintData::RelatedVariables
std::vector< VariableId > RelatedVariables() const
Definition
storage.cc:63
operations_research::math_opt::IndicatorConstraintData::DeleteVariable
void DeleteVariable(VariableId var)
Definition
storage.cc:74
operations_research::math_opt::IndicatorConstraintData::name
std::string name
Definition
storage.h:52
ortools
math_opt
constraints
indicator
storage.cc
Generated by
1.13.2