首页 > 其他 > 详细

PuLp之一: 线性规划问题求解的一般步骤

时间:2020-09-28 23:25:04      阅读:111      评论:0      收藏:0      [点我收藏+]

使用PuLp求解

1、安装PuLp并导入, 下载地址:https://github.com/coin-or/pulp
pip install pulp
from pulp import *

2、定义线性规划问题
Prob = LpProblem ( problem name , sense )

3、构造函数,用来构造一个LP问题实例,其中name指定问题名(输出信息用),sense值是LpMinimize或LpMaximize中的一个,用来指定目标函数是求最大值还是最小值。

4、定义决策变量
有两种方式, 一种是定义单个变量(适用于变量个数不多的情况)
DV = LpVariable ( decision variable name , lowbound , upbound ,category )
decision variable name指定变量名,lowBound和upBound是下界和上界, 默认分别是负无穷到正无穷,cat用来指定变量是离散(LpInteger,LpBinary)还是连续(LpContinuous)

还有一种方式是用dict方式来定义大量的变量,如下:
Ingredients = [‘Chicken‘,‘Beef‘,‘Mutton‘,‘Rice‘,‘Wheat‘,‘Gel‘]
variables = LpVariable.dicts ("Ingr",Ingredients,0)
上面两行代码输出的变量字典是:
{‘Chicken‘: Ingr_Chicken, ‘Beef‘: Ingr_Beef, ‘Mutton‘: Ingr_Mutton, ‘Rice‘: Ingr_Rice, ‘Wheat‘: Ingr_Wheat, ‘Gel‘: Ingr_Gel}

5、添加目标函数和约束条件
Prob += linear objective in equantion from objective name
Prob += linear objective in equantion from constraint name

6、写入LP文件
Prob.writeLP ( filename )

7、模型求解
Prob.slove ( )

8、结果显示
check status : pulp.LpStatus[Prob.status]

其它参考资料:https://github.com/benalexkeen/Introduction-to-linear-programming
https://www.codercto.com/a/109524.html

PuLp之一: 线性规划问题求解的一般步骤

原文:https://www.cnblogs.com/treasury-manager/p/13747308.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!