Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <sparse_matrix.h>
Public Member Functions | |
bool | set (RowId row, ColumnId column, double value) |
double | get (RowId row, ColumnId column) const |
Zero is returned if the value is not present. | |
bool | contains (RowId row, ColumnId column) const |
Returns true if the value is present (nonzero). | |
void | DeleteRow (RowId row) |
Zeros out all coefficients for this variable. | |
void | DeleteColumn (ColumnId column) |
Zeros out all coefficients for this variable. | |
std::vector< ColumnId > | row (RowId row_id) const |
std::vector< RowId > | column (ColumnId column_id) const |
std::vector< std::pair< ColumnId, double > > | RowTerms (RowId row_id) const |
std::vector< std::pair< RowId, double > > | ColumnTerms (ColumnId col_id) const |
std::vector< std::tuple< RowId, ColumnId, double > > | Terms () const |
void | Clear () |
Removes all terms from the matrix. | |
int64_t | nonzeros () const |
The number of (row, column) keys with nonzero value. | |
int64_t | impl_detail_matrix_storage_size () const |
SparseDoubleMatrixProto | Proto () const |
SparseDoubleMatrixProto | Update (const absl::flat_hash_set< RowId > &deleted_rows, absl::Span< const RowId > new_rows, const absl::flat_hash_set< ColumnId > &deleted_columns, absl::Span< const ColumnId > new_columns, const absl::flat_hash_set< std::pair< RowId, ColumnId > > &dirty) const |
A sparse double valued matrix over int-like rows and columns.
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 or columns with many deletions may be slow).
Implementation: The entries are stored in a flat_hash_map<pair<RowId, ColumnId>, double> values_
. Additionally, we maintain a flat_hash_map<RowId, vector<ColumnId>> rows_
and a flat_hash_map<ColumnId, vector<RowID>> columns_
that enable efficient queries of the nonzeros in any row or column. When a coefficient is set to zero or a variable is deleted, we do not immediately delete the data from values_
, rows_
, or columns_
, 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 internal::kZerosCleanup
), we clean up rows_
, columns_
, 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 175 of file sparse_matrix.h.
void operations_research::math_opt::SparseMatrix< RowId, ColumnId >::Clear | ( | ) |
Removes all terms from the matrix.
Definition at line 505 of file sparse_matrix.h.
std::vector< RowId > operations_research::math_opt::SparseMatrix< RowId, ColumnId >::column | ( | ColumnId | column_id | ) | const |
Returns the variables that have nonzero entries with variable
.
Definition at line 445 of file sparse_matrix.h.
std::vector< std::pair< RowId, double > > operations_research::math_opt::SparseMatrix< RowId, ColumnId >::ColumnTerms | ( | ColumnId | col_id | ) | 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 477 of file sparse_matrix.h.
bool operations_research::math_opt::SparseMatrix< RowId, ColumnId >::contains | ( | RowId | row, |
ColumnId | column ) const |
Returns true if the value is present (nonzero).
Definition at line 391 of file sparse_matrix.h.
void operations_research::math_opt::SparseMatrix< RowId, ColumnId >::DeleteColumn | ( | ColumnId | column | ) |
Zeros out all coefficients for this variable.
Definition at line 414 of file sparse_matrix.h.
void operations_research::math_opt::SparseMatrix< RowId, ColumnId >::DeleteRow | ( | RowId | row | ) |
Zeros out all coefficients for this variable.
Definition at line 398 of file sparse_matrix.h.
double operations_research::math_opt::SparseMatrix< RowId, ColumnId >::get | ( | RowId | row, |
ColumnId | column ) const |
Zero is returned if the value is not present.
Definition at line 382 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 230 of file sparse_matrix.h.
int64_t operations_research::math_opt::SparseMatrix< RowId, ColumnId >::nonzeros | ( | ) | const |
The number of (row, column) keys with nonzero value.
Definition at line 512 of file sparse_matrix.h.
SparseDoubleMatrixProto operations_research::math_opt::SparseMatrix< RowId, ColumnId >::Proto | ( | ) | const |
Definition at line 517 of file sparse_matrix.h.
std::vector< ColumnId > operations_research::math_opt::SparseMatrix< RowId, ColumnId >::row | ( | RowId | row_id | ) | const |
Returns the variables that have nonzero entries with variable
.
Definition at line 430 of file sparse_matrix.h.
std::vector< std::pair< ColumnId, double > > operations_research::math_opt::SparseMatrix< RowId, ColumnId >::RowTerms | ( | RowId | row_id | ) | 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 461 of file sparse_matrix.h.
bool operations_research::math_opt::SparseMatrix< RowId, ColumnId >::set | ( | RowId | row, |
ColumnId | column, | ||
double | value ) |
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 352 of file sparse_matrix.h.
std::vector< std::tuple< RowId, ColumnId, double > > operations_research::math_opt::SparseMatrix< RowId, ColumnId >::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 493 of file sparse_matrix.h.
SparseDoubleMatrixProto operations_research::math_opt::SparseMatrix< RowId, ColumnId >::Update | ( | const absl::flat_hash_set< RowId > & | deleted_rows, |
absl::Span< const RowId > | new_rows, | ||
const absl::flat_hash_set< ColumnId > & | deleted_columns, | ||
absl::Span< const ColumnId > | new_columns, | ||
const absl::flat_hash_set< std::pair< RowId, ColumnId > > & | dirty ) const |
Extract changes to the matrix of linear constraint coefficients
NOTE(user): we already have the columns above the checkpoint from the loop above, but we will do at most twice as much as needed here.
Definition at line 530 of file sparse_matrix.h.