Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
variable_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
15
16#include <string>
17#include <vector>
18
19#include "absl/algorithm/container.h"
20#include "absl/container/flat_hash_map.h"
21#include "absl/container/flat_hash_set.h"
22#include "absl/strings/string_view.h"
29
31
33 const double upper_bound, const bool is_integer,
34 const absl::string_view name) {
35 const VariableId id = next_variable_id_;
36 VariableData& var_data = variables_[id];
37 var_data.lower_bound = lower_bound;
38 var_data.upper_bound = upper_bound;
39 var_data.is_integer = is_integer;
40 var_data.name = std::string(name);
41 ++next_variable_id_;
42 return id;
43}
44
45std::vector<VariableId> VariableStorage::Variables() const {
46 std::vector<VariableId> result;
47 result.reserve(variables_.size());
48 for (const auto& [var, _] : variables_) {
49 result.push_back(var);
50 }
51 return result;
52}
53
54std::vector<VariableId> VariableStorage::SortedVariables() const {
55 std::vector<VariableId> result = Variables();
56 absl::c_sort(result);
57 return result;
58}
59
60std::vector<VariableId> VariableStorage::VariablesFrom(
61 const VariableId start) const {
62 std::vector<VariableId> result;
63 for (const VariableId v :
64 util_intops::MakeStrongIntRange(start, next_variable_id_)) {
65 if (variables_.contains(v)) {
66 result.push_back(v);
67 }
68 }
69 return result;
70}
71
72void VariableStorage::AppendVariable(const VariableId variable,
73 VariablesProto* proto) const {
74 const VariableData& data = variables_.at(variable);
75 proto->add_ids(variable.value());
76 proto->add_lower_bounds(data.lower_bound);
77 proto->add_upper_bounds(data.upper_bound);
78 proto->add_integers(data.is_integer);
79 proto->add_names(data.name);
80}
81
83 VariablesProto result;
84 for (const VariableId v : SortedVariables()) {
85 AppendVariable(v, &result);
86 }
87 return result;
88}
89
91 diff.checkpoint = next_variable_id_;
92 diff.deleted.clear();
93 diff.lower_bounds.clear();
94 diff.upper_bounds.clear();
95 diff.integer.clear();
96}
97
99 UpdateResult result;
100 for (const VariableId v : diff.deleted) {
101 result.deleted.Add(v.value());
102 }
103 absl::c_sort(result.deleted);
104 for (const VariableId v : SortedSetElements(diff.lower_bounds)) {
107 }
108 for (const VariableId v : SortedSetElements(diff.upper_bounds)) {
111 }
112 for (const VariableId v : SortedSetElements(diff.integer)) {
113 result.updates.mutable_integers()->add_ids(v.value());
115 }
116 for (const VariableId v :
117 util_intops::MakeStrongIntRange(diff.checkpoint, next_variable_id_)) {
118 if (variables_.contains(v)) {
119 AppendVariable(v, &result.creates);
120 }
121 }
122 return result;
123}
124
125} // namespace operations_research::math_opt
int64_t value() const
Returns the raw id value.
Definition elements.h:77
VariablesProto Proto() const
An equivalent proto of this.
std::vector< VariableId > Variables() const
The VariableIds in use (not deleted), order not defined.
void AdvanceCheckpointInDiff(Diff &diff) const
Updates the checkpoint and clears all stored changes in diff.
std::vector< VariableId > SortedVariables() const
std::vector< VariableId > VariablesFrom(VariableId start) const
VariableId Add(double lower_bound, double upper_bound, bool is_integer, absl::string_view name)
const std::string & name(VariableId id) const
UpdateResult Update(const Diff &diff) const
::operations_research::math_opt::SparseDoubleVectorProto *PROTOBUF_NONNULL mutable_lower_bounds()
::operations_research::math_opt::SparseDoubleVectorProto *PROTOBUF_NONNULL mutable_upper_bounds()
::operations_research::math_opt::SparseBoolVectorProto *PROTOBUF_NONNULL mutable_integers()
::std::string *PROTOBUF_NONNULL add_names()
Definition model.pb.h:2774
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
ElementId< ElementType::kVariable > VariableId
Definition elements.h:264
std::vector< T > SortedSetElements(const absl::flat_hash_set< T > &elements)
Definition sorted.h:28
google::protobuf::RepeatedField< int64_t > deleted
Variables before the checkpoint that have been deleted.
VariablesProto creates
Variables created at or after the checkpoint that have not been deleted.
VariableUpdatesProto updates
Variables before the checkpoint that have been modified and not deleted.