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__clean_inputs2(): c = 1 A_ub = [[1]] b_ub = 1 A_eq = [[1]] b_eq = 1 bounds = (0, 1) outputs = _clean_inputs( c=c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds) assert_allclose(outputs[0], np.array(c)) assert_allclose(outputs[1], np.array(A_ub)) assert_allclose(outputs[2], np.array(b_ub)) assert_allclose(outputs[3], np.array(A_eq)) assert_allclose(outputs[4], np.array(b_eq)) assert_(outputs[5] == [(0, 1)], "") assert_(outputs[0].shape == (1,), "") assert_(outputs[1].shape == (1, 1), "") assert_(outputs[2].shape == (1,), "") assert_(outputs[3].shape == (1, 1), "") assert_(outputs[4].shape == (1,), "")
def test__clean_inputs1(): 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 outputs = _clean_inputs( c=c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds) assert_allclose(outputs[0], np.array(c)) assert_allclose(outputs[1], np.array(A_ub)) assert_allclose(outputs[2], np.array(b_ub)) assert_allclose(outputs[3], np.array(A_eq)) assert_allclose(outputs[4], np.array(b_eq)) assert_(outputs[5] == [(0, None)] * 2, "") assert_(outputs[0].shape == (2,), "") assert_(outputs[1].shape == (2, 2), "") assert_(outputs[2].shape == (2,), "") assert_(outputs[3].shape == (2, 2), "") assert_(outputs[4].shape == (2,), "")
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_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_aliasing(): c = 1 A_ub = [[1]] b_ub = [1] A_eq = [[1]] b_eq = [1] bounds = (-np.inf, np.inf) c_copy = deepcopy(c) A_ub_copy = deepcopy(A_ub) b_ub_copy = deepcopy(b_ub) A_eq_copy = deepcopy(A_eq) b_eq_copy = deepcopy(b_eq) bounds_copy = deepcopy(bounds) _clean_inputs(c, A_ub, b_ub, A_eq, b_eq, bounds) assert_(c == c_copy, "c modified by _clean_inputs") assert_(A_ub == A_ub_copy, "A_ub modified by _clean_inputs") assert_(b_ub == b_ub_copy, "b_ub modified by _clean_inputs") assert_(A_eq == A_eq_copy, "A_eq modified by _clean_inputs") assert_(b_eq == b_eq_copy, "b_eq modified by _clean_inputs") assert_(bounds == bounds_copy, "bounds modified by _clean_inputs")
def test_aliasing2(): 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)] c_copy = c.copy() A_ub_copy = A_ub.copy() b_ub_copy = b_ub.copy() A_eq_copy = A_eq.copy() b_eq_copy = b_eq.copy() bounds_copy = deepcopy(bounds) _clean_inputs(c, A_ub, b_ub, A_eq, b_eq, bounds) assert_allclose(c, c_copy, err_msg="c modified by _clean_inputs") assert_allclose(A_ub, A_ub_copy, err_msg="A_ub modified by _clean_inputs") assert_allclose(b_ub, b_ub_copy, err_msg="b_ub modified by _clean_inputs") assert_allclose(A_eq, A_eq_copy, err_msg="A_eq modified by _clean_inputs") assert_allclose(b_eq, b_eq_copy, err_msg="b_eq modified by _clean_inputs") assert_(bounds == bounds_copy, "bounds modified by _clean_inputs")
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__clean_inputs3(): 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)] outputs = _clean_inputs(c=c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds) assert_allclose(outputs[0], np.array([1, 2])) assert_allclose(outputs[2], np.array([1, 2])) assert_allclose(outputs[4], np.array([1, 2])) assert_(outputs[5] == [(0, 1)] * 2, "") assert_(outputs[0].shape == (2, ), "") assert_(outputs[2].shape == (2, ), "") assert_(outputs[4].shape == (2, ), "")
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 test__clean_inputs3(): 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)] outputs = _clean_inputs( c=c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds) assert_allclose(outputs[0], np.array([1, 2])) assert_allclose(outputs[2], np.array([1, 2])) assert_allclose(outputs[4], np.array([1, 2])) assert_(outputs[5] == [(0, 1)] * 2, "") assert_(outputs[0].shape == (2,), "") assert_(outputs[2].shape == (2,), "") assert_(outputs[4].shape == (2,), "")
def test_good_bounds(): c = [1, 2] outputs = _clean_inputs(c=c, bounds=None) assert_(outputs[5] == [(0, None)] * 2, "") outputs = _clean_inputs(c=c, bounds=(1, 2)) assert_(outputs[5] == [(1, 2)] * 2, "") outputs = _clean_inputs(c=c, bounds=[(1, 2)]) assert_(outputs[5] == [(1, 2)] * 2, "") outputs = _clean_inputs(c=c, bounds=[(1, np.inf)]) assert_(outputs[5] == [(1, None)] * 2, "") outputs = _clean_inputs(c=c, bounds=[(-np.inf, 1)]) assert_(outputs[5] == [(None, 1)] * 2, "") outputs = _clean_inputs(c=c, bounds=[(-np.inf, np.inf), (-np.inf, np.inf)]) assert_(outputs[5] == [(None, None)] * 2, "")
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 _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
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)