Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
In memory representation of the objective of an optimization model. More...
#include <objective_storage.h>
Classes | |
struct | Diff |
Public Member Functions | |
ObjectiveStorage (absl::string_view name={}) | |
AuxiliaryObjectiveId | AddAuxiliaryObjective (int64_t priority, absl::string_view name) |
bool | maximize (ObjectiveId id) const |
int64_t | priority (ObjectiveId id) const |
double | offset (ObjectiveId id) const |
double | linear_term (ObjectiveId id, VariableId v) const |
double | quadratic_term (ObjectiveId id, VariableId v1, VariableId v2) const |
const std::string & | name (ObjectiveId id) const |
const absl::flat_hash_map< VariableId, double > & | linear_terms (ObjectiveId id) const |
const SparseSymmetricMatrix & | quadratic_terms (ObjectiveId id) const |
template<typename DiffIter > | |
void | set_maximize (ObjectiveId id, bool maximize, const iterator_range< DiffIter > &diffs) |
template<typename DiffIter > | |
void | set_priority (ObjectiveId id, int64_t priority, const iterator_range< DiffIter > &diffs) |
template<typename DiffIter > | |
void | set_offset (ObjectiveId id, double offset, const iterator_range< DiffIter > &diffs) |
template<typename DiffIter > | |
void | set_linear_term (ObjectiveId id, VariableId variable, double value, const iterator_range< DiffIter > &diffs) |
template<typename DiffIter > | |
void | set_quadratic_term (ObjectiveId id, VariableId v1, VariableId v2, double val, const iterator_range< DiffIter > &diffs) |
template<typename DiffIter > | |
void | Delete (AuxiliaryObjectiveId id, const iterator_range< DiffIter > &diffs) |
int64_t | num_auxiliary_objectives () const |
AuxiliaryObjectiveId | next_id () const |
void | ensure_next_id_at_least (AuxiliaryObjectiveId minimum) |
bool | contains (AuxiliaryObjectiveId id) const |
Returns true if this id has been created and not yet deleted. | |
std::vector< AuxiliaryObjectiveId > | AuxiliaryObjectives () const |
The AuxiliaryObjectivesIds in use (not deleted), order not defined. | |
std::vector< AuxiliaryObjectiveId > | SortedAuxiliaryObjectives () const |
template<typename DiffIter > | |
void | Clear (ObjectiveId id, const iterator_range< DiffIter > &diffs) |
template<typename DiffIter > | |
void | DeleteVariable (VariableId variable, const iterator_range< DiffIter > &diffs) |
std::pair< ObjectiveProto, google::protobuf::Map< int64_t, ObjectiveProto > > | Proto () const |
bool | diff_is_empty (const Diff &diff) const |
std::pair< ObjectiveUpdatesProto, AuxiliaryObjectivesUpdatesProto > | Update (const Diff &diff, const absl::flat_hash_set< VariableId > &deleted_variables, absl::Span< const VariableId > new_variables) const |
void | AdvanceCheckpointInDiff (VariableId variable_checkpoint, Diff &diff) const |
Updates the checkpoint and clears all stored changes in diff. | |
In memory representation of the objective of an optimization model.
Definition at line 38 of file objective_storage.h.
|
inlineexplicit |
Definition at line 248 of file objective_storage.h.
AuxiliaryObjectiveId operations_research::math_opt::ObjectiveStorage::AddAuxiliaryObjective | ( | int64_t | priority, |
absl::string_view | name ) |
Adds an auxiliary objective to the model and returns its id.
The returned ids begin at zero and strictly increase (in particular, if ensure_next_id_at_least() is not used, they will be consecutive). Deleted ids are NOT reused.
Definition at line 66 of file objective_storage.cc.
void operations_research::math_opt::ObjectiveStorage::AdvanceCheckpointInDiff | ( | VariableId | variable_checkpoint, |
Diff & | diff ) const |
Updates the checkpoint and clears all stored changes in diff.
Definition at line 203 of file objective_storage.cc.
std::vector< AuxiliaryObjectiveId > operations_research::math_opt::ObjectiveStorage::AuxiliaryObjectives | ( | ) | const |
The AuxiliaryObjectivesIds in use (not deleted), order not defined.
Definition at line 78 of file objective_storage.cc.
void operations_research::math_opt::ObjectiveStorage::Clear | ( | ObjectiveId | id, |
const iterator_range< DiffIter > & | diffs ) |
Clears the objective function (coefficients and offset), but not the sense or priority.
Definition at line 392 of file objective_storage.h.
|
inline |
Returns true if this id has been created and not yet deleted.
Definition at line 387 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::Delete | ( | AuxiliaryObjectiveId | id, |
const iterator_range< DiffIter > & | diffs ) |
Removes an auxiliary objective from the model.
It is an error to use a deleted auxiliary objective id as input to any subsequent function calls on the model.
Definition at line 362 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::DeleteVariable | ( | VariableId | variable, |
const iterator_range< DiffIter > & | diffs ) |
Removes all occurrences of var from the objective. Runs in O(# objectives) time (though this can potentially be improved to O(1) if the need arises).
Definition at line 416 of file objective_storage.h.
|
inline |
Functions for working with Diff Returns true if there are no changes (tracked changes before the checkpoint).
There is a new auxiliary objective that needs extracting.
We must apply an objective modification.
If nonempty we need to delete some auxiliary objectives.
Definition at line 430 of file objective_storage.h.
|
inline |
Sets the next auxiliary objective id to be the maximum of next_id() and minimum
.
Definition at line 382 of file objective_storage.h.
|
inline |
Definition at line 264 of file objective_storage.h.
|
inline |
Definition at line 279 of file objective_storage.h.
|
inline |
Definition at line 252 of file objective_storage.h.
|
inline |
Definition at line 275 of file objective_storage.h.
|
inline |
The returned id of the next call to AddAuxiliaryObjective.
Equal to the number of auxiliary objectives created.
Definition at line 378 of file objective_storage.h.
|
inline |
The number of auxiliary objectives in the model.
Equal to the number of auxiliary objectives created minus the number of auxiliary objectives deleted.
Definition at line 374 of file objective_storage.h.
|
inline |
Definition at line 260 of file objective_storage.h.
|
inline |
Definition at line 256 of file objective_storage.h.
std::pair< ObjectiveProto, google::protobuf::Map< int64_t, ObjectiveProto > > operations_research::math_opt::ObjectiveStorage::Proto | ( | ) | const |
Returns a proto description for the primary objective (.first) and all auxiliary objectives (.second).
Definition at line 96 of file objective_storage.cc.
|
inline |
Definition at line 269 of file objective_storage.h.
|
inline |
Definition at line 284 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::set_linear_term | ( | ObjectiveId | id, |
VariableId | variable, | ||
double | value, | ||
const iterator_range< DiffIter > & | diffs ) |
Definition at line 333 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::set_maximize | ( | ObjectiveId | id, |
bool | maximize, | ||
const iterator_range< DiffIter > & | diffs ) |
Definition at line 290 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::set_offset | ( | ObjectiveId | id, |
double | offset, | ||
const iterator_range< DiffIter > & | diffs ) |
Definition at line 319 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::set_priority | ( | ObjectiveId | id, |
int64_t | priority, | ||
const iterator_range< DiffIter > & | diffs ) |
Definition at line 304 of file objective_storage.h.
void operations_research::math_opt::ObjectiveStorage::set_quadratic_term | ( | ObjectiveId | id, |
VariableId | v1, | ||
VariableId | v2, | ||
double | val, | ||
const iterator_range< DiffIter > & | diffs ) |
Definition at line 347 of file objective_storage.h.
std::vector< AuxiliaryObjectiveId > operations_research::math_opt::ObjectiveStorage::SortedAuxiliaryObjectives | ( | ) | const |
Returns a sorted vector of all existing (not deleted) auxiliary objectives in the model.
Runs in O(n log(n)), where n is the number of auxiliary objectives returned.
Definition at line 88 of file objective_storage.cc.
std::pair< ObjectiveUpdatesProto, AuxiliaryObjectivesUpdatesProto > operations_research::math_opt::ObjectiveStorage::Update | ( | const Diff & | diff, |
const absl::flat_hash_set< VariableId > & | deleted_variables, | ||
absl::Span< const VariableId > | new_variables ) const |
Delete()
d objective will not be in the objectives_
map. Hence, each entry is either new (if not extracted) or potentially an update on an existing objective.An un-extracted objective goes in the new_objectives
map. It is fresh and so there is no need to update, so we continue.
Diff
provides no guarantees on which objectives will have entries in objective_diffs
; a missing entry is equivalent to one with an empty key.
If the update message is empty we do not export it. This is particularly important for auxiliary objectives as we do not want to add empty map entries.
Definition at line 158 of file objective_storage.cc.