单纯形法python实现
运筹学刚学完单纯形法,为了减轻作业负担,这个里给出通过单纯形表求解线性规划的python实现代码。
import numpy as np
class LinearProblem:
def __init__(self, A, b, c):
"""
used to solve the linear constrain optimize problem.
min c*x
s.t. Ax=b
:param A: shape (m, n)
:param b: shape (m, 1)
:param c: shape (n, 1)
"""
self.x = np.zeros_like(c, dtype=float)
self.A = np.array(A)
self.x_dim = self.A.shape[1]
self.b = np.array(b).reshape([-1, 1])
self.b_dim = self.b.shape[0]
self.c = np.array(c).reshape([-1, 1])
self.no_solution = False
self.minimum = False
self.find_solution = False
m = np.concatenate([self.A, np.eye(self.b_dim), self.b], axis=1)
init_zeta = np.concatenate([np.zeros([1, self.x_dim]), -np.ones([1, self.b_dim]), np.zeros([1, 1])], axis=1)
self.m = np.concatenate([init_zeta, m], axis=0)
self.indexes = list(range(self.x_dim+self