Skip to content

Commit

Permalink
Revert sparray adoption
Browse files Browse the repository at this point in the history
  • Loading branch information
Foggalong committed Jul 24, 2024
1 parent fe08695 commit 2800068
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
20 changes: 10 additions & 10 deletions robustocs/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def load_symmetric_matrix(filename: str, dimension: int


def load_symmetric_matrix_coo(filename: str, dimension: int, nnz: int
) -> sparse.sparray:
) -> sparse.spmatrix:
"""
Since neither NumPy or SciPy have a stock way to load symmetric matrices
into sparse coordinate format, this adds one.
Expand Down Expand Up @@ -126,15 +126,15 @@ def load_symmetric_matrix_coo(filename: str, dimension: int, nnz: int

index += 1

return sparse.coo_array(
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.sparray:
) -> 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
Expand All @@ -157,7 +157,7 @@ def load_symmetric_matrix_csr(filename: str, dimension: int, nnz: int
"""

matrix = load_symmetric_matrix_coo(filename, dimension, nnz)
return sparse.csr_array(matrix)
return sparse.csr_matrix(matrix)


def load_ped(filename: str) -> dict[int, list[int]]:
Expand Down Expand Up @@ -238,9 +238,9 @@ def load_problem(A_filename: str, E_filename: str, S_filename: str,
nnzA: int | None = None, nnzS: int | None = None,
dimension: int | None = None, pedigree: bool = False,
issparse: bool = False
) -> tuple[npt.NDArray[np.floating] | sparse.sparray,
) -> tuple[npt.NDArray[np.floating] | sparse.spmatrix,
npt.NDArray[np.floating],
npt.NDArray[np.floating] | sparse.sparray,
npt.NDArray[np.floating] | sparse.spmatrix,
int]:
"""
Load a robust genetic selection problem into Python.
Expand Down Expand Up @@ -274,19 +274,19 @@ def load_problem(A_filename: str, E_filename: str, S_filename: str,
Returns
-------
ndarray or sparray
ndarray or spmatrix
Covariance matrix of candidates in the cohort.
ndarray
Vector of expected values of the expected breeding values of
candidates in the cohort.
ndarray or sparray
ndarray or spmatrix
Covariance matrix of expected breeding values of candidates in the
cohort.
int
Dimension of the problem.
"""

E = np.loadtxt(E_filename, dtype=float)
E = np.loadtxt(E_filename, dtype=np.floating)
# if dimension not specified, use `E` which doesn't need preallocation
if not dimension:
assert isinstance(E.size, int) # catches E being empty
Expand All @@ -307,7 +307,7 @@ def load_problem(A_filename: str, E_filename: str, S_filename: str,
A = makeA(load_ped(A_filename))
# HACK this loads the full matrix, then converts it down to sparse
if issparse:
A = sparse.coo_array(A)
A = sparse.coo_matrix(A)
else:
if issparse:
if not nnzA:
Expand Down
16 changes: 8 additions & 8 deletions robustocs/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def gurobi_standard_genetics(
sigma: npt.NDArray[np.floating] | sparse.sparray,
sigma: npt.NDArray[np.floating] | sparse.spmatrix,
mu: npt.NDArray[np.floating],
sires, # type could be np.ndarray, sets[ints], lists[int], range, etc
dams, # type could be np.ndarray, sets[ints], lists[int], range, etc
Expand Down Expand Up @@ -141,9 +141,9 @@ def gurobi_standard_genetics(


def gurobi_robust_genetics(
sigma: npt.NDArray[np.floating] | sparse.sparray,
sigma: npt.NDArray[np.floating] | sparse.spmatrix,
mubar: npt.NDArray[np.floating],
omega: npt.NDArray[np.floating] | sparse.sparray,
omega: npt.NDArray[np.floating] | sparse.spmatrix,
sires, # type could be np.ndarray, sets[ints], lists[int], range, etc
dams, # type could be np.ndarray, sets[ints], lists[int], range, etc
lam: float, # cannot be called `lambda`, that's reserved in Python
Expand Down Expand Up @@ -259,7 +259,7 @@ def gurobi_robust_genetics(
M[0, sires] = 1
M[1, dams] = 1
# define the right hand side of the constraint Mx = m
m = np.full(2, 0.5, dtype=float)
m = np.full(2, 0.5, dtype=np.floating)
model.addConstr(M@w == m, name="sum-to-half")

# conic constraint which comes from robust optimization
Expand All @@ -280,9 +280,9 @@ def gurobi_robust_genetics(


def gurobi_robust_genetics_sqp(
sigma: npt.NDArray[np.floating] | sparse.sparray,
sigma: npt.NDArray[np.floating] | sparse.spmatrix,
mubar: npt.NDArray[np.floating],
omega: npt.NDArray[np.floating] | sparse.sparray,
omega: npt.NDArray[np.floating] | sparse.spmatrix,
sires, # type could be np.ndarray, sets[ints], lists[int], range, etc
dams, # type could be np.ndarray, sets[ints], lists[int], range, etc
lam: float, # cannot be called `lambda`, that's reserved in Python
Expand Down Expand Up @@ -376,7 +376,7 @@ def gurobi_robust_genetics_sqp(
ndarray
Portfolio vector which Gurobi has determined is a solution.
float
Auxiliary variable corresponding to uncertainty associated with the
Auxillary variable corresponding to uncertainty associated with the
portfolio vector which Gurobi has determined is a solution.
float
Value of the objective function for returned solution vector.
Expand Down Expand Up @@ -453,7 +453,7 @@ def gurobi_robust_genetics_sqp(

def highs_bound_like(dimension: int,
value: float | list[float] | npt.NDArray[np.floating]
): # BUG -> npt.NDArray[np.floating] | list[float]
): # -> npt.NDArray[np.floating] | list[float] # BUG broke
"""
Helper function which allows HiGHS to interpret variable bounds specified
either as a vector or a single floating point value. If `value` is an array
Expand Down
4 changes: 2 additions & 2 deletions robustocs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def obj_string(name: str, value: float, precision: int,
def check_uncertainty_constraint(
z: float,
w: npt.NDArray[np.floating],
omega: npt.NDArray[np.floating] | sparse.sparray,
omega: npt.NDArray[np.floating] | sparse.spmatrix,
tol: float = 1e-7,
debug: bool = False
) -> bool:
Expand All @@ -123,7 +123,7 @@ def check_uncertainty_constraint(
Parameters
----------
z : float
Auxiliary variable from a solution to the robust selection problem.
Auxillary variable from a solution to the robust selection problem.
w : ndarray
Portfolio vector from a solution to the robust selection problem.
omega : ndarray
Expand Down

0 comments on commit 2800068

Please sign in to comment.