21#include "absl/algorithm/container.h"
22#include "absl/container/flat_hash_map.h"
23#include "absl/container/flat_hash_set.h"
24#include "absl/strings/string_view.h"
25#include "absl/types/span.h"
28#include "ortools/math_opt/model.pb.h"
29#include "ortools/math_opt/model_update.pb.h"
30#include "ortools/math_opt/sparse_containers.pb.h"
38 const absl::string_view
name) {
39 const LinearConstraintId
id = next_id_++;
40 Data& lin_con_data = linear_constraints_[id];
43 lin_con_data.name = std::string(
name);
49 std::vector<LinearConstraintId> result;
50 for (
const auto& [lin_con, _] : linear_constraints_) {
51 result.push_back(lin_con);
56std::vector<LinearConstraintId>
63std::vector<LinearConstraintId> LinearConstraintStorage::ConstraintsFrom(
64 const LinearConstraintId
start)
const {
65 std::vector<LinearConstraintId> result;
66 for (
const LinearConstraintId
c :
68 if (linear_constraints_.contains(
c)) {
75std::pair<LinearConstraintsProto, SparseDoubleMatrixProto>
77 LinearConstraintsProto constraints;
78 const std::vector<LinearConstraintId> sorted_constraints =
80 for (
const LinearConstraintId
id : sorted_constraints) {
81 AppendConstraint(
id, &constraints);
83 return {constraints, matrix_.
Proto()};
86void LinearConstraintStorage::AppendConstraint(
87 const LinearConstraintId constraint,
88 LinearConstraintsProto*
const proto)
const {
89 proto->add_ids(constraint.value());
90 const Data& data = linear_constraints_.at(constraint);
91 proto->add_lower_bounds(data.lower_bound);
92 proto->add_upper_bounds(data.upper_bound);
94 proto->add_names(data.name);
98 const LinearConstraintId
start,
const LinearConstraintId
end)
const {
99 LinearConstraintsProto result;
100 for (
const LinearConstraintId
id :
102 if (linear_constraints_.contains(
id)) {
103 AppendConstraint(
id, &result);
110 const VariableId variable_checkpoint)
111 : checkpoint(storage.next_id()), variable_checkpoint(variable_checkpoint) {}
114 const VariableId variable_checkpoint,
Diff& diff)
const {
125 const Diff& diff,
const absl::flat_hash_set<VariableId>& deleted_variables,
126 absl::Span<const VariableId> new_variables)
const {
128 for (
const LinearConstraintId
c : diff.
deleted) {
134 result.
updates.mutable_lower_bounds()->add_ids(
c.value());
139 result.
updates.mutable_upper_bounds()->add_ids(
c.value());
147 deleted_variables, new_variables, diff.
matrix_keys);
UpdateResult Update(const Diff &diff, const absl::flat_hash_set< VariableId > &deleted_variables, absl::Span< const VariableId > new_variables) const
std::pair< LinearConstraintsProto, SparseDoubleMatrixProto > Proto() const
Returns an equivalent proto of this.
void AdvanceCheckpointInDiff(VariableId variable_checkpoint, Diff &diff) const
Updates the checkpoint and clears all stored changes in diff.
LinearConstraintId Add(double lower_bound, double upper_bound, absl::string_view name)
std::vector< LinearConstraintId > LinearConstraints() const
The LinearConstraintsIds in use (not deleted), order not defined.
std::vector< LinearConstraintId > SortedLinearConstraints() const
SparseDoubleMatrixProto Update(const absl::flat_hash_set< RowId > &deleted_rows, absl::Span< const RowId > new_rows, const absl::flat_hash_set< ColumnId > &deleted_columns, absl::Span< const ColumnId > new_columns, const absl::flat_hash_set< std::pair< RowId, ColumnId > > &dirty) const
SparseDoubleMatrixProto Proto() const
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
An object oriented wrapper for quadratic constraints in ModelStorage.
std::vector< T > SortedSetElements(const absl::flat_hash_set< T > &elements)
StrongIntRange< IntType > MakeStrongIntRange(IntType end)
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