实验九:求解线性/整数/01/连续线性规划

本博文源于python基础,旨在讲解用python做线性规划。先讲解python下做线性规划的相关函数,然后python四道例题进行演示。

0.linprog()函数调用格式

optimize.linprog(C,A,B,Aeq,beq,bounds,method,X0,options)
  • C 是最值向量
  • A和B对应线性不等式约束
  • Aeq和Beq对应线性等式约束
  • bounds对应公式中的Lb和Ub,即决策向量的上界和下界
  • method是求解器(方法)的类型
  • X0是X的初始值
  • option提供常用最值算法,如Nelder-Mead,Powell,CG,BFGS,Newtonn-CG等;部分参数可默认。

1.求解线性规划 m a x f = 2 x 1 + 3 x 2 + 4 x 3 max f=2x_1+3x_2+4x_3 maxf=2x1+3x2+4x3

s . t . { 1.5 x 1 + 3 x 2 + 5 x 3 ≤ 600 280 x 1 + 250 x 2 + 400 x 3 ≤ 60000 x 1 , x 2 , x 3 ≥ 0 s.t.\left\{ \begin{array}{rcl} 1.5x_1+3x_2+5x_3\le{600}\\ 280x_1+250x_2+400x_3\leq{60000}\\ x_1,x_2,x_3\ge{0}\\ \end{array} \right. s.t.1.5x1+3x2+5x3600280x1+250x2+400x360000x1,x2,x30

>>> from scipy import optimize # 导入库
>>> from sympy import * # 导入库
>>> C = Matrix([2,3,4]) # 确定目标函数系数矩阵C
>>> A = Matrix([[1.5,3,5],[280,250,400]]) # 确定约束条件系数矩阵A
>>> B = Matrix([[600],[60000]]) # 确定约束条件常数矩阵B
>>> res = optimize.linprog(-C,A,B,method='simplex') # 问题要求最大值,雇佣-C
>>> print(res)
     con: array([], dtype=float64)
     fun: -632.258064516129
 message: 'Optimization terminated successfully.'
     nit: 3
   slack: array([0., 0.])
  status: 0
 success: True
       x: array([ 64.51612903, 167.74193548,   0.        ])
>>>

通过观察解之的 x 1 = 64.52 , x 2 = 167.74 , x 3 = 0 x_1=64.52,x_2=167.74,x_3=0 x1=64.52,x2=167.74,x3=0时,此时线性规划最优值为 f = 632.26 f=632.26 f=632.26

2.用python求解连续线性规划问题

m a x f = 72 x 1 + 64 x 2 s . t { x 1 + x 2 ≤ 50 12 x 1 + 8 x 2 ≤ 480 3 x 1 ≤ 100 x 1 ≥ 0 , x 2 ≥ 0 max f=72x_1+64x_2\\ s.t\left\{ \begin{array}{rcl} x_1+x_2\le{}50\\ 12x_1+8x_2\leq{480}\\ 3x_1\leq{100}\\ x_1\geq{0},x_2\geq{0}\\ \end{array} \right. maxf=72x1+64x2s.tx1+x25012x1+8x24803x1100x10,x20

>>> from scipy import optimize
>>> from sympy import * # 第一行第二行导入库
>>> C = Matrix([72,64]) # 确定目标函数系数矩阵C
>>> A = Matrix([[1,1],[12,8],[3,0]]) # 确定约束条件系数矩阵A
>>> B = Matrix([[50],[480],[100]]) # 确定约束条件常数矩阵B
>>> res = optimize.linprog(-C,A,B,method='simplex') # 问题要求最低阿志,故用-C
>>> res # 最优值是显示结果的相反数
     con: array([], dtype=float64)
     fun: -3360.0
 message: 'Optimization terminated successfully.'
     nit: 4
   slack: array([ 0.,  0., 40.])
  status: 0
 success: True
       x: array([20., 30.])
>>>


最后得出结果是 x 1 = 20 , x 2 = 30 x_1=20,x_2=30 x1=20,x2=30,此时线性规划问题最优值为 f = 3360 f=3360 f=3360

3.求解整数规划

m a x   y s . t . { y ≤ 1 2 x 1 , y ≤ 1 3 x 2 5 x 1 + 4 x 2 ≤ 960 9 x 1 + 10 x 2 ≤ 1440 ( 5 x 1 + 4 x 2 ) − ( 9 x 1 + 10 x 2 ) ≥ − 60 ( 5 x 1 + 4 x 2 ) − ( 9 x 1 + 10 x 2 ) ≤ 60 x 1 ≥ 0 , x 2 ≥ 0 , y ≥ 0 , 且 为 整 数 max\ y \\ s.t.\left\{ \begin{array}{rcl} y\le\frac{1}{2}x_1,y\leq\frac{1}{3}x_2\\ 5x_1+4x_2\leq{960}\\ 9x_1+10x_2\leq{1440}\\ (5x_1+4x_2)-(9x_1+10x_2)\geq{-60}\\ (5x_1+4x_2)-(9x_1+10x_2)\leq{60}\\ x_1\ge{0},x_2\ge{0},y\ge{0},且为整数 \end{array} \right. max ys.t.y21x1,y31x25x1+4x29609x1+10x21440(5x1+4x2)(9x1+10x2)60(5x1+4x2)(9x1+10x2)60x10,x20,y0,

import pulp

model = pulp.LpProblem("Example 8",pulp.LpMaximize)
x1 = pulp.LpVariable("x1",lowBound=0,cat='Integer') # 限制约束变量为整数
x2 = pulp.LpVariable('x2',lowBound=0,cat = 'Integer') # 限制约束变量为整数
y = pulp.LpVariable('y',lowBound=0,cat='Integer') # 限制约束变量为整数
# 目标函数
model += y
# 约束条件
model += y-0.5*x1 <= 0
model += y-1/3*x2 <= 0
model += 5*x1+4*x2 <= 960
model += 9*x1+10*x2 <= 1440
model += 4*x1+6*x2 <= 60

model.solve() # 解决问题
pulp.LpStatus[model.status]

print('x1={}'.format(x1.varValue))
print('x2={}'.format(x2.varValue)) # 打印最有解
print("最大产量为y=",pulp.value(model.objective)) # 打印最优值

在这里插入图片描述

x 1 与 x 2 x_1与x_2 x1x2就是最优解

4.求解0-1规划

m a x   f = 1000 x 1 + 1500 x 2 + 900 x 3 + 2100 x 4 s . t . { 4 x 1 + 6 x 2 + 6 x 3 + 8 x 4 ≤ 20 x i = 0 或 1 , i = 1 , 2 , 3 , 4 max \ f = 1000x_1 + 1500x_2+900x_3+2100x_4 s.t.\left\{ \begin{array}{rcl} 4x_1+6x_2+6x_3+8x_4\leq{20}\\ x_i=0或1,i=1,2,3,4 \end{array} \right. max f=1000x1+1500x2+900x3+2100x4s.t.{ 4x1+6x2+6x3+8x420xi=01i=1,2,3,4

import pulp
model = pulp.LpProblem("Example 8",pulp.LpMaximize)
x1 = pulp.LpVariable("x1",lowBound=0,cat='Binary') # 限制约束变量为0-1
x2 = pulp.LpVariable('x2',lowBound=0,cat = 'Binary') # 限制约束变量为0-1
x3 = pulp.LpVariable('x3',lowBound=0,cat = 'Binary') # 限制约束变量为0-1
x4 = pulp.LpVariable('x4',lowBound=0,cat = 'Binary') # 限制约束变量为0-1

# 目标函数
model += 1000*x1+1500*x2+900*x3+2100*x4
# 约束条件
model += 4*x1+6*x2+6*x3+8*x4 <= 20

model.solve() # 解决问题
pulp.LpStatus[model.status]

print('x1={}'.format(x1.varValue))
print('x2={}'.format(x2.varValue)) # 打印最有解
print('x3={}'.format(x3.varValue))
print('x4={}'.format(x4.varValue))

print('x1={}'.format(x1.varValue))

print("最大产量为y=",pulp.value(model.objective)) # 打印最优值

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37149062/article/details/121105749