线性规划的例题

pull/5/head
p3itgm2rp 4 months ago
commit b19bd9bef5

@ -0,0 +1,19 @@
from matplotlib import pyplot as plt
from numpy import ones , diag , c_ , zeros
from scipy.optimize import linprog
import time
start = time.time()
c=list( [-0.05,-0.27,-0.19,-0.185,-0.185])
A=c_[zeros(4),diag([0.025,0.015,0.055,0.026])]
Aeq = [[1,1.01,1.02,1.045,1.065]];beq=[[1]]
a=0;aa=[];ss = []
while a<=0.05:
b=list(ones(4)*a)
res= linprog(c,A,b,Aeq,beq)
aa.append(a);ss.append(-res.fun)
a=a+0.001
end = time.time()
print("花费时间:",end-start)
plt.plot(aa,ss,"r*")
plt.xlabel("$a$");plt.ylabel("$Q$",rotation=90)
plt.savefig("figure5_1_1.png",dpi=500) ;plt.show()

@ -0,0 +1,26 @@
import numpy as np
from numpy import ones , zeros , c_,diag
from scipy.optimize import linprog
import matplotlib.pyplot as plt
c = np.append(zeros(5).tolist(),[1]).tolist()
print(c)
A=np.append(zeros(4).reshape(4,1),diag([0.025,0.015,0.055,0.026]),axis=1)
A=np.append(A,ones(4).reshape(4,1)*-1,axis=1).tolist()
Aeq =[[1,1.01,1.02,1.045,1.065,0]] ;beq=[1]
A.append([-0.05,-0.27,-0.19,0.185,-0.185,0])
print(A)
k=0.05;step = 0.005
b=([0]*4);b.append(-k)
print(b)
kk=[];ss=[]
while k<0.28:
res= linprog(c,A,b,Aeq,beq)
kk.append(k)
ss.append(res.fun)
print(res.fun)
k+=step
b[4]=-k
plt.plot(kk,ss,'r*')
plt.xlabel("$k$");plt.ylabel('$R$')
plt.savefig("figures5_1_2.png",dpi=500);plt.show()

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

@ -0,0 +1,56 @@
from scipy.optimize import linprog
import time
import numpy as np
'''
目标函数
min z= -x1 + 4 x2
约束条件
-3x1 + x2 < = 6
x1 + 2x2 <= 4
x2 >= -3
'''
begin = time.time()
c=[-1,4]
A=[[-3,1],[1,2]]
b=[[6],[4]]
bounds=((None , None),(-3,None))
res= linprog(c,A,b,None,None,bounds)
end= time.time()
print("结果为:",res.fun)
print("x的值位",res.x," ",type(res.x))
print(end-begin,"\n",'----------------------------------------')
'''
线性规划问题
max z = x1 - 2x2 - 3x3
约束条件
-2x1 + x2 + x3 <= 9
-3x1 + x2 + 2x3 >= 4
4x1 -2x2 -x3 = -6
x1 >= -10 ,x2 >=0
'''
'''
scipy标准形式
min w= -x1 + 2x2 + 3x3
约束
-2x1 + x2 + x3 <= 9
3x1 - x2 - 2x3 <= -4
'''
c= (-np.array([1,-2,-3])).tolist()
A=[[-2,1,1],[-3,1,2]]
A[1]= (-np.array(A)[1]).tolist()
b= [[9],[4]]
b[1]=(-np.array(b[1])).tolist()
Aeq=[[4,-2,-1]]
beq =[[-6]]
LB =[-10,0,None]
UB = [None]*len(c)
bounds= tuple(zip(LB,UB))
res= linprog(c,A,b,Aeq,beq,bounds)
end= time.time()
print("结果为:",res.fun)
print("x的值位",res.x," ",type(res.x))
print(end-begin,"\n",'----------------------------------------')
print(res)

@ -0,0 +1,23 @@
import time
import numpy as np
from scipy.optimize import linprog
start = time.time()
c = [-110,-120,-130,-110,-115,150]
c = (-np.array(c)).tolist()
A=[[1,1,0,0,0,0],
[0,0,1,1,1,0],
[8.8,6.1,2.0,4.2,5.0,-6],
[8.8,6.1,2.0,4.2,5.0,-3]
]
A[3]=(-np.array(A[3])).tolist()
b=[[200],[250],[0],[0]]
Aeq =[[1,1,1,1,1,-1]]
beq = [[0]]
LB= [0]*len(c)
UB= [None]*len(c)
bounds= tuple(zip(LB,UB))
res = linprog(c,A,b,Aeq,beq,bounds)
end = time.time()
print("最优解:\n",res.x)
print("目标函数最小值:\n",-res.fun)
Loading…
Cancel
Save