使用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
原文:https://www.cnblogs.com/treasury-manager/p/13747308.html