14#ifndef OR_TOOLS_MATH_OPT_STORAGE_VARIABLE_STORAGE_H_
15#define OR_TOOLS_MATH_OPT_STORAGE_VARIABLE_STORAGE_H_
23#include "absl/container/flat_hash_map.h"
24#include "absl/container/flat_hash_set.h"
25#include "absl/strings/string_view.h"
27#include "ortools/math_opt/model.pb.h"
28#include "ortools/math_opt/model_update.pb.h"
60 google::protobuf::RepeatedField<int64_t>
deleted;
73 absl::string_view
name);
78 inline const std::string&
name(VariableId
id)
const;
80 template <
typename DiffIter>
84 template <
typename DiffIter>
88 template <
typename DiffIter>
96 template <
typename DiffIter>
103 inline int64_t
size()
const;
108 inline VariableId
next_id()
const;
114 inline bool contains(VariableId
id)
const;
117 std::vector<VariableId>
Variables()
const;
126 VariablesProto
Proto()
const;
146 struct VariableData {
147 double lower_bound = -std::numeric_limits<double>::infinity();
148 double upper_bound = std::numeric_limits<double>::infinity();
149 bool is_integer =
false;
150 std::string name =
"";
153 void AppendVariable(VariableId variable, VariablesProto*
proto)
const;
155 VariableId next_variable_id_ = VariableId(0);
156 absl::flat_hash_map<VariableId, VariableData> variables_;
164 return variables_.at(
id).lower_bound;
168 return variables_.at(
id).upper_bound;
172 return variables_.at(
id).is_integer;
176 return variables_.at(
id).name;
179template <
typename DiffIter>
182 VariableData& data = variables_.at(
id);
187 for (Diff& diff : diffs) {
188 if (
id < diff.checkpoint) {
189 diff.lower_bounds.insert(
id);
194template <
typename DiffIter>
196 const iterator_range<DiffIter>& diffs) {
197 VariableData& data = variables_.at(
id);
202 for (Diff& diff : diffs) {
203 if (
id < diff.checkpoint) {
204 diff.upper_bounds.insert(
id);
209template <
typename DiffIter>
211 const iterator_range<DiffIter>& diffs) {
212 VariableData& data = variables_.at(
id);
217 for (Diff& diff : diffs) {
218 if (
id < diff.checkpoint) {
219 diff.integer.insert(
id);
224template <
typename DiffIter>
226 const iterator_range<DiffIter>& diffs) {
227 for (Diff& diff : diffs) {
228 if (
id >= diff.checkpoint) {
231 diff.deleted.insert(
id);
232 diff.lower_bounds.erase(
id);
233 diff.upper_bounds.erase(
id);
234 diff.integer.erase(
id);
236 variables_.erase(
id);
244 next_variable_id_ = std::max(minimum, next_variable_id_);
248 return variables_.contains(
id);
bool contains(VariableId id) const
Returns true if this id has been created and not yet deleted.
VariablesProto Proto() const
An equivalent proto of this.
double upper_bound(VariableId id) const
double lower_bound(VariableId id) const
std::vector< VariableId > Variables() const
The VariableIds in use (not deleted), order not defined.
bool is_integer(VariableId id) const
VariableId next_id() const
void AdvanceCheckpointInDiff(Diff &diff) const
Updates the checkpoint and clears all stored changes in diff.
void set_integer(VariableId id, bool is_integer, const iterator_range< DiffIter > &diffs)
void ensure_next_id_at_least(VariableId minimum)
Sets the next variable id to be the maximum of next_id() and minimum.
std::vector< VariableId > SortedVariables() const
void set_lower_bound(VariableId id, double lower_bound, const iterator_range< DiffIter > &diffs)
std::vector< VariableId > VariablesFrom(VariableId start) const
bool diff_is_empty(const Diff &diff) const
void set_upper_bound(VariableId id, double upper_bound, const iterator_range< DiffIter > &diffs)
VariableId Add(double lower_bound, double upper_bound, bool is_integer, absl::string_view name)
void Delete(VariableId id, const iterator_range< DiffIter > &diffs)
const std::string & name(VariableId id) const
UpdateResult Update(const Diff &diff) const
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
An object oriented wrapper for quadratic constraints in ModelStorage.
absl::flat_hash_set< VariableId > integer
absl::flat_hash_set< VariableId > lower_bounds
absl::flat_hash_set< VariableId > upper_bounds
absl::flat_hash_set< VariableId > deleted
Diff(const VariableStorage &storage)
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.