def test_reduce_equation(): print "\nTest reduce equation" c = [4, 3, 2, 1] A = [[2, 3, 4, 4], [1, 1, 2, 1]] b = [6, 3] c = np.array(c) A = np.array(A) b = np.array(b) print "Primal Problem" print linprog(c, A_eq=A, b_eq=b) row, col = A.shape c_s = np.concatenate((np.zeros(col), np.ones(row))) A_s = np.concatenate((A, np.eye(row)), axis=1) basis = range(col, col + row) opt = simplex_revised(c_s, A_s, b, basis, ret_lu=True) null_row, null_var = find_null_variable(opt.basis, A, opt.x_basis, lu_basis=opt.lu_basis) c_res, A_res, b_res, basis_res = reduce_equation(null_row, null_var, c, A, b, basis) print "\nReduce Problem" print "A\n%s\n%s" % (str(A), str(A_res)) print "b\n%s\n%s" % (str(b), str(b_res)) print "c\n%s\n%s" % (str(c), str(c_res)) print simplex_revised(c_res, A_res, b_res, basis_res)
def init_basis_primal(A, b, **argv): """ Solve Artificial Linear Programming min 1*s s.t s + A*x = b, s, x >= 0 Input: A: equation constraint b: equation constraint eps: tolerance max_iter: max number of iteration Return: success: (basis, x, lambda) fail: -1: invalid -2: infeasible, the minimum is not zero """ eps = argv.get("eps", 1e-10) row, col = A.shape cp = np.concatenate((np.zeros(col), np.ones(row))) Ap = np.concatenate((A, np.eye(row)), axis=1) basis = range(col, col + row) ret = simplex_revised(cp, Ap, b, basis, ret_lu=True) if type(ret) == int: sys.stderr.write("Problem invalid\n") return -1 if not is_zero(ret.z_opt, eps): sys.stderr.write("Problem infeasible\n") return -2 return ret
def test_sudoku_p0(): print "\nTest Sudoku p0" sd2 = Sudoku() idx = [1, 3, 6, 8, 9, 11, 14, 16] val = [1, 3, 4, 2, 4, 2, 1, 3] idx = idx[:0] val = val[:0] mat_set = sd2.make_sudoku_matrix(pre_set=(idx, val)) row, col = mat_set.shape print "size %s %s" % (row, col) b = np.ones(row) c = np.zeros(col) A = mat_set.toarray() ## scipy linprog cur = time.time() ret = optimize.linprog(c, A_eq=A, b_eq=b) print "\nScipy time cost %5s" % (time.time() - cur) mat = sd2.vec2mat(ret.x) print ret print "Sudoku Matrix\n%s" % str(mat) ## simplex LU num_slack = row c = np.concatenate((c, np.ones(row))) A = np.concatenate((A, np.eye(row)), axis=1) basis = range(col, col+row) cur = time.time() opt = simplex_revised(c, A, b, basis, eps=1e-10, max_iter=1000) print "\nSimplexLU time cost %5s" % (time.time() - cur) mat = sd2.vec2mat(opt.x_opt) print opt print "Sudoku Matrix\n%s" % str(mat)
def linprog_primal(c, A, b, **argv): """ Solve Linear Programming in standard form min c*x s.t A*x = b, x >= 0 Input: c: object vector A: equation constraint b: equation constraint eps: tolerance max_iter: max number of iteration Return: success: (basis, x, lambda) fail: -1: illegal -2: unbounded -3: infeasible """ # Init eps = argv.get("eps", 1e-16) debug = argv.get("debug", False) is_neg = lambda x: x < -eps # size row, col = A.shape if debug: print "\nProblem size row %s col %s" % (row, col) # Make sure b >= 0 for i in range(row): if is_neg(b[i]): b[i] = -b[i] A[i] = -A[i] # Init basic solution ret0 = init_basis_primal(A, b) if type(ret0) == int: sys.stderr.write("Problem infeasible\n") return -3 basis = ret0.basis x0 = ret0.x_basis if debug: print "\nBasic Problem solved" print "basis\t%s" % str(basis) print "x0\t%s" % str(x0) null_row, null_var = find_null_variable(basis, A, x0, lu_basis=ret0.lu_basis) if len(null_row) != 0: sys.stderr.write("Reduce enable null_row %s null_var %s\n" % (str(null_row), str(null_var))) c, A, b, basis = reduce_equation(null_row, null_var, c, A, b, basis) check_basis_slack(basis, A) # Solve LP opt = simplex_revised(c, A, b, basis, debug=debug) if type(opt) == int: sys.stderr.write("Problem unsolved\n") return opt if debug: print "\nPrimal Problem solved" print "z_opt\t%s" % opt.z_opt print "x_opt\t%s" % str(opt.x_opt) return opt
def test_revised(): c = [-3, -1, -3] b = [2, 5, 6] A = [[2, 1, 1], [1, 2, 3], [2, 2, 1]] basis = [3, 4, 5] c = np.array(c) A = np.array(A) b = np.array(b) print "\nTest linprog" print linprog(c, A_ub=A, b_ub=b) basis = np.array(basis) num_slack = 3 c = np.concatenate((c, np.zeros(num_slack))) A = np.concatenate((A, np.eye(num_slack)), axis=1) print "\nTest Revised Simplex" print simplex_revised(c, A, b, basis, debug=True) print "\nTest Two Phrase Method" print linprog_primal(c, A, b, debug=True)
def solve_sub(self, lmbd, lmbd_i, max_upper=1e16, debug=False): c_sub = self.c - self.L.T.dot(lmbd) if debug: print "sub c\t%s" % str(c_sub) print "sub intercept\t%s" % -lmbd_i ret = simplex_revised(c_sub, self.A, self.b, self.basis) if type(ret) == int: return max_upper else: basis = ret.basis x_b = ret.x_opt self.basis = basis self.x_b = x_b z_opt = np.dot(c_sub, x_b) - lmbd_i return z_opt
def test_sudoku_p1(): print "\nTest Sudoku p0" sdk = Sudoku(3) rows = "11 22222 33333 444 5555 666 77777 88888 99" cols = "59 24678 34679 379 2468 137 13467 23468 15" vals = "59 89461 42685 897 4173 251 91437 27598 81" rows = str2int(rows) cols = str2int(cols) vals = str2int(vals) print "Sudoku preset" row_sp = np.array(rows) - 1 col_sp = np.array(cols) - 1 print sparse.csr_matrix((vals, (row_sp, col_sp))).toarray() #mat_set = sdk.make_sudoku_matrix(pre_set=(rows, cols, vals)) mat_set = sdk.make_sudoku_matrix() row, col = mat_set.shape print "size %s %s" % (row, col) b = np.ones(row) c = np.zeros(col) A = mat_set.toarray() ## scipy linprog cfg = {"maxiter": 10000} cur = time.time() ret = optimize.linprog(c, A_eq=A, b_eq=b, options=cfg) print "\nScipy time cost %5s" % (time.time() - cur) print ret if ret.success: mat = sdk.vec2mat(ret.x) print "Sudoku Matrix\n%s" % str(mat) ## simplex LU num_slack = row c_s = np.concatenate((c, np.ones(row))) A_s = np.concatenate((A, np.eye(row)), axis=1) basis = range(col, col+row) cur = time.time() opt = simplex_revised(c_s, A_s, b, basis, eps=1e-10, max_iter=10000, ret_lu=True) print "\nSimplexLU time cost %5s" % (time.time() - cur) print opt if opt.x_opt is not None: mat = sdk.vec2mat(opt.x_opt[:col]) print "x_opt sum %s integer %s" % (opt.x_opt.sum(), is_integer_list(opt.x_opt, 1e-6)) print "Sudoku Matrix\n%s" % str(mat) int_idx = range(col)