from scipy import optimize as op
import numpy as np
c=np.array([500,550,630,1000,800,700,800,700,600,950,900,930,1000,960,840,650,600,700,1200,1040,980,860,880,780])
A=np.zeros((6,24),dtype=np.float64)
B=np.zeros((6,1),dtype=np.float64)
Aeq=np.array([[1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1],[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0],[0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],[0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0],[0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0],[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0],[0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1]])
Beq=np.array([76,88,96,40,42,56,44,39,60,59])
print(op.linprog(-c,A,B,Aeq,Beq))
sicpy.optimize运行结果为:
con: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crossover_nit: 0
eqlin: marginals: array([ -0., -100., -210., -410., -790., -750., -630., -1000.,
-800., -830.])
residual: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
fun: -284230.0
ineqlin: marginals: array([-0., -0., -0., -0., -0., -0.])
residual: array([0., 0., 0., 0., 0., 0.])
lower: marginals: array([290., 200., 0., 0., 0., 130., 90., 150., 130., 150., 0.,
0., 0., 0., 0., 560., 410., 340., 0., 120., 60., 550.,
330., 460.])
residual: array([ 0., 0., 6., 39., 31., 0., 0., 0., 0., 0., 29., 59., 2.,
56., 38., 0., 0., 0., 40., 0., 0., 0., 0., 0.])
message: 'Optimization terminated successfully. (HiGHS Status 7: Optimal)'
nit: 15
slack: array([0., 0., 0., 0., 0., 0.])
status: 0
success: True
upper: marginals: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0.])
residual: array([inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf])
x: array([ 0., 0., 6., 39., 31., 0., 0., 0., 0., 0., 29., 59., 2.,
56., 38., 0., 0., 0., 40., 0., 0., 0., 0., 0.])
于是,可得:
目标函数最大值为:284230元。
目标函数最大时,决策变量取值为:[第1块地小麦,第2块地小麦,第3块地小麦,第4块地小麦,第5块地小麦,第6块地小麦,第1块地玉米,第2块地玉米,第3块地玉米,第4块地玉米,第5块地玉米,第6块地玉米,第1块地蔬菜,第2块地蔬菜,第3块地蔬菜,第4块地蔬菜,第5块地蔬菜,第6块地蔬菜,第1块地水果,第2块地水果,第3块地水果,第4块地水果,第5块地水果,第6块地水果]=[ 0., 0., 6., 39., 31., 0., 0., 0., 0., 0., 29., 59., 2.,56., 38., 0., 0., 0., 40., 0., 0., 0., 0., 0.]。