Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <sparse_matrix.h>
Public Member Functions | |
bool | set (VariableId first, VariableId second, double value) |
double | get (VariableId first, VariableId second) const |
Zero is returned if the value is not present. | |
void | Delete (VariableId variable) |
Zeros out all coefficients for this variable. | |
std::vector< VariableId > | Variables () const |
std::vector< VariableId > | RelatedVariables (VariableId variable) const |
std::vector< std::pair< VariableId, double > > | Terms (VariableId variable) const |
std::vector< std::tuple< VariableId, VariableId, double > > | Terms () const |
void | Clear () |
Removes all terms from the matrix. | |
int64_t | nonzeros () const |
int64_t | impl_detail_matrix_storage_size () const |
const absl::flat_hash_map< std::pair< VariableId, VariableId >, double > & | values () const |
SparseDoubleMatrixProto | Proto () 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 |
A sparse symmetric double valued matrix over VariableIds.
Getting/setting/clearing entries are O(1) operations. Getting a row of the matrix runs in O(size of the row) if nothing has been deleted, and getting all the rows runs in O(number of nonzero entries), even with deletions (with deletions, accessing a particular row with many deletions may be slow).
Implementation: The entries are stored in a flat_hash_map<pair<VariableId, VariableId>, double> values_
where for each key, key.first <= key.second. Additionally, we maintain a flat_hash_map<VariableId, vector<VariableId>> related_variables_
that says for each variable, which variables they have a nonzero term with. When a coefficient is set to zero or a variable is deleted, we do not immediately delete the data from values_ or related_variables_, we simply set the coefficient to zero in values_. We track how many zeros are in values_, and when more than some constant fraction of all entries are zero (see kZerosCleanup in cc file), we clean up related_variables_ and values_ to remove all the zeros. Iteration over the rows or total entries of the matrix must check for zeros in values_ and skip these terms.
Memory use:
Definition at line 66 of file sparse_matrix.h.
void operations_research::math_opt::SparseSymmetricMatrix::Clear | ( | ) |
Removes all terms from the matrix.
Definition at line 134 of file sparse_matrix.cc.
void operations_research::math_opt::SparseSymmetricMatrix::Delete | ( | VariableId | variable | ) |
Zeros out all coefficients for this variable.
Definition at line 32 of file sparse_matrix.cc.
|
inline |
Zero is returned if the value is not present.
Definition at line 340 of file sparse_matrix.h.
|
inline |
For testing/debugging only, do not depend on this value, behavior may change based on implementation.
Definition at line 113 of file sparse_matrix.h.
|
inline |
The number of (var, var) keys with nonzero value. Note that (x, y) and (y, x) are the same key.
Definition at line 109 of file sparse_matrix.h.
SparseDoubleMatrixProto operations_research::math_opt::SparseSymmetricMatrix::Proto | ( | ) | const |
Definition at line 140 of file sparse_matrix.cc.
std::vector< VariableId > operations_research::math_opt::SparseSymmetricMatrix::RelatedVariables | ( | VariableId | variable | ) | const |
Returns the variables that have nonzero entries with variable
.
The return order is deterministic but not defined.
Definition at line 47 of file sparse_matrix.cc.
|
inline |
Setting value
to zero removes the value from the matrix. Returns true if value
is different from the existing value in the matrix.
Definition at line 308 of file sparse_matrix.h.
std::vector< std::tuple< VariableId, VariableId, double > > operations_research::math_opt::SparseSymmetricMatrix::Terms | ( | ) | const |
Returns (x, y, c) tuples where variables x and y have nonzero coefficient c, and x <= y.
The return order is non-deterministic and not defined.
Definition at line 92 of file sparse_matrix.cc.
std::vector< std::pair< VariableId, double > > operations_research::math_opt::SparseSymmetricMatrix::Terms | ( | VariableId | variable | ) | const |
Returns the variable value pairs (x, c) where variable
and x have nonzero coefficient c.
The return order is deterministic but not defined.
Definition at line 76 of file sparse_matrix.cc.
SparseDoubleMatrixProto operations_research::math_opt::SparseSymmetricMatrix::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 |
If either variable has been deleted, don't add it.
Definition at line 165 of file sparse_matrix.cc.
|
inline |
Definition at line 118 of file sparse_matrix.h.
std::vector< VariableId > operations_research::math_opt::SparseSymmetricMatrix::Variables | ( | ) | const |
Returns the variables with any nonzero in the matrix.
The return order is deterministic but not defined.
Definition at line 61 of file sparse_matrix.cc.