def solve_ilp(dictionary, indices): initialized = initialize(dictionary, np.copy(dictionary[-2, 1:]), np.copy(dictionary[-1, 1:])) if initialized is None: return 'INFEASIBLE' else: solved = solve(initialized) if solved is None: return 'UNBOUNDED' elif not is_ilp_final(solved, indices): return solve_ilp(add_cutting_planes(solved), indices) else: return solved
def main(): new_file = open(argv[1], "r") model, n_rows, n_cols = simplex.read_model(new_file) c, A, b, non_basic, basic = simplex.create_model(model, n_rows, n_cols) if (b < 0).any(): print("\nThere are negative elements in the b vector.\nI'm sorry, I can't solve this yet :-(\n") return z, w, xB, basic, status = simplex.solve(A, b, c, basic, non_basic) print("obj:", z) print("basic variables", basic, "with values:", xB) print("status:", status)
def test(A, b, c, m, n): Aup = A[:m, :n] cup = c[0, :n] res = opt.linprog(-1 * cup, Aup, b) c = np.append(c, 0) optV, vec, num_pivots = s.solve(A, b, c, 'max', default_method, False) if not np.allclose(optV, -1 * res['fun']): print opt, vec, num_pivots print 'NOT THE SAME AS SCIPY' exit(1) return np.allclose(optV, -1 * res['fun']), optV, num_pivots
def solve_ilp(dictionary, indices): initialized = initialize(dictionary, np.copy(dictionary[-2, 1:]), np.copy(dictionary[-1, 1:])) if initialized is None: return 'INFEASIBLE' else: solved = solve(initialized) if solved is None: return 'UNBOUNDED' elif np.array_equal(dictionary, solved): return 'INFEASIBLE' elif not is_ilp_final(solved, indices): return solve_ilp(add_cutting_planes(solved), indices) else: return solved
import simplex from test_data import problems from numpy import matrix simplex.enable_formatting() for problem in problems: print("Problem:") print("c =", problem["c"]) print(matrix(problem["A"])) print("b =", problem["b"]) expected_result = problem.pop("expected_result") result = simplex.solve(**problem) assert all((result - expected_result) < 0.01), f"Result should be {expected_result}" print("Result is correct!\n\n")
from sys import argv from graph import Graph from simplex import solve if __name__ == "__main__": graph = Graph(argv[1]) # Set initial feasible solution graph.base_size = 3 graph.set_flow(from_id=1, to_id=2, value=4) graph.set_flow(from_id=2, to_id=3, value=6) graph.set_flow(from_id=3, to_id=4, value=5) solve(graph) print(graph)
for i in ans[2]: print(i, end=' ') elif (len(ans) == 4): print("ilimitada") for i in ans[0][n:n + m]: print(i, end=' ') print() for i in ans[1][n:n + m]: print(i, end=' ') print() else: firstBase = [[i, i + m] for i in range(n)] ans = solve(A, B, C, firstBase) if (len(ans) == 5): print("otima") print(ans[0]) for i in ans[1][:m]: print(i, end=' ') print() for i in ans[2]: print(i, end=' ') elif (len(ans) == 4): print("ilimitada") for i in ans[0]: print(i, end=' ') print() for i in ans[1]:
def process(msg, url): try: text = msg['message']['text'] text = text.split('\n') print(text) print("chat_id = {}".format(msg['message']['chat']['id'])) st = 0 minim = True if not text[0][0].isdigit() and not text[0][0] == '-': minim = False st = 1 c = [int(x) for x in text[st+0].split()] b = [int(x) for x in text[st+1].split()] A_simple = [int(x) for x in text[st+2].split()] A = [] elem_num = 0 for i in range(len(b)): A.append([]) for j in range(len(c)): A[i].append(A_simple[elem_num]) elem_num += 1 tosend = {} tosend['chat_id'] = msg['message']['chat']['id'] task = str(c) + '\n' + str(b) + '\n' + str(A) + '\n' tosend['text'] = task requests.post(url + 'sendMessage', params = tosend) tosend['text'] = simplex.solve(A,b,c, minim) requests.post(url + 'sendMessage', params = tosend) except: print("Message processing failed.") try: tosend = {} tosend['chat_id'] = msg['message']['chat']['id'] user_manual_string = "User Manual:\n" +\ "First line - vector с.\n" +\ "Second line - vector b.\n" +\ "Third line - matrix A (all rows in the same line).\n" +\ "If you want maximization instead of minimization, write any word before first line.\n" +\ "All information should go in one message!\n" +\ "If parsing fails you will receive this message again.\n\n" +\ "Example of maximization:\n" +\ "max\n" +\ "4 -3 1 -1\n" +\ "4 5 6\n" +\ "3 2 1 0 1 0 2 1 2 1 0 2\n\n" +\ "Example of minimization:\n" +\ "4 -3 1 -1\n" +\ "4 5 6\n" +\ "3 2 1 0 1 0 2 1 2 1 0 2\n" tosend['text'] = user_manual_string requests.post(url + 'sendMessage', params = tosend) print("Sended manual for user.") except: print("Sending manual for user failed.") return
A1 = np.concatenate((A1, np.eye(n), -1 * np.eye(n)), axis=1) A2 = np.concatenate((A2, -1 * np.eye(n), np.eye(n)), axis=1) A3 = np.ones((1, (1 + len(active_set)))) A3 = np.concatenate((A3, np.zeros((1, 2 * n))), axis=1) A4 = -1 * A3 A = np.concatenate((A1, A2, A3, A4), axis=0) c = np.concatenate((np.zeros((1, 1 + len(active_set))), np.ones( (1, 2 * n))), axis=1) final_matrix_1 = np.concatenate((A, c), axis=0) final_matrix_2 = np.zeros((2 * n + 3, 1)) final_matrix_2[-2] = -1 final_matrix_2[-3] = 1 A = np.concatenate((final_matrix_1, final_matrix_2), axis=1) x, y, z = simplex.solve(A, n) d = y[0:n] - y[n:2 * n] print('X:', X_list[-1]) print('active_set', active_set) print(A) print('d', d, 'z', z) if (abs(z) <= 1e-3): break lambda_max = 10 while (lambda_max >= 0): temp = g_list(current + lambda_max * d) if (np.sum(temp > 0) == 0): break