1、线性规划的单纯形法求解步骤
2、大M法的excel求解
①例题如下:
②具体步骤如下:
③实验结果如下:
④结果分析
由实验结果可以得到,当x1为0,x2为0,x3为2时,求得最大值为2
3、Excel自带的规划求解包求解上个例题
①具体步骤如下:
在G2处填入=MMULT(B6:D6,G6:G8)
在G3处填入=MMULT(B3:D3,G6:G8)
在G4处填入=MMULT(B4:D4,G6:G8)
在G5处填入=MMULT(B5:D5,G6:G8)
点击数据中的规划求解
修改以下地方并点击求解
②实验结果如下
③结果分析
由实验结果可知,最后结果算出来与大M法的excel求解结果一致,都为2
4、python编程求解上个例题
①具体代码如下:
import numpy as np
class Simplex():
def __init__(self):
self._A = ""
self._b = ""
self._c = ''
self._B = ''
self.row = 0
def solve(self):
A = []
b = []
c = []
self._A = np.array(A, dtype=float)
self._b = np.array(b, dtype=float)
self._c = np.array(c, dtype=float)
self._A = np.array([[0,2,-1],[0,1,-1]],dtype=float)
self._b = np.array([-2,1],dtype=float)
self._A = np.array([[1,-1,1]])
self._b = np.array([2])
self._c = np.array([2,1,1],dtype=float)
self._B = []
self.row = len(self._b)
self.var = len(self._c)
(x, obj) = self.Simplex(self._A, self._b, self._c)
self.pprint(x, obj, A)
def pprint(self, x, obj, A):
px = ['x_%d = %f' % (i + 1, x[i]) for i in range(len(x))]
print(','.join(px))
print('max : %f' % obj)
for i in range(len(A)):
print('%d-th line constraint value is : %f' % (i + 1, x.dot(A[i])))
def InitializeSimplex(self, A, b):
b_min, min_pos = (np.min(b), np.argmin(b))
if (b_min < 0):
for i in range(self.row):
if i != min_pos:
A[i] = A[i] - A[min_pos]
b[i] = b[i] - b[min_pos]
A[min_pos] = A[min_pos] * -1
b[min_pos] = b[min_pos] * -1
slacks = np.eye(self.row)
A = np.concatenate((A, slacks), axis=1)
c = np.concatenate((np.zeros(self.var), np.ones(self.row)), axis=0)
new_B = [i + self.var for i in range(self.row)]
obj = np.sum(b)
c = c[new_B].reshape(1, -1).dot(A) - c
c = c[0]
e = np.argmax(c)
while c[e] > 0:
theta = []
for i in range(len(b)):
if A[i][e] > 0:
theta.append(b[i] / A[i][e])
else:
theta.append(float("inf"))
l = np.argmin(np.array(theta))
if theta[l] == float('inf'):
print('unbounded')
return False
(new_B, A, b, c, obj) = self._PIVOT(new_B, A, b, c, obj, l, e)
e = np.argmax(c)
for mb in new_B:
if mb >= self.var:
row = mb - self.var
i = 0
while A[row][i] == 0 and i < self.var:
i += 1
(new_B, A, b, c, obj) = self._PIVOT(new_B, A, b, c, obj, new_B.index(mb), i)
return (new_B, A[:, 0:self.var], b)
def Simplex(self, A, b, c):
B = ''
(B, A, b) = self.InitializeSimplex(A, b)
obj = np.dot(c[B], b)
c = np.dot(c[B].reshape(1, -1), A) - c
c = c[0]
e = np.argmax(c)
while c[e] > 0:
theta = []
for i in range(len(b)):
if A[i][e] > 0:
theta.append(b[i] / A[i][e])
else:
theta.append(float("inf"))
l = np.argmin(np.array(theta))
if theta[l] == float('inf'):
print("unbounded")
return False
(B, A, b, c, obj) = self._PIVOT(B, A, b, c, obj, l, e)
e = np.argmax(c)
x = self._CalculateX(B, A, b, c)
return (x, obj)
def _CalculateX(self, B, A, b, c):
x = np.zeros(self.var, dtype=float)
x[B] = b
return x
def _PIVOT(self, B, A, b, c, z, l, e):
main_elem = A[l][e]
A[l] = A[l] / main_elem
b[l] = b[l] / main_elem
for i in range(self.row):
if i != l:
b[i] = b[i] - A[i][e] * b[l]
A[i] = A[i] - A[i][e] * A[l]
z -= b[l] * c[e]
c = c - c[e] * A[l]
B[l] = e
return (B, A, b, c, z)
s = Simplex()
s.solve()
x_1 = 0.000000,x_2 = 0.000000,x_3 = 2.000000
max : 2.000000
②实验结果分析:
由运行结果可以得到,结果与上面结果一致,也为2
3、python包求解上个例题
①具体代码如下:
import numpy as np
from scipy import optimize as op
c = np.array([2,1,1])
A_ub = np.array([[0,2,-1],[0,1,-1]])
B_ub = np.array([-2,1])
A_eq = np.array([[1,-1,1]])
B_eq = np.array([2])
res=op.linprog(c,A_ub,B_ub,A_eq,B_eq)
print(res)
con: array([3.41131567e-11])
fun: 1.9999999999762486
message: 'Optimization terminated successfully.'
nit: 4
slack: array([-3.94368982e-11, 3.00000000e+00])
status: 0
success: True
x: array([2.85788562e-13, 5.03801677e-12, 2.00000000e+00])
②实验结果分析:
由运行结果可以得到,结果与上面结果一致,最大值也为2
4、参考文献
①https://blog.csdn.net/qq_40707407/article/details/81709122
②https://wenku.baidu.com/view/8cb7f636eefdc8d376ee3290.html