OR-Tools  9.6
variable_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 <string>
18 #include <vector>
19 
20 #include "absl/algorithm/container.h"
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/container/flat_hash_set.h"
23 #include "absl/strings/string_view.h"
25 #include "ortools/math_opt/model.pb.h"
26 #include "ortools/math_opt/model_update.pb.h"
27 #include "ortools/math_opt/sparse_containers.pb.h"
30 
32 
33 VariableId VariableStorage::Add(const double lower_bound,
34  const double upper_bound, const bool is_integer,
35  const absl::string_view name) {
36  const VariableId id = next_variable_id_;
37  VariableData& var_data = variables_[id];
38  var_data.lower_bound = lower_bound;
39  var_data.upper_bound = upper_bound;
40  var_data.is_integer = is_integer;
41  var_data.name = std::string(name);
42  ++next_variable_id_;
43  return id;
44 }
45 
46 std::vector<VariableId> VariableStorage::Variables() const {
47  std::vector<VariableId> result;
48  result.reserve(variables_.size());
49  for (const auto& [var, _] : variables_) {
50  result.push_back(var);
51  }
52  return result;
53 }
54 
55 std::vector<VariableId> VariableStorage::SortedVariables() const {
56  std::vector<VariableId> result = Variables();
57  absl::c_sort(result);
58  return result;
59 }
60 
61 std::vector<VariableId> VariableStorage::VariablesFrom(
62  const VariableId start) const {
63  std::vector<VariableId> result;
64  for (const VariableId v :
65  util_intops::MakeStrongIntRange(start, next_variable_id_)) {
66  if (variables_.contains(v)) {
67  result.push_back(v);
68  }
69  }
70  return result;
71 }
72 
73 void VariableStorage::AppendVariable(const VariableId variable,
74  VariablesProto* proto) const {
75  const VariableData& data = variables_.at(variable);
76  proto->add_ids(variable.value());
77  proto->add_lower_bounds(data.lower_bound);
78  proto->add_upper_bounds(data.upper_bound);
79  proto->add_integers(data.is_integer);
80  proto->add_names(data.name);
81 }
82 
83 VariablesProto VariableStorage::Proto() const {
84  VariablesProto result;
85  for (const VariableId v : SortedVariables()) {
86  AppendVariable(v, &result);
87  }
88  return result;
89 }
90 
92  diff.checkpoint = next_variable_id_;
93  diff.deleted.clear();
94  diff.lower_bounds.clear();
95  diff.upper_bounds.clear();
96  diff.integer.clear();
97 }
98 
100  UpdateResult result;
101  for (const VariableId v : diff.deleted) {
102  result.deleted.Add(v.value());
103  }
104  absl::c_sort(result.deleted);
105  for (const VariableId v : SortedElements(diff.lower_bounds)) {
106  result.updates.mutable_lower_bounds()->add_ids(v.value());
107  result.updates.mutable_lower_bounds()->add_values(lower_bound(v));
108  }
109  for (const VariableId v : SortedElements(diff.upper_bounds)) {
110  result.updates.mutable_upper_bounds()->add_ids(v.value());
111  result.updates.mutable_upper_bounds()->add_values(upper_bound(v));
112  }
113  for (const VariableId v : SortedElements(diff.integer)) {
114  result.updates.mutable_integers()->add_ids(v.value());
115  result.updates.mutable_integers()->add_values(is_integer(v));
116  }
117  for (const VariableId v :
118  util_intops::MakeStrongIntRange(diff.checkpoint, next_variable_id_)) {
119  if (variables_.contains(v)) {
120  AppendVariable(v, &result.creates);
121  }
122  }
123  return result;
124 }
125 
126 } // namespace operations_research::math_opt
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
std::vector< VariableId > Variables() const
UpdateResult Update(const Diff &diff) const
CpModelProto proto
const std::string name
IntVar * var
Definition: expr_array.cc:1874
std::vector< T > SortedElements(const absl::flat_hash_set< T > &elements)
Definition: sorted.h:28
StrongIntRange< IntType > MakeStrongIntRange(IntType end)
Definition: strong_int.h:410
IntVar * upper_bound
Definition: routing.cc:1087
IntVar * lower_bound
Definition: routing.cc:1086
int64_t start
absl::flat_hash_set< VariableId > lower_bounds
absl::flat_hash_set< VariableId > upper_bounds
google::protobuf::RepeatedField< int64_t > deleted