Skip to content

Commit

Permalink
Consolidated sparse matrix loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Foggalong committed Jul 24, 2024
1 parent 2800068 commit 9273dda
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
66 changes: 26 additions & 40 deletions robustocs/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

def count_sparse_nnz(filename: str) -> int:
"""
Return the number of non-zero entries for a matrix stored in symmetric
sparse coordinate format.
Return the number of non-zero entries for a matrix stored in a file
in symmetric sparse coordinate (COO) format.
Parameters
----------
Expand Down Expand Up @@ -78,11 +78,11 @@ def load_symmetric_matrix(filename: str, dimension: int
return matrix


def load_symmetric_matrix_coo(filename: str, dimension: int, nnz: int
) -> sparse.spmatrix:
def load_sparse_symmetric_matrix(filename: str, dimension: int, nnz: int,
format: str = 'csr') -> sparse.spmatrix:
"""
Since neither NumPy or SciPy have a stock way to load symmetric matrices
into sparse coordinate format, this adds one.
stored in symmetric coordinate format into SciPy's formats, this adds one.
Parameters
----------
Expand All @@ -93,10 +93,14 @@ def load_symmetric_matrix_coo(filename: str, dimension: int, nnz: int
nnz : int
Number of non-zero entries in the matrix stored in the file. Note that
due to symmetry, this may be larger than the number of lines in file.
format : str
Format to use for the sparse matrix. Options available are 'coo' (for
sparse coordinate format) or 'csr' (compressed sparse row format).
Default value is `csr`.
Returns
-------
ndarray
spmatrix
The matrix represented by the file.
"""

Expand Down Expand Up @@ -126,38 +130,20 @@ def load_symmetric_matrix_coo(filename: str, dimension: int, nnz: int

index += 1

return sparse.coo_matrix(
(vals, (rows, cols)),
shape=(dimension, dimension),
dtype=np.floating
)


def load_symmetric_matrix_csr(filename: str, dimension: int, nnz: int
) -> sparse.spmatrix:
"""
Loads a symmetric matrix into compressed sparse row format. It does this
by first loading into sparse coordinate format and then converting with
Scipy. TODO: add a more efficient way which loads into CSR directly.
Parameters
----------
filename : str
Filename, including extension. Space-separated data file.
dimension : int
Number of rows (and columns) of the matrix stored in the file.
nnz : int
Number of non-zero entries in the matrix stored in the file. Note that
due to symmetry, this may be larger than the number of lines in file.
Returns
-------
ndarray
The matrix represented by the file.
"""

matrix = load_symmetric_matrix_coo(filename, dimension, nnz)
return sparse.csr_matrix(matrix)
if format == 'coo':
return sparse.coo_matrix(
(vals, (rows, cols)),
shape=(dimension, dimension),
dtype=np.floating
)
elif format == 'csr':
return sparse.csr_matrix(
(vals, (rows, cols)),
shape=(dimension, dimension),
dtype=np.floating
)
else:
raise ValueError("Format must be 'coo' or 'csr'.")


def load_ped(filename: str) -> dict[int, list[int]]:
Expand Down Expand Up @@ -296,7 +282,7 @@ def load_problem(A_filename: str, E_filename: str, S_filename: str,
if issparse:
if not nnzS:
nnzS = count_sparse_nnz(S_filename)
S = load_symmetric_matrix_csr(S_filename, dimension, nnzS)
S = load_sparse_symmetric_matrix(S_filename, dimension, nnzS)
else:
# if nnzS was defined here, it's ignored as a parameter
S = load_symmetric_matrix(S_filename, dimension)
Expand All @@ -312,7 +298,7 @@ def load_problem(A_filename: str, E_filename: str, S_filename: str,
if issparse:
if not nnzA:
nnzA = count_sparse_nnz(A_filename)
A = load_symmetric_matrix_csr(A_filename, dimension, nnzA)
A = load_sparse_symmetric_matrix(A_filename, dimension, nnzA)
else:
# if nnzA was defined here, it's ignored as a parameter
A = load_symmetric_matrix(A_filename, dimension)
Expand Down
1 change: 0 additions & 1 deletion robustocs/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ def highs_standard_genetics(
model.lp_.col_upper_ = highs_bound_like(dimension, upper_bound)

# define the quadratic term in the objective
sigma = sparse.csc_matrix(sigma) # BUG is sigma in CSR or CSC format?
model.hessian_.dim_ = dimension
model.hessian_.start_ = sigma.indptr
model.hessian_.index_ = sigma.indices
Expand Down

0 comments on commit 9273dda

Please sign in to comment.