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 <string>
17#include <vector>
18
19#include "absl/container/flat_hash_set.h"
20#include "ortools/math_opt/sparse_containers.pb.h"
24
26
28 const ProtoType& in_proto) {
30 data.lower_bound = in_proto.lower_bound();
31 data.upper_bound = in_proto.upper_bound();
32 data.name = in_proto.name();
33 for (int i = 0; i < in_proto.linear_terms().ids_size(); ++i) {
34 data.linear_terms.set(VariableId(in_proto.linear_terms().ids(i)),
35 in_proto.linear_terms().values(i));
36 }
37 for (int i = 0; i < in_proto.quadratic_terms().row_ids_size(); ++i) {
39 VariableId(in_proto.quadratic_terms().row_ids(i)),
40 VariableId(in_proto.quadratic_terms().column_ids(i)),
41 in_proto.quadratic_terms().coefficients(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.mutable_linear_terms() = linear_terms.Proto();
52 *constraint.mutable_quadratic_terms() = quadratic_terms.Proto();
53 constraint.set_name(name);
54 return constraint;
55}
56
57std::vector<VariableId> QuadraticConstraintData::RelatedVariables() const {
58 std::vector<VariableId> quad_terms = quadratic_terms.Variables();
59 absl::flat_hash_set<VariableId> vars(quad_terms.begin(), quad_terms.end());
60 for (const auto& [var, coef] : linear_terms.terms()) {
61 vars.insert(var);
62 }
63 return std::vector<VariableId>(vars.begin(), vars.end());
64}
65
70
71} // 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
void Delete(VariableId variable)
Zeros out all coefficients for this variable.
bool set(VariableId first, VariableId second, double value)
IntVar * var
int64_t coef
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
static QuadraticConstraintData FromProto(const ProtoType &in_proto)
Definition storage.cc:27
std::vector< VariableId > RelatedVariables() const
Definition storage.cc:57