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"
50 {std::min(deleted_variable, v2), std::max(deleted_variable, v2)});
70 CHECK(!auxiliary_objectives_.contains(
id));
72 ObjectiveData& data = auxiliary_objectives_[id];
74 data.name = std::string(
name);
81 std::vector<AuxiliaryObjectiveId> ids;
83 for (
const auto& [
id, unused] : auxiliary_objectives_) {
96std::pair<ObjectiveProto, google::protobuf::Map<int64_t, ObjectiveProto>>
98 google::protobuf::Map<int64_t, ObjectiveProto> auxiliary_objectives;
99 for (
const auto& [
id, objective] : auxiliary_objectives_) {
100 auxiliary_objectives[
id.value()] = objective.Proto();
102 return {primary_objective_.Proto(), std::move(auxiliary_objectives)};
107void EnsureHasValue(std::optional<ObjectiveUpdatesProto>& update) {
108 if (!update.has_value()) {
115std::optional<ObjectiveUpdatesProto> ObjectiveStorage::ObjectiveData::Update(
116 const Diff::SingleObjective& diff_data,
117 const absl::flat_hash_set<VariableId>& deleted_variables,
118 absl::Span<const VariableId> new_variables)
const {
119 std::optional<ObjectiveUpdatesProto> update_proto;
121 if (diff_data.direction) {
122 EnsureHasValue(update_proto);
123 update_proto->set_direction_update(
maximize);
125 if (diff_data.priority) {
126 EnsureHasValue(update_proto);
127 update_proto->set_priority_update(
priority);
129 if (diff_data.offset) {
130 EnsureHasValue(update_proto);
131 update_proto->set_offset_update(
offset);
134 EnsureHasValue(update_proto);
135 update_proto->mutable_linear_coefficients()->add_ids(v.value());
136 update_proto->mutable_linear_coefficients()->add_values(
142 EnsureHasValue(update_proto);
143 update_proto->mutable_linear_coefficients()->add_ids(v.value());
144 update_proto->mutable_linear_coefficients()->add_values(val);
148 deleted_variables, new_variables, diff_data.quadratic_coefficients);
149 if (!quadratic_update.row_ids().empty()) {
151 EnsureHasValue(update_proto);
152 *update_proto->mutable_quadratic_coefficients() =
153 std::move(quadratic_update);
158std::pair<ObjectiveUpdatesProto, AuxiliaryObjectivesUpdatesProto>
160 const Diff& diff,
const absl::flat_hash_set<VariableId>& deleted_variables,
161 absl::Span<const VariableId> new_variables)
const {
169 for (
const auto& [
id, objective] : auxiliary_objectives_) {
177 auxiliary_objectives_.at(
id).Proto();
184 std::optional<ObjectiveUpdatesProto> update_proto =
186 deleted_variables, new_variables);
187 if (update_proto.has_value()) {
192 *std::move(update_proto);
196 return {primary_objective_
199 deleted_variables, new_variables)
201 std::move(auxiliary_result)};
::google::protobuf::RepeatedField<::int64_t > *PROTOBUF_NONNULL mutable_deleted_objective_ids()
::google::protobuf::Map<::int64_t, ::operations_research::math_opt::ObjectiveProto > *PROTOBUF_NONNULL mutable_new_objectives()
void add_deleted_objective_ids(::int64_t value)
::google::protobuf::Map<::int64_t, ::operations_research::math_opt::ObjectiveUpdatesProto > *PROTOBUF_NONNULL mutable_objective_updates()
::operations_research::math_opt::SparseDoubleVectorProto *PROTOBUF_NONNULL mutable_linear_coefficients()
::operations_research::math_opt::SparseDoubleMatrixProto *PROTOBUF_NONNULL mutable_quadratic_coefficients()
void set_offset(double value)
void set_name(Arg_ &&arg, Args_... args)
void set_maximize(bool value)
void set_priority(::int64_t value)
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
const std::string & name(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
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
An object oriented wrapper for quadratic constraints in ModelStorage.
ElementId< ElementType::kAuxiliaryObjective > AuxiliaryObjectiveId
ElementId< ElementType::kVariable > VariableId
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