|
| 1 | +from .tupledict import tupledict |
| 2 | +from .core_ext import ScalarAffineFunction |
| 3 | + |
| 4 | + |
| 5 | +def iterate_sparse_matrix_rows(A): |
| 6 | + """ |
| 7 | + Iterate over rows of a sparse matrix and get non-zero elements for each row. |
| 8 | +
|
| 9 | + A is a 2-dimensional scipy sparse matrix |
| 10 | + isinstance(A, scipy.sparse.sparray) = True and A.ndim = 2 |
| 11 | + """ |
| 12 | + from scipy.sparse import csr_array |
| 13 | + |
| 14 | + if not isinstance(A, csr_array): |
| 15 | + A = csr_array(A) # Convert to CSR format if not already |
| 16 | + |
| 17 | + for i in range(A.shape[0]): |
| 18 | + row_start = A.indptr[i] |
| 19 | + row_end = A.indptr[i + 1] |
| 20 | + row_indices = A.indices[row_start:row_end] |
| 21 | + row_data = A.data[row_start:row_end] |
| 22 | + yield row_indices, row_data |
| 23 | + |
| 24 | + |
| 25 | +def add_matrix_constraints(model, A, x, sense, b): |
| 26 | + """ |
| 27 | + add constraints Ax <= / = / >= b |
| 28 | +
|
| 29 | + A is a 2-dimensional numpy array or scipy sparse matrix |
| 30 | + x is an iterable of variables |
| 31 | + sense is one of (poi.Leq, poi.Eq, poi.Geq) |
| 32 | + b is an iterable of values or a single scalar |
| 33 | + """ |
| 34 | + import numpy as np |
| 35 | + from scipy.sparse import sparray |
| 36 | + |
| 37 | + is_ndarray = isinstance(A, np.ndarray) |
| 38 | + is_sparse = isinstance(A, sparray) |
| 39 | + |
| 40 | + if not is_ndarray and not is_sparse: |
| 41 | + raise ValueError("A must be a numpy array or scipy.sparse array") |
| 42 | + |
| 43 | + ndim = A.ndim |
| 44 | + if ndim != 2: |
| 45 | + raise ValueError("A must be a 2-dimensional array") |
| 46 | + |
| 47 | + M, N = A.shape |
| 48 | + |
| 49 | + # turn x into a list if x is an iterable |
| 50 | + if isinstance(x, np.ndarray): |
| 51 | + xdim = x.ndim |
| 52 | + if xdim != 1: |
| 53 | + raise ValueError("x must be a 1-dimensional array") |
| 54 | + elif isinstance(x, tupledict): |
| 55 | + x = list(x.values()) |
| 56 | + else: |
| 57 | + x = list(x) |
| 58 | + |
| 59 | + if len(x) != N: |
| 60 | + raise ValueError("x must have length equal to the number of columns of A") |
| 61 | + |
| 62 | + # check b |
| 63 | + if np.isscalar(b): |
| 64 | + b = np.full(M, b) |
| 65 | + elif len(b) != M: |
| 66 | + raise ValueError("b must have length equal to the number of rows of A") |
| 67 | + |
| 68 | + constraints = np.empty(M, dtype=object) |
| 69 | + |
| 70 | + if is_ndarray: |
| 71 | + for i in range(M): |
| 72 | + expr = ScalarAffineFunction() |
| 73 | + row = A[i] |
| 74 | + for coef, var in zip(row, x): |
| 75 | + expr.add_term(var, coef) |
| 76 | + con = model.add_linear_constraint(expr, sense, b[i]) |
| 77 | + constraints[i] = con |
| 78 | + elif is_sparse: |
| 79 | + for i, (row_indices, row_data), rhs in zip( |
| 80 | + range(M), iterate_sparse_matrix_rows(A), b |
| 81 | + ): |
| 82 | + expr = ScalarAffineFunction() |
| 83 | + for j, coef in zip(row_indices, row_data): |
| 84 | + expr.add_term(x[j], coef) |
| 85 | + con = model.add_linear_constraint(expr, sense, rhs) |
| 86 | + constraints[i] = con |
| 87 | + |
| 88 | + return constraints |
0 commit comments