def test_too_few_dimensions(): bad = np.random.rand(4, 4).ravel() cb = np.random.rand(4) assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_ub=bad, b_ub=cb)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_eq=bad, b_eq=cb))
def test_too_many_dimensions(): cb = [1, 2, 3, 4] A = np.random.rand(4, 4) bad2D = [[1, 2], [3, 4]] bad3D = np.random.rand(4, 4, 4) assert_raises(ValueError, _clean_inputs, _LPProblem(c=bad2D, A_ub=A, b_ub=cb)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_ub=bad3D, b_ub=cb)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_ub=A, b_ub=bad2D)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_eq=bad3D, b_eq=cb)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_eq=A, b_eq=bad2D))
def test_good_bounds(): lp = _LPProblem(c=[1, 2]) lp_cleaned = _clean_inputs(lp) # lp.bounds is None by default assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2) lp_cleaned = _clean_inputs(lp._replace(bounds=[])) assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2) lp_cleaned = _clean_inputs(lp._replace(bounds=[[]])) assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2) lp_cleaned = _clean_inputs(lp._replace(bounds=(1, 2))) assert_equal(lp_cleaned.bounds, [(1, 2)] * 2) lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, 2)])) assert_equal(lp_cleaned.bounds, [(1, 2)] * 2) lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, None)])) assert_equal(lp_cleaned.bounds, [(1, np.inf)] * 2) lp_cleaned = _clean_inputs(lp._replace(bounds=[(None, 1)])) assert_equal(lp_cleaned.bounds, [(-np.inf, 1)] * 2) lp_cleaned = _clean_inputs( lp._replace(bounds=[(None, None), (-np.inf, None)])) assert_equal(lp_cleaned.bounds, [(-np.inf, np.inf)] * 2) lp = _LPProblem(c=[1, 2, 3, 4]) lp_cleaned = _clean_inputs(lp) # lp.bounds is None by default assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 4) lp_cleaned = _clean_inputs(lp._replace(bounds=(1, 2))) assert_equal(lp_cleaned.bounds, [(1, 2)] * 4) lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, 2)])) assert_equal(lp_cleaned.bounds, [(1, 2)] * 4) lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, None)])) assert_equal(lp_cleaned.bounds, [(1, np.inf)] * 4) lp_cleaned = _clean_inputs(lp._replace(bounds=[(None, 1)])) assert_equal(lp_cleaned.bounds, [(-np.inf, 1)] * 4) lp_cleaned = _clean_inputs( lp._replace(bounds=[(None, None), (-np.inf, None), (None, np.inf), (-np.inf, np.inf)])) assert_equal(lp_cleaned.bounds, [(-np.inf, np.inf)] * 4)
def test_bad_bounds(): lp = _LPProblem(c=[1, 2]) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=(1, 2, 2))) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2, 2)])) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2), (1, 2, 2)])) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2), (1, 2), (1, 2)])) lp = _LPProblem(c=[1, 2, 3, 4]) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2, 3, 4), (1, 2, 3, 4)]))
def test_inconsistent_dimensions(): m = 2 n = 4 c = [1, 2, 3, 4] Agood = np.random.rand(m, n) Abad = np.random.rand(m, n + 1) bgood = np.random.rand(m) bbad = np.random.rand(m + 1) boundsbad = [(0, 1)] * (n + 1) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=Abad, b_ub=bgood)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=Agood, b_ub=bbad)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=Abad, b_eq=bgood)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=Agood, b_eq=bbad)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, bounds=boundsbad))
def setup(self, meth, prob): if prob not in enabled_rr_problems: raise NotImplementedError("skipped") if (meth.__name__, prob) in self.known_fails: raise NotImplementedError("Known issues with these benchmarks.") dir_path = os.path.dirname(os.path.realpath(__file__)) datafile = os.path.join(dir_path, "linprog_benchmark_files", prob + ".npz") data = np.load(datafile, allow_pickle=True) c, A_eq, A_ub, b_ub, b_eq = (data["c"], data["A_eq"], data["A_ub"], data["b_ub"], data["b_eq"]) bounds = np.squeeze(data["bounds"]) x0 = np.zeros(c.shape) lp = _LPProblem(c, A_ub, b_ub, A_eq, b_eq, bounds, x0) lp_cleaned = _clean_inputs(lp) # rr_method is None here because we're not using RR res = _presolve(lp_cleaned, rr=False, rr_method=None, tol=1e-9)[0] self.A_eq, self.b_eq = res.A_eq, res.b_eq self.true_rank = np.linalg.matrix_rank(self.A_eq) if meth == _remove_redundancy_pivot_sparse: self.A_eq = csc_matrix(self.A_eq) self.rr_A = None
def test_type_errors(): lp = _LPProblem(c=[1, 2], A_ub=np.array([[1, 1], [2, 2]]), b_ub=np.array([1, 1]), A_eq=np.array([[1, 1], [2, 2]]), b_eq=np.array([1, 1]), bounds=[(0, 1)]) bad = "hello" assert_raises(TypeError, _clean_inputs, lp._replace(c=bad)) assert_raises(TypeError, _clean_inputs, lp._replace(A_ub=bad)) assert_raises(TypeError, _clean_inputs, lp._replace(b_ub=bad)) assert_raises(TypeError, _clean_inputs, lp._replace(A_eq=bad)) assert_raises(TypeError, _clean_inputs, lp._replace(b_eq=bad)) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=bad)) assert_raises(ValueError, _clean_inputs, lp._replace(bounds="hi")) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=["hi"])) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[("hi")])) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, "")])) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2), (1, "")])) assert_raises(TypeError, _clean_inputs, lp._replace(bounds=[(1, date(2020, 2, 29))])) assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[[[1, 2]]]))
def test_aliasing2(): """ Similar purpose as `test_aliasing` above. """ lp = _LPProblem(c=np.array([1, 1]), A_ub=np.array([[1, 1], [2, 2]]), b_ub=np.array([[1], [1]]), A_eq=np.array([[1, 1]]), b_eq=np.array([1]), bounds=[(-np.inf, np.inf), (None, 1)]) lp_copy = deepcopy(lp) _clean_inputs(lp) assert_allclose(lp.c, lp_copy.c, err_msg="c modified by _clean_inputs") assert_allclose(lp.A_ub, lp_copy.A_ub, err_msg="A_ub modified by _clean_inputs") assert_allclose(lp.b_ub, lp_copy.b_ub, err_msg="b_ub modified by _clean_inputs") assert_allclose(lp.A_eq, lp_copy.A_eq, err_msg="A_eq modified by _clean_inputs") assert_allclose(lp.b_eq, lp_copy.b_eq, err_msg="b_eq modified by _clean_inputs") assert_(lp.bounds == lp_copy.bounds, "bounds modified by _clean_inputs")
def test_missing_inputs(): c = [1, 2] A_ub = np.array([[1, 1], [2, 2]]) b_ub = np.array([1, 1]) A_eq = np.array([[1, 1], [2, 2]]) b_eq = np.array([1, 1]) assert_raises(TypeError, _clean_inputs) assert_raises(TypeError, _clean_inputs, _LPProblem(c=None)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=A_ub)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=A_ub, b_ub=None)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, b_ub=b_ub)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=None, b_ub=b_ub)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=A_eq)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=A_eq, b_eq=None)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, b_eq=b_eq)) assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=None, b_eq=b_eq))
def test__clean_inputs2(): lp = _LPProblem(c=1, A_ub=[[1]], b_ub=1, A_eq=[[1]], b_eq=1, bounds=(0, 1)) lp_cleaned = _clean_inputs(lp) assert_allclose(lp_cleaned.c, np.array(lp.c)) assert_allclose(lp_cleaned.A_ub, np.array(lp.A_ub)) assert_allclose(lp_cleaned.b_ub, np.array(lp.b_ub)) assert_allclose(lp_cleaned.A_eq, np.array(lp.A_eq)) assert_allclose(lp_cleaned.b_eq, np.array(lp.b_eq)) assert_(lp_cleaned.bounds == [(0, 1)], "") assert_(lp_cleaned.c.shape == (1, ), "") assert_(lp_cleaned.A_ub.shape == (1, 1), "") assert_(lp_cleaned.b_ub.shape == (1, ), "") assert_(lp_cleaned.A_eq.shape == (1, 1), "") assert_(lp_cleaned.b_eq.shape == (1, ), "")
def test__clean_inputs3(): lp = _LPProblem(c=[[1, 2]], A_ub=np.random.rand(2, 2), b_ub=[[1], [2]], A_eq=np.random.rand(2, 2), b_eq=[[1], [2]], bounds=[(0, 1)]) lp_cleaned = _clean_inputs(lp) assert_allclose(lp_cleaned.c, np.array([1, 2])) assert_allclose(lp_cleaned.b_ub, np.array([1, 2])) assert_allclose(lp_cleaned.b_eq, np.array([1, 2])) assert_(lp_cleaned.bounds == [(0, 1)] * 2, "") assert_(lp_cleaned.c.shape == (2, ), "") assert_(lp_cleaned.b_ub.shape == (2, ), "") assert_(lp_cleaned.b_eq.shape == (2, ), "")
def setup(self, meth, prob): dir_path = os.path.dirname(os.path.realpath(__file__)) datafile = os.path.join(dir_path, "linprog_benchmark_files", prob + ".npz") data = np.load(datafile, allow_pickle=True) c, A_eq, A_ub, b_ub, b_eq = (data["c"], data["A_eq"], data["A_ub"], data["b_ub"], data["b_eq"]) bounds = np.squeeze(data["bounds"]) x0 = np.zeros(c.shape) if meth == "sparse": A_eq = csr_matrix(A_eq) A_ub = csr_matrix(A_ub) lp = _LPProblem(c, A_ub, b_ub, A_eq, b_eq, bounds, x0) self.lp_cleaned = _clean_inputs(lp)
def test_non_finite_errors(): lp = _LPProblem( c=[1, 2], A_ub=np.array([[1, 1], [2, 2]]), b_ub=np.array([1, 1]), A_eq=np.array([[1, 1], [2, 2]]), b_eq=np.array([1, 1]), bounds=[(0, 1)] ) assert_raises(ValueError, _clean_inputs, lp._replace(c=[0, None])) assert_raises(ValueError, _clean_inputs, lp._replace(c=[np.inf, 0])) assert_raises(ValueError, _clean_inputs, lp._replace(c=[0, -np.inf])) assert_raises(ValueError, _clean_inputs, lp._replace(c=[np.nan, 0])) assert_raises(ValueError, _clean_inputs, lp._replace(A_ub=[[1, 2], [None, 1]])) assert_raises(ValueError, _clean_inputs, lp._replace(b_ub=[np.inf, 1])) assert_raises(ValueError, _clean_inputs, lp._replace(A_eq=[[1, 2], [1, -np.inf]])) assert_raises(ValueError, _clean_inputs, lp._replace(b_eq=[1, np.nan]))
def test_good_bounds(): lp = _LPProblem(c=[1, 2]) lp_cleaned = _clean_inputs(lp) # lp.bounds is None by default assert_(lp_cleaned.bounds == [(0, None)] * 2, "") lp_cleaned = _clean_inputs(lp._replace(bounds=(1, 2))) assert_(lp_cleaned.bounds == [(1, 2)] * 2, "") lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, 2)])) assert_(lp_cleaned.bounds == [(1, 2)] * 2, "") lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, np.inf)])) assert_(lp_cleaned.bounds == [(1, None)] * 2, "") lp_cleaned = _clean_inputs(lp._replace(bounds=[(-np.inf, 1)])) assert_(lp_cleaned.bounds == [(None, 1)] * 2, "") lp_cleaned = _clean_inputs(lp._replace(bounds=[(-np.inf, np.inf), (-np.inf, np.inf)])) assert_(lp_cleaned.bounds == [(None, None)] * 2, "")
def test_aliasing(): """ Test for ensuring that no objects referred to by `lp` attributes, `c`, `A_ub`, `b_ub`, `A_eq`, `b_eq`, `bounds`, have been modified by `_clean_inputs` as a side effect. """ lp = _LPProblem(c=1, A_ub=[[1]], b_ub=[1], A_eq=[[1]], b_eq=[1], bounds=(-np.inf, np.inf)) lp_copy = deepcopy(lp) _clean_inputs(lp) assert_(lp.c == lp_copy.c, "c modified by _clean_inputs") assert_(lp.A_ub == lp_copy.A_ub, "A_ub modified by _clean_inputs") assert_(lp.b_ub == lp_copy.b_ub, "b_ub modified by _clean_inputs") assert_(lp.A_eq == lp_copy.A_eq, "A_eq modified by _clean_inputs") assert_(lp.b_eq == lp_copy.b_eq, "b_eq modified by _clean_inputs") assert_(lp.bounds == lp_copy.bounds, "bounds modified by _clean_inputs")
def test__clean_inputs1(): lp = _LPProblem(c=[1, 2], A_ub=[[1, 1], [2, 2]], b_ub=[1, 1], A_eq=[[1, 1], [2, 2]], b_eq=[1, 1], bounds=None) lp_cleaned = _clean_inputs(lp) assert_allclose(lp_cleaned.c, np.array(lp.c)) assert_allclose(lp_cleaned.A_ub, np.array(lp.A_ub)) assert_allclose(lp_cleaned.b_ub, np.array(lp.b_ub)) assert_allclose(lp_cleaned.A_eq, np.array(lp.A_eq)) assert_allclose(lp_cleaned.b_eq, np.array(lp.b_eq)) assert_(lp_cleaned.bounds == [(0, None)] * 2, "") assert_(lp_cleaned.c.shape == (2, ), "") assert_(lp_cleaned.A_ub.shape == (2, 2), "") assert_(lp_cleaned.b_ub.shape == (2, ), "") assert_(lp_cleaned.A_eq.shape == (2, 2), "") assert_(lp_cleaned.b_eq.shape == (2, ), "")
def _fill_prob(c, A_ub, b_ub, A_eq, b_eq, bounds, sense, prob_name): '''Create and populate GLPK prob struct from linprog definition.''' # Housekeeping lp = _clean_inputs(_LPProblem(c, A_ub, b_ub, A_eq, b_eq, bounds, None)) c, A_ub, b_ub, A_eq, b_eq, processed_bounds, _x0 = lp # coo for (i, j, val) format A = coo_matrix(np.concatenate((A_ub, A_eq), axis=0)) # Convert linprog-style bounds to GLPK-style bounds bounds = _convert_bounds(processed_bounds) # Get the library _lib = GLPK()._lib # Create problem instance prob = _lib.glp_create_prob() # Give problem a name _lib.glp_set_prob_name(prob, prob_name.encode()) # Set objective name _lib.glp_set_obj_name(prob, b'obj-name') # Set objective sense _lib.glp_set_obj_dir(prob, sense) # Set objective coefficients and column bounds first_col = _lib.glp_add_cols(prob, len(c)) for ii, (c0, bnd) in enumerate(zip(c, bounds)): _lib.glp_set_obj_coef(prob, ii + first_col, c0) _lib.glp_set_col_name(prob, ii + first_col, b'c%d' % ii) # name is c[idx], idx is 0-based index if bnd is not None: _lib.glp_set_col_bnds(prob, ii + first_col, bnd[0], bnd[1], bnd[2]) # else: default is GLP_FX with lb=0, ub=0 # Need to load both matrices at the same time first_row = _lib.glp_add_rows(prob, A.shape[0]) # prepend an element and make 1-based index # b/c GLPK expects indices starting at 1 nnz = A.nnz rows = np.concatenate(([-1], A.row + first_row)).astype(ctypes.c_int) cols = np.concatenate(([-1], A.col + first_col)).astype(ctypes.c_int) values = np.concatenate(([0], A.data)).astype(ctypes.c_double) _lib.glp_load_matrix( prob, nnz, rows, cols, values, ) # Set row bounds # Upper bounds (b_ub): for ii, b0 in enumerate(b_ub): # lb is ignored for upper bounds _lib.glp_set_row_bnds(prob, ii + first_row, GLPK.GLP_UP, 0, b0) # Equalities (b_eq) for ii, b0 in enumerate(b_eq): _lib.glp_set_row_bnds(prob, ii + first_row + len(b_ub), GLPK.GLP_FX, b0, b0) return prob, lp