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

#include <linear_constraint_storage.h>

Classes

struct  Diff
 
struct  UpdateResult
 

Public Member Functions

LinearConstraintId Add (double lower_bound, double upper_bound, absl::string_view name)
 
double lower_bound (LinearConstraintId id) const
 
double upper_bound (LinearConstraintId id) const
 
const std::string & name (LinearConstraintId id) const
 
template<typename DiffIter >
void set_lower_bound (LinearConstraintId id, double lower_bound, const iterator_range< DiffIter > &diffs)
 
template<typename DiffIter >
void set_upper_bound (LinearConstraintId id, double upper_bound, const iterator_range< DiffIter > &diffs)
 
template<typename DiffIter >
void Delete (LinearConstraintId id, const iterator_range< DiffIter > &diffs)
 
int64_t size () const
 
LinearConstraintId next_id () const
 
void ensure_next_id_at_least (LinearConstraintId minimum)
 Sets the next variable id to be the maximum of next_id() and minimum.
 
bool contains (LinearConstraintId id) const
 Returns true if this id has been created and not yet deleted.
 
std::vector< LinearConstraintId > LinearConstraints () const
 The LinearConstraintsIds in use (not deleted), order not defined.
 
std::vector< LinearConstraintId > SortedLinearConstraints () const
 
template<typename DiffIter >
void DeleteVariable (VariableId variable, const iterator_range< DiffIter > &diffs)
 Removes all occurrences of var from the constraint matrix.
 
template<typename DiffIter >
void set_term (LinearConstraintId constraint, VariableId variable, double value, const iterator_range< DiffIter > &diffs)
 Setting value=0 deletes the key from the matrix.
 
const SparseMatrix< LinearConstraintId, VariableId > & matrix () const
 The matrix of coefficients for the linear terms in the constraints.
 
std::pair< LinearConstraintsProto, SparseDoubleMatrixProto > Proto () const
 Returns an equivalent proto of this.
 
bool diff_is_empty (const Diff &diff) const
 
UpdateResult 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 linear constraints of an optimization model.

The setter functions all accept a DiffIter, which must be an iterator over non-const references to LinearConstraintStorage::Diff. These functions will modify the LinearConstraintStorage::Diff objects.

Definition at line 45 of file linear_constraint_storage.h.

Member Function Documentation

◆ Add()

LinearConstraintId operations_research::math_opt::LinearConstraintStorage::Add ( double lower_bound,
double upper_bound,
absl::string_view name )

Adds a linear constraint 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 36 of file linear_constraint_storage.cc.

◆ AdvanceCheckpointInDiff()

void operations_research::math_opt::LinearConstraintStorage::AdvanceCheckpointInDiff ( VariableId variable_checkpoint,
Diff & diff ) const

Updates the checkpoint and clears all stored changes in diff.

Definition at line 113 of file linear_constraint_storage.cc.

◆ contains()

bool operations_research::math_opt::LinearConstraintStorage::contains ( LinearConstraintId id) const
inline

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

Definition at line 293 of file linear_constraint_storage.h.

◆ Delete()

template<typename DiffIter >
void operations_research::math_opt::LinearConstraintStorage::Delete ( LinearConstraintId id,
const iterator_range< DiffIter > & diffs )

Removes a linear constraint from the model.

It is an error to use a deleted linear constraint id as input to any subsequent function calls on the model.

if the constraint >= checkpoint_, we don't store any info.

Definition at line 246 of file linear_constraint_storage.h.

◆ DeleteVariable()

template<typename DiffIter >
void operations_research::math_opt::LinearConstraintStorage::DeleteVariable ( VariableId variable,
const iterator_range< DiffIter > & diffs )

Removes all occurrences of var from the constraint matrix.

Definition at line 267 of file linear_constraint_storage.h.

◆ diff_is_empty()

bool operations_research::math_opt::LinearConstraintStorage::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 or new constraints after the checkpoint).

Note
when a linear constraint coefficient is modified for a variable past the checkpoint, the Diff object can be empty (and diff_is_empty will return true), but Update() can return a non-empty UpdateResult. This behavior MAY CHANGE in the future, so diff_is_empty is true iff the UpdateResult returned by Update() is empty (a more intuitive API, harder to implement efficiently).

Definition at line 313 of file linear_constraint_storage.h.

◆ ensure_next_id_at_least()

void operations_research::math_opt::LinearConstraintStorage::ensure_next_id_at_least ( LinearConstraintId minimum)
inline

Sets the next variable id to be the maximum of next_id() and minimum.

Definition at line 288 of file linear_constraint_storage.h.

◆ LinearConstraints()

std::vector< LinearConstraintId > operations_research::math_opt::LinearConstraintStorage::LinearConstraints ( ) const

The LinearConstraintsIds in use (not deleted), order not defined.

Definition at line 47 of file linear_constraint_storage.cc.

◆ lower_bound()

double operations_research::math_opt::LinearConstraintStorage::lower_bound ( LinearConstraintId id) const
inline

Inline function implementation

Definition at line 200 of file linear_constraint_storage.h.

◆ matrix()

const SparseMatrix< LinearConstraintId, VariableId > & operations_research::math_opt::LinearConstraintStorage::matrix ( ) const
inline

The matrix of coefficients for the linear terms in the constraints.

Definition at line 139 of file linear_constraint_storage.h.

◆ name()

const std::string & operations_research::math_opt::LinearConstraintStorage::name ( LinearConstraintId id) const
inline

Definition at line 208 of file linear_constraint_storage.h.

◆ next_id()

LinearConstraintId operations_research::math_opt::LinearConstraintStorage::next_id ( ) const
inline

The returned id of the next call to AddLinearConstraint.

Equal to the number of linear constraints created.

Definition at line 286 of file linear_constraint_storage.h.

◆ Proto()

std::pair< LinearConstraintsProto, SparseDoubleMatrixProto > operations_research::math_opt::LinearConstraintStorage::Proto ( ) const

Returns an equivalent proto of this.

Definition at line 76 of file linear_constraint_storage.cc.

◆ set_lower_bound()

template<typename DiffIter >
void operations_research::math_opt::LinearConstraintStorage::set_lower_bound ( LinearConstraintId id,
double lower_bound,
const iterator_range< DiffIter > & diffs )

Definition at line 214 of file linear_constraint_storage.h.

◆ set_term()

template<typename DiffIter >
void operations_research::math_opt::LinearConstraintStorage::set_term ( LinearConstraintId constraint,
VariableId variable,
double value,
const iterator_range< DiffIter > & diffs )

Setting value=0 deletes the key from the matrix.

Definition at line 298 of file linear_constraint_storage.h.

◆ set_upper_bound()

template<typename DiffIter >
void operations_research::math_opt::LinearConstraintStorage::set_upper_bound ( LinearConstraintId id,
double upper_bound,
const iterator_range< DiffIter > & diffs )

Definition at line 230 of file linear_constraint_storage.h.

◆ size()

int64_t operations_research::math_opt::LinearConstraintStorage::size ( ) const
inline

The number of linear constraints in the model.

Equal to the number of linear constraints created minus the number of linear constraints deleted.

Definition at line 282 of file linear_constraint_storage.h.

◆ SortedLinearConstraints()

std::vector< LinearConstraintId > operations_research::math_opt::LinearConstraintStorage::SortedLinearConstraints ( ) const

Returns a sorted vector of all existing (not deleted) linear constraints in the model.

Runs in O(n log(n)), where n is the number of linear constraints returned.

Definition at line 57 of file linear_constraint_storage.cc.

◆ Update()

LinearConstraintStorage::UpdateResult operations_research::math_opt::LinearConstraintStorage::Update ( const Diff & diff,
const absl::flat_hash_set< VariableId > & deleted_variables,
absl::Span< const VariableId > new_variables ) const

Definition at line 124 of file linear_constraint_storage.cc.

◆ upper_bound()

double operations_research::math_opt::LinearConstraintStorage::upper_bound ( LinearConstraintId id) const
inline

Definition at line 204 of file linear_constraint_storage.h.


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