||
1.使用工具 PULP+GLPK
PULP 是Python PULP是用python写的建模描述语言,GLPK是线性规划工具
2.下载、安装Python、PULP 和GLPK
以Python 2.7+PULP 1.4.8 +GLPK4.49 为例
Python 下载地址:http://www.python.org/getit/
GLPK 下载地址:http://www.lupaworld.com/proj-cont-id-121212.html
PUPL 下载地址:http://www.coin-or.org/download/source/PuLP/
PUPL Windows安装:
1.解压下载好的文件(如解压到C:\Python27)
2. 点击开始—运行 键入cmd 进入命令行,键入cd C:\Python27\PuLP-1.4.8更改路径到setup.py所在的文件夹
3.键入setup.py install 安装PUPL
4.测试是否安装成功 import PULP
pulp.pulpTestAll()
GLPK安装 1.解压下载好的文件(如解压到C:\glpk-4.49)
3.求解过程
Formulate the Objective Function
The objective function becomes:
min 0:013x1 + 0:008x2
The Constraints
The constraints on the variables are that they must sum to 100 and that the nutritional requirements are met:
1:000x1 + 1:000x2 = 100.0
0:100x1 + 0:200x2 >= 8.0
0:080x1 + 0:100x2 >=6.0
0:001x1 + 0:005x2 >=2.0
0:002x1 + 0:005x2 >=0.4
Solution to Simplified Problem
from pulp import *
##Create the ’prob’ variable to contain the problem data
prob = LpProblem("The Whiskas Problem",LpMinimize)
x1=LpVariable("ChickenPercent",0,None,LpInteger)
x2=LpVariable("BeefPercent",0,None, LpInteger)
prob += 0.013*x1 + 0.008*x2
## The five constraints are entered
prob += x1 + x2 == 100
prob += 0.100*x1 + 0.200*x2 >= 8.0
prob += 0.080*x1 + 0.100*x2 >= 6.0
prob += 0.001*x1 + 0.005*x2 <= 2.0
prob += 0.002*x1 + 0.005*x2 <= 0.4
# The problem data is written to an .lp file
prob.writeLP("WhiskasModel.lp")
# The problem is solved using PuLP’s choice of Solver
prob.solve(“C:\\glpk-4.49\\w32\\glpsol.exe”)
# prob.solve()也可以,或者将C:\glpk-4.49\w32\glpsol.exe加入到path 用#prob.solve(glpk())
# The status of the solution is printed to the screen
print "Status:", LpStatus[prob.status]
for v in prob.variables():
print v.name, "=", v.varValue
print "Total Cost of Ingredients per can = ", value(prob.objective)
-----------------------Result ---------------------------------------
Status: Optimal
ChickenPercent = 34.0
BeefPercent = 66.0
Total Cost of Ingredients per can = 0.97
Powered by Discuz! X2.5 © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 ) 论坛法律顾问:王兆丰
GMT+8, 2025-5-9 13:50 , Processed in 0.296278 second(s), 28 queries .