23#include "absl/algorithm/container.h"
24#include "absl/container/flat_hash_set.h"
25#include "absl/log/check.h"
26#include "absl/strings/string_view.h"
27#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"
40 const VariableId deleted_variable,
const VariableId variable_checkpoint,
46 for (
const VariableId v2 :
50 {std::min(deleted_variable, v2), std::max(deleted_variable, v2)});
55ObjectiveProto ObjectiveStorage::ObjectiveData::Proto()
const {
68 const AuxiliaryObjectiveId
id = next_id_++;
69 CHECK(!auxiliary_objectives_.contains(
id));
71 ObjectiveData& data = auxiliary_objectives_[id];
73 data.name = std::string(
name);
80 std::vector<AuxiliaryObjectiveId> ids;
82 for (
const auto& [
id, unused] : auxiliary_objectives_) {
95std::pair<ObjectiveProto, google::protobuf::Map<int64_t, ObjectiveProto>>
97 google::protobuf::Map<int64_t, ObjectiveProto> auxiliary_objectives;
98 for (
const auto& [
id, objective] : auxiliary_objectives_) {
99 auxiliary_objectives[
id.value()] = objective.Proto();
101 return {primary_objective_.Proto(), std::move(auxiliary_objectives)};
106void EnsureHasValue(std::optional<ObjectiveUpdatesProto>& update) {
107 if (!update.has_value()) {
114std::optional<ObjectiveUpdatesProto> ObjectiveStorage::ObjectiveData::Update(
115 const Diff::SingleObjective& diff_data,
116 const absl::flat_hash_set<VariableId>& deleted_variables,
117 absl::Span<const VariableId> new_variables)
const {
118 std::optional<ObjectiveUpdatesProto> update_proto;
120 if (diff_data.direction) {
121 EnsureHasValue(update_proto);
122 update_proto->set_direction_update(
maximize);
124 if (diff_data.priority) {
125 EnsureHasValue(update_proto);
126 update_proto->set_priority_update(
priority);
128 if (diff_data.offset) {
129 EnsureHasValue(update_proto);
130 update_proto->set_offset_update(
offset);
133 EnsureHasValue(update_proto);
134 update_proto->mutable_linear_coefficients()->add_ids(v.value());
135 update_proto->mutable_linear_coefficients()->add_values(
138 for (
const VariableId v : new_variables) {
141 EnsureHasValue(update_proto);
142 update_proto->mutable_linear_coefficients()->add_ids(v.value());
143 update_proto->mutable_linear_coefficients()->add_values(val);
147 deleted_variables, new_variables, diff_data.quadratic_coefficients);
148 if (!quadratic_update.row_ids().empty()) {
150 EnsureHasValue(update_proto);
151 *update_proto->mutable_quadratic_coefficients() =
152 std::move(quadratic_update);
157std::pair<ObjectiveUpdatesProto, AuxiliaryObjectivesUpdatesProto>
159 const Diff& diff,
const absl::flat_hash_set<VariableId>& deleted_variables,
160 absl::Span<const VariableId> new_variables)
const {
161 AuxiliaryObjectivesUpdatesProto auxiliary_result;
163 for (
const AuxiliaryObjectiveId
id : diff.
deleted) {
164 auxiliary_result.add_deleted_objective_ids(
id.
value());
166 absl::c_sort(*auxiliary_result.mutable_deleted_objective_ids());
168 for (
const auto& [
id, objective] : auxiliary_objectives_) {
175 (*auxiliary_result.mutable_new_objectives())[
id.value()] =
176 auxiliary_objectives_.at(
id).Proto();
183 std::optional<ObjectiveUpdatesProto> update_proto =
185 deleted_variables, new_variables);
186 if (update_proto.has_value()) {
190 (*auxiliary_result.mutable_objective_updates())[
id.value()] =
191 *std::move(update_proto);
195 return {primary_objective_
198 deleted_variables, new_variables)
199 .value_or(ObjectiveUpdatesProto{}),
200 std::move(auxiliary_result)};
double offset(ObjectiveId id) const
const SparseSymmetricMatrix & quadratic_terms(ObjectiveId id) const
std::pair< ObjectiveProto, google::protobuf::Map< int64_t, ObjectiveProto > > Proto() const
std::vector< AuxiliaryObjectiveId > SortedAuxiliaryObjectives() const
const absl::flat_hash_map< VariableId, double > & linear_terms(ObjectiveId id) const
bool maximize(ObjectiveId id) const
void AdvanceCheckpointInDiff(VariableId variable_checkpoint, Diff &diff) const
Updates the checkpoint and clears all stored changes in diff.
int64_t num_auxiliary_objectives() const
std::vector< AuxiliaryObjectiveId > AuxiliaryObjectives() const
The AuxiliaryObjectivesIds in use (not deleted), order not defined.
int64_t priority(ObjectiveId id) const
AuxiliaryObjectiveId AddAuxiliaryObjective(int64_t priority, absl::string_view name)
std::pair< ObjectiveUpdatesProto, AuxiliaryObjectivesUpdatesProto > Update(const Diff &diff, const absl::flat_hash_set< VariableId > &deleted_variables, absl::Span< const 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< VariableId > RelatedVariables(VariableId variable) const
SparseDoubleMatrixProto Proto() const
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
An object oriented wrapper for quadratic constraints in ModelStorage.
constexpr ObjectiveId kPrimaryObjectiveId
std::vector< T > SortedSetElements(const absl::flat_hash_set< T > &elements)
absl::flat_hash_set< std::pair< VariableId, VariableId > > quadratic_coefficients
void DeleteVariable(VariableId deleted_variable, VariableId variable_checkpoint, const SparseSymmetricMatrix &quadratic_terms)
absl::flat_hash_set< VariableId > linear_coefficients
bool objective_tracked(ObjectiveId id) const
absl::flat_hash_map< ObjectiveId, SingleObjective > objective_diffs
absl::flat_hash_set< AuxiliaryObjectiveId > deleted
VariableId variable_checkpoint
AuxiliaryObjectiveId objective_checkpoint