14#ifndef OR_TOOLS_MATH_OPT_STORAGE_LINEAR_CONSTRAINT_STORAGE_H_
15#define OR_TOOLS_MATH_OPT_STORAGE_LINEAR_CONSTRAINT_STORAGE_H_
24#include "absl/container/flat_hash_map.h"
25#include "absl/container/flat_hash_set.h"
26#include "absl/log/check.h"
27#include "absl/meta/type_traits.h"
28#include "absl/strings/string_view.h"
29#include "absl/types/span.h"
31#include "ortools/math_opt/model.pb.h"
32#include "ortools/math_opt/model_update.pb.h"
33#include "ortools/math_opt/sparse_containers.pb.h"
58 absl::flat_hash_set<LinearConstraintId>
deleted;
65 absl::flat_hash_set<std::pair<LinearConstraintId, VariableId>>
matrix_keys;
69 google::protobuf::RepeatedField<int64_t>
deleted;
81 absl::string_view
name);
83 inline double lower_bound(LinearConstraintId
id)
const;
84 inline double upper_bound(LinearConstraintId
id)
const;
85 inline const std::string&
name(LinearConstraintId
id)
const;
87 template <
typename DiffIter>
91 template <
typename DiffIter>
99 template <
typename DiffIter>
106 inline int64_t
size()
const;
111 inline LinearConstraintId
next_id()
const;
117 inline bool contains(LinearConstraintId
id)
const;
129 template <
typename DiffIter>
134 template <
typename DiffIter>
135 void set_term(LinearConstraintId constraint, VariableId variable,
144 std::pair<LinearConstraintsProto, SparseDoubleMatrixProto>
Proto()
const;
162 const absl::flat_hash_set<VariableId>& deleted_variables,
163 absl::Span<const VariableId> new_variables)
const;
171 double lower_bound = -std::numeric_limits<double>::infinity();
172 double upper_bound = std::numeric_limits<double>::infinity();
176 std::vector<LinearConstraintId> ConstraintsFrom(
177 LinearConstraintId
start)
const;
179 void AppendConstraint(LinearConstraintId constraint,
180 LinearConstraintsProto*
proto)
const;
184 LinearConstraintsProto
Proto(LinearConstraintId
start,
185 LinearConstraintId
end)
const;
187 LinearConstraintId next_id_{0};
188 absl::flat_hash_map<LinearConstraintId, Data> linear_constraints_;
189 SparseMatrix<LinearConstraintId, VariableId> matrix_;
197 return linear_constraints_.at(
id).lower_bound;
201 return linear_constraints_.at(
id).upper_bound;
205 const LinearConstraintId
id)
const {
206 return linear_constraints_.at(
id).name;
209template <
typename DiffIter>
211 const LinearConstraintId
id,
const double lower_bound,
213 const auto it = linear_constraints_.find(
id);
218 for (Diff& diff : diffs) {
219 if (
id < diff.checkpoint) {
220 diff.lower_bounds.insert(
id);
225template <
typename DiffIter>
227 const LinearConstraintId
id,
const double upper_bound,
228 const iterator_range<DiffIter>& diffs) {
229 const auto it = linear_constraints_.find(
id);
234 for (Diff& diff : diffs) {
235 if (
id < diff.checkpoint) {
236 diff.upper_bounds.insert(
id);
241template <
typename DiffIter>
243 const iterator_range<DiffIter>& diffs) {
244 for (Diff& diff : diffs) {
246 if (
id >= diff.checkpoint) {
249 diff.lower_bounds.erase(
id);
250 diff.upper_bounds.erase(
id);
251 diff.deleted.insert(
id);
252 for (
const VariableId row_var : matrix_.
row(
id)) {
253 if (row_var < diff.variable_checkpoint) {
254 diff.matrix_keys.erase({id, row_var});
259 linear_constraints_.erase(
id);
262template <
typename DiffIter>
264 const VariableId variable,
const iterator_range<DiffIter>& diffs) {
265 for (Diff& diff : diffs) {
266 if (variable >= diff.variable_checkpoint) {
269 for (
const LinearConstraintId constraint : matrix_.
column(variable)) {
270 if (constraint < diff.checkpoint) {
271 diff.matrix_keys.erase({constraint, variable});
279 return linear_constraints_.size();
285 const LinearConstraintId minimum) {
286 next_id_ = std::max(minimum, next_id_);
290 return linear_constraints_.contains(
id);
293template <
typename DiffIter>
295 const VariableId variable,
298 DCHECK(linear_constraints_.contains(constraint));
299 if (!matrix_.
set(constraint, variable,
value)) {
302 for (Diff& diff : diffs) {
303 if (constraint < diff.checkpoint && variable < diff.variable_checkpoint) {
304 diff.matrix_keys.insert({constraint, variable});
310 return next_id_ <= diff.checkpoint && diff.deleted.empty() &&
311 diff.lower_bounds.empty() && diff.upper_bounds.empty() &&
312 diff.matrix_keys.empty();
void DeleteVariable(VariableId variable, const iterator_range< DiffIter > &diffs)
Removes all occurrences of var from the constraint matrix.
double lower_bound(LinearConstraintId id) const
LinearConstraintId next_id() const
bool diff_is_empty(const Diff &diff) const
void ensure_next_id_at_least(LinearConstraintId minimum)
Sets the next variable id to be the maximum of next_id() and minimum.
void set_upper_bound(LinearConstraintId id, double upper_bound, const iterator_range< DiffIter > &diffs)
UpdateResult Update(const Diff &diff, const absl::flat_hash_set< VariableId > &deleted_variables, absl::Span< const VariableId > new_variables) const
double upper_bound(LinearConstraintId id) const
void set_term(LinearConstraintId constraint, VariableId variable, double value, const iterator_range< DiffIter > &diffs)
Setting value=0 deletes the key from the matrix.
std::pair< LinearConstraintsProto, SparseDoubleMatrixProto > Proto() const
Returns an equivalent proto of this.
const SparseMatrix< LinearConstraintId, VariableId > & matrix() const
The matrix of coefficients for the linear terms in the constraints.
void AdvanceCheckpointInDiff(VariableId variable_checkpoint, Diff &diff) const
Updates the checkpoint and clears all stored changes in diff.
const std::string & name(LinearConstraintId id) const
void set_lower_bound(LinearConstraintId id, double lower_bound, const iterator_range< DiffIter > &diffs)
void Delete(LinearConstraintId id, const iterator_range< DiffIter > &diffs)
LinearConstraintId Add(double lower_bound, double upper_bound, absl::string_view name)
bool contains(LinearConstraintId id) const
Returns true if this id has been created and not yet deleted.
std::vector< LinearConstraintId > LinearConstraints() const
The LinearConstraintsIds in use (not deleted), order not defined.
std::vector< LinearConstraintId > SortedLinearConstraints() const
std::vector< RowId > column(ColumnId column_id) const
bool set(RowId row, ColumnId column, double value)
std::vector< ColumnId > row(RowId row_id) const
void DeleteColumn(ColumnId column)
Zeros out all coefficients for this variable.
void DeleteRow(RowId row)
Zeros out all coefficients for this variable.
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
An object oriented wrapper for quadratic constraints in ModelStorage.
std::optional< int64_t > end
absl::flat_hash_set< LinearConstraintId > lower_bounds
absl::flat_hash_set< LinearConstraintId > upper_bounds
absl::flat_hash_set< std::pair< LinearConstraintId, VariableId > > matrix_keys
LinearConstraintId checkpoint
Diff(const LinearConstraintStorage &storage, VariableId variable_checkpoint)
VariableId variable_checkpoint
absl::flat_hash_set< LinearConstraintId > deleted
LinearConstraintsProto creates
google::protobuf::RepeatedField< int64_t > deleted
LinearConstraintUpdatesProto updates
SparseDoubleMatrixProto matrix_updates
Result of the Update() on an incremental solver.