import numpy as np from numpy.linalg import inv import sys def f(x): return 2 * x[0] + 3 * x[1] - 4 * x[2] def simplex(A0, b0, c0): n = 4 m = 2 shape = A0.shape B = A0[:, 0:m] N = A0[:, m:n] cB = c0[:, 0:m] cN = c0[:, m:n] x = np.zeros((1, n)) pi = np.dot(cB, inv(B)) z = np.dot(pi, N) d = z - cN beta = np.transpose(np.dot(inv(B), np.transpose(b))) q = d[0].argmin() if d[0][q] > 0: buf = np.zeros((1, m)) i = m while i < n: buf[0][i-m] = x[0][i] i += 1 buf = beta - np.dot(np.dot(inv(B), N), np.transpose(buf)) i = 0 while i < m: x[0][i] = buf[0][i] i += 1 return x alphaQ = np.dot(inv(B), N[:, q]) minP = -1 minmin = sys.float_info.max i = 0 while i < m: if alphaQ[i] > (1 ** (-10)) and beta[i] / alphaQ[i] < minmin: minmin = beta[i] / alphaQ[i] minP = i if minP < 0: return " :( " x[0][q + m] = minmin B[:, minP] = N[:, q] c = np.array([[2, 3, -4, 0]]) A = np.array([[-1, 2, 3, 0], \ [3, 1, 0, 1]]) b = np.array([[24, 32]]) x = simplex(A, b, c) print f(x[0])