Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::math_opt::ObjectiveStorage Class Reference

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 SparseSymmetricMatrixquadratic_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.
 

Detailed Description

In memory representation of the objective of an optimization model.

Definition at line 38 of file objective_storage.h.

Constructor & Destructor Documentation

◆ ObjectiveStorage()

operations_research::math_opt::ObjectiveStorage::ObjectiveStorage ( absl::string_view name = {})
inlineexplicit

Definition at line 248 of file objective_storage.h.

Member Function Documentation

◆ AddAuxiliaryObjective()

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.

◆ AdvanceCheckpointInDiff()

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.

◆ AuxiliaryObjectives()

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.

◆ Clear()

template<typename DiffIter >
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.

◆ contains()

bool operations_research::math_opt::ObjectiveStorage::contains ( AuxiliaryObjectiveId id) const
inline

Returns true if this id has been created and not yet deleted.

Definition at line 387 of file objective_storage.h.

◆ Delete()

template<typename DiffIter >
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.

◆ DeleteVariable()

template<typename DiffIter >
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.

◆ diff_is_empty()

bool operations_research::math_opt::ObjectiveStorage::diff_is_empty ( const Diff & diff) const
inline

Functions for working with Diff Returns true if there are no changes (tracked changes before the checkpoint).

Note
when there are new variables with nonzero objective coefficient, the Diff object can be empty (and diff_is_empty will return true), but Update() can return a non-empty ObjectiveUpdatesProto. This behavior MAY CHANGE in the future (this new behavior would be more intuitive, though it is harder to implement efficiently).

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.

◆ ensure_next_id_at_least()

void operations_research::math_opt::ObjectiveStorage::ensure_next_id_at_least ( AuxiliaryObjectiveId minimum)
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.

◆ linear_term()

double operations_research::math_opt::ObjectiveStorage::linear_term ( ObjectiveId id,
VariableId v ) const
inline

Definition at line 264 of file objective_storage.h.

◆ linear_terms()

const absl::flat_hash_map< VariableId, double > & operations_research::math_opt::ObjectiveStorage::linear_terms ( ObjectiveId id) const
inline

Definition at line 279 of file objective_storage.h.

◆ maximize()

bool operations_research::math_opt::ObjectiveStorage::maximize ( ObjectiveId id) const
inline

Definition at line 252 of file objective_storage.h.

◆ name()

const std::string & operations_research::math_opt::ObjectiveStorage::name ( ObjectiveId id) const
inline

Definition at line 275 of file objective_storage.h.

◆ next_id()

AuxiliaryObjectiveId operations_research::math_opt::ObjectiveStorage::next_id ( ) const
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.

◆ num_auxiliary_objectives()

int64_t operations_research::math_opt::ObjectiveStorage::num_auxiliary_objectives ( ) const
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.

◆ offset()

double operations_research::math_opt::ObjectiveStorage::offset ( ObjectiveId id) const
inline

Definition at line 260 of file objective_storage.h.

◆ priority()

int64_t operations_research::math_opt::ObjectiveStorage::priority ( ObjectiveId id) const
inline

Definition at line 256 of file objective_storage.h.

◆ Proto()

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.

◆ quadratic_term()

double operations_research::math_opt::ObjectiveStorage::quadratic_term ( ObjectiveId id,
VariableId v1,
VariableId v2 ) const
inline

Definition at line 269 of file objective_storage.h.

◆ quadratic_terms()

const SparseSymmetricMatrix & operations_research::math_opt::ObjectiveStorage::quadratic_terms ( ObjectiveId id) const
inline

Definition at line 284 of file objective_storage.h.

◆ set_linear_term()

template<typename DiffIter >
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.

◆ set_maximize()

template<typename DiffIter >
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.

◆ set_offset()

template<typename DiffIter >
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.

◆ set_priority()

template<typename DiffIter >
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.

◆ set_quadratic_term()

template<typename DiffIter >
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.

◆ SortedAuxiliaryObjectives()

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.

◆ Update()

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
Note
any 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.


The documentation for this class was generated from the following files: