Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
linear_constraint_storage.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <algorithm>
17#include <string>
18#include <utility>
19#include <vector>
20
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"
33
35
36LinearConstraintId LinearConstraintStorage::Add(const double lower_bound,
37 const double upper_bound,
38 const absl::string_view name) {
39 const LinearConstraintId id = next_id_++;
40 Data& lin_con_data = linear_constraints_[id];
41 lin_con_data.lower_bound = lower_bound;
42 lin_con_data.upper_bound = upper_bound;
43 lin_con_data.name = std::string(name);
44 return id;
45}
46
47std::vector<LinearConstraintId> LinearConstraintStorage::LinearConstraints()
48 const {
49 std::vector<LinearConstraintId> result;
50 for (const auto& [lin_con, _] : linear_constraints_) {
51 result.push_back(lin_con);
52 }
53 return result;
54}
55
56std::vector<LinearConstraintId>
58 std::vector<LinearConstraintId> result = LinearConstraints();
59 absl::c_sort(result);
60 return result;
61}
62
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)) {
69 result.push_back(c);
70 }
71 }
72 return result;
73}
74
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);
82 }
83 return {constraints, matrix_.Proto()};
84}
85
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);
93 // TODO(b/238115672): we should potentially not fill this in on empty names.
94 proto->add_names(data.name);
95}
96
97LinearConstraintsProto LinearConstraintStorage::Proto(
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);
104 }
105 }
106 return result;
107}
108
110 const VariableId variable_checkpoint)
111 : checkpoint(storage.next_id()), variable_checkpoint(variable_checkpoint) {}
112
114 const VariableId variable_checkpoint, Diff& diff) const {
115 diff.checkpoint = std::max(diff.checkpoint, next_id_);
117 std::max(diff.variable_checkpoint, variable_checkpoint);
118 diff.deleted.clear();
119 diff.lower_bounds.clear();
120 diff.upper_bounds.clear();
121 diff.matrix_keys.clear();
122}
123
125 const Diff& diff, const absl::flat_hash_set<VariableId>& deleted_variables,
126 absl::Span<const VariableId> new_variables) const {
127 UpdateResult result;
128 for (const LinearConstraintId c : diff.deleted) {
129 result.deleted.Add(c.value());
130 }
131 absl::c_sort(result.deleted);
132
133 for (const LinearConstraintId c : SortedSetElements(diff.lower_bounds)) {
134 result.updates.mutable_lower_bounds()->add_ids(c.value());
135 result.updates.mutable_lower_bounds()->add_values(lower_bound(c));
136 }
137
138 for (const LinearConstraintId c : SortedSetElements(diff.upper_bounds)) {
139 result.updates.mutable_upper_bounds()->add_ids(c.value());
140 result.updates.mutable_upper_bounds()->add_values(upper_bound(c));
141 }
142
143 result.creates = Proto(diff.checkpoint, next_id_);
144
145 result.matrix_updates =
146 matrix_.Update(diff.deleted, ConstraintsFrom(diff.checkpoint),
147 deleted_variables, new_variables, diff.matrix_keys);
148 return result;
149}
150
151} // namespace operations_research::math_opt
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.
double upper_bound
double lower_bound
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::vector< T > SortedSetElements(const absl::flat_hash_set< T > &elements)
Definition sorted.h:28
StrongIntRange< IntType > MakeStrongIntRange(IntType end)
Definition strong_int.h:438
std::optional< int64_t > end
int64_t start
absl::flat_hash_set< std::pair< LinearConstraintId, VariableId > > matrix_keys
Diff(const LinearConstraintStorage &storage, VariableId variable_checkpoint)