文章目录
- 模块下载
- 常用函数
- LpProblem(name='NoName', sense=1)
- LpVariable(name, lowBound=None, upBound=None, cat='Continuous', e=None)
- LpAffineExpression(e=None, constant=0, name=None)
- lpSum(vector)
- LpConstraint(e=None, sense=0, name=None, rhs=None)
- 应用
模块下载
pip install pulp
常用函数
LpProblem(name=‘NoName’, sense=1)
参数
- name
- sense:LpMinimize(default)或者LpMaximize
重要的属性
- object.objective返回
LpAffineExpression对象
(即线性规划问题的目标函数,eg,1*x_0+ -3*x_1+3)
- object.constraints返回
LpConstraints对象
(即线性规划问题的约束条件,字典类型)
- object.status返回
LpStatus对象
(即线性规划问题的状态)
LpStatus key |
string value |
numerical value |
constants.LpStatusOptimal |
Optimal |
1 |
constants.LpStatusNotSolved |
Not Solved |
0 |
constants.LpStatusInfeasible |
Infeasible |
-1 |
constants.LpStatusUnbounded |
Unbounded |
-2 |
constants.LpStatusUndefined |
Undefined |
-3 |
重要的方法
- object.solve(solver=None, **kwargs)
LpVariable(name, lowBound=None, upBound=None, cat=‘Continuous’, e=None)
参数
- name 变量名称
- lowbound 变量下限
- upBound 变量上限
- cat 变量类型,
Integer、Binary或Continuous(default)
LpAffineExpression(e=None, constant=0, name=None)
- e
- None 空白表达式
- dict 键为变量名,值为参数
- list、generator of 2-tuples dict.items()
- LpElement
lpSum(vector)
LpConstraint(e=None, sense=0, name=None, rhs=None)
- e
LpAffineExpression对象
- sense
LpConstraintEQ、LpConstraintGE、LpConstraintLE对象
- name 名称
- rhs 约束目标,数值型
应用
- 目标函数
z = 160 x 11 + 130 x 12 + 220 x 13 + 170 x 14 + 140 x 21 + 130 x 22 + 190 x 23 + 150 x 24 + 190 x 31 + 200 x 32 + 230 x 33 z=160x_{11}+130x_{12}+220x_{13}+170x_{14}+140x_{21}+130x_{22}+190x_{23}+150x_{24}+190x_{31}+200x_{32}+230x_{33} z=160x11+130x12+220x13+170x14+140x21+130x22+190x23+150x24+190x31+200x32+230x33
- 约束条件
x 11 + x 12 + x 13 + x 14 = 50 x_{11}+x_{12}+x_{13}+x_{14}=50 x11+x12+x13+x14=50
x 21 + x 22 + x 23 + x 24 = 60 x_{21}+x_{22}+x_{23}+x_{24}=60 x21+x22+x23+x24=60
x 31 + x 32 + x 33 = 50 x_{31}+x_{32}+x_{33}=50 x31+x32+x33=50
30 ≤ x 11 + x 21 + x 31 ≤ 80 30\le x_{11}+x_{21}+x_{31} \le 80 30≤x11+x21+x31≤80
70 ≤ x 12 + x 22 + x 32 ≤ 140 70\le x_{12}+x_{22}+x_{32}\le 140 70≤x12+x22+x32≤140
10 ≤ x 13 + x 23 + x 33 ≤ 30 10\le x_{13}+x_{23}+x_{33}\le 30 10≤x13+x23+x33≤30
10 ≤ x 14 + x 24 ≤ 50 10\le x_{14}+x_{24}\le 50 10≤x14+x24≤50
from pulp import *
problem = LpProblem('myPro', LpMinimize)
x11 = LpVariable("x11", lowBound=0)
x12 = LpVariable("x12", lowBound=0)
x13 = LpVariable("x13", lowBound=0)
x14 = LpVariable("x14", lowBound=0)
x21 = LpVariable("x21", lowBound=0)
x22 = LpVariable("x22", lowBound=0)
x23 = LpVariable("x23", lowBound=0)
x24 = LpVariable("x23", lowBound=0)
x31 = LpVariable("x31", lowBound=0)
x32 = LpVariable("x32", lowBound=0)
x33 = LpVariable("x33", lowBound=0)
X = [x11, x12, x13, x14, x21, x22, x23, x24, x31, x32, x33]
c = [160, 130, 220, 170, 140, 130, 190, 150, 190, 200, 230]
z = 0
for i in range(len(X)):
z += X[i]*c[i]
prob += z
prob += x11+x12+x13+x14 == con[0]
prob += x21+x22+x23+x24 == con[1]
prob += x31+x32+x33 == con[2]
prob += x11+x21+x31 <= con[3]
prob += x11+x21+x31 >= con[4]
prob += x12 + x22 + x32 <= con[5]
prob += x12 + x22 + x32 >= con[6]
prob += x13 + x23 + x33 <= con[7]
prob += x13 + x23 + x33 >= con[8]
prob += x14 + x24 <= con[9]
prob += x14 + x24 >= con[10]
status = prob.solve()
for i in prob.variables():
print(i.varValue)