OR-Tools  9.6
objective_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 <algorithm>
17 #include <vector>
18 
19 #include "absl/container/flat_hash_set.h"
21 #include "ortools/math_opt/model.pb.h"
22 #include "ortools/math_opt/model_update.pb.h"
23 #include "ortools/math_opt/sparse_containers.pb.h"
27 
29 
30 ObjectiveProto ObjectiveStorage::Proto() const {
31  ObjectiveProto result;
32  result.set_maximize(maximize_);
33  result.set_offset(offset_);
34  *result.mutable_linear_coefficients() = linear_terms_.Proto();
35  *result.mutable_quadratic_coefficients() = quadratic_terms_.Proto();
36  return result;
37 }
38 
39 ObjectiveUpdatesProto ObjectiveStorage::Update(
40  const Diff& diff, const absl::flat_hash_set<VariableId>& deleted_variables,
41  const std::vector<VariableId>& new_variables) const {
42  ObjectiveUpdatesProto result;
43  if (diff.direction) {
44  result.set_direction_update(maximize_);
45  }
46  if (diff.offset) {
47  result.set_offset_update(offset_);
48  }
49 
50  for (const VariableId v : SortedElements(diff.linear_coefficients)) {
51  result.mutable_linear_coefficients()->add_ids(v.value());
52  result.mutable_linear_coefficients()->add_values(linear_term(v));
53  }
54  for (const VariableId v : new_variables) {
55  const double val = linear_term(v);
56  if (val != 0.0) {
57  result.mutable_linear_coefficients()->add_ids(v.value());
58  result.mutable_linear_coefficients()->add_values(val);
59  }
60  }
61  *result.mutable_quadratic_coefficients() = quadratic_terms_.Update(
62  deleted_variables, new_variables, diff.quadratic_coefficients);
63  return result;
64 }
65 
67  const VariableId variable_checkpoint, Diff& diff) const {
68  diff.variable_checkpoint =
69  std::max(diff.variable_checkpoint, variable_checkpoint);
70  diff.offset = false;
71  diff.direction = false;
72  diff.linear_coefficients.clear();
73  diff.quadratic_coefficients.clear();
74 }
75 
76 } // namespace operations_research::math_opt
int64_t max
Definition: alldiff_cst.cc:140
void AdvanceCheckpointInDiff(VariableId variable_checkpoint, Diff &diff) const
ObjectiveUpdatesProto Update(const Diff &diff, const absl::flat_hash_set< VariableId > &deleted_variables, const std::vector< VariableId > &new_variables) const
SparseDoubleMatrixProto Update(const absl::flat_hash_set< VariableId > &deleted_variables, absl::Span< const VariableId > new_variables, const absl::flat_hash_set< std::pair< VariableId, VariableId >> &dirty) const
std::vector< T > SortedElements(const absl::flat_hash_set< T > &elements)
Definition: sorted.h:28
absl::flat_hash_set< std::pair< VariableId, VariableId > > quadratic_coefficients
absl::flat_hash_set< VariableId > linear_coefficients