注册地址 登录
数学建模社区-数学中国 返回首页

水木年华zzu的个人空间 http://www.madio.net/?94765 [收藏] [复制] [分享] [RSS]

日志

Python解线性规划问题

已有 3877 次阅读2013-5-17 09:41 |个人分类:学习心得| 安装, 下载地址, Windows, python

1.使用工具 PULP+GLPK

PULP Python PULP是用python写的建模描述语言,GLPK是线性规划工具

2.下载、安装PythonPULP 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


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2024-5-10 21:14 , Processed in 0.562248 second(s), 28 queries .

回顶部