1.PuLP概述

本文详细介绍如何使用Python的PuLP库进行线性规划建模和求解,包括变量定义、目标函数设置、约束条件添加及求解过程,最后展示了一个具体实例的求解结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

来自 <https://www.learningwhat.com/course/OR2018IE/flow-session/40838/0/>

参考资料:

以下为一个简单的例子(下载),演示了PuLP的建模和求解流程。参考来源链接.

  • 官方文档 链接
  • python_pulp_glpk-tutorial.pdf ·
  • PuLP: A Linear Programming Toolkit for Python.pdf
  • PuLP严格来说是一个基于Python的建模描述语言
    • 它本身并没有求解功能,而是通过整合各种外部的求解工具来进行求解
    • 比如让大家安装的GLPK就是一个求解工具.
  • 相对于Matlab、AMPL来说简单,易用
  • 安装PuLP及GLPK,链接,密码:6y89,GLPK目录.
  • 分别安装PuLP和winglpk(后者安装方法见解压中的说明文件)

导入PuLP模块

from pulp import *

创建一个线性规划问题,名字为"test1"

prob = LpProblem("test1", LpMinimize)

Variables(决策变量)

# 0 <= x <= 4
x = LpVariable("x", 0, 4)

# -1 <= y <= 1
y = LpVariable("y", -1, 1)

# 0 <= z
z = LpVariable("z", 0)

LpVariable的第1位置参数为name(变量名),第2位置参数为lowBound(下限),第3位置参数为upBound(上限),第4位置参数为cat(变量类型)

使用None来表示无穷大(为lowBound的时候表示负无穷大,为upBound的时候为正无穷大), 例如z <= 0应表示为LpVariable("z", None, 0)

变量的类型默认值为LpContinuous, 整数类型为LpInteger, 0-1变量为LpBinary.

Objective(目标函数)

prob += x + 4*y + 9*z, "obj"

(最后一个name为该问题的名称,根据需要可设可不设)

Constraints(约束条件)

prob += x+y <= 5, "c1"
prob += x+z >= 10, "c2"
prob += -y+z == 7, "c3"

(最后一个name为该约束的名称,根据需要可设可不设)

输出一个LP文件

Don't do this on LearningWhat.com !!

prob.writeLP("test1.lp")

用默认的求解器(solver)求解问题

  • 用prob.solve(GLPK())使用GLPK作为solver, 可使用用GLPK(msg = 0)来消除GLPK生成的消息
  • 如果GLPK不在系统路径中,而且你没有安装pulpGLPK模块,将GLPK()替换成GLPK("/path/"),其中的/path/表示glpsol的路径(不包含glpsol 本身).
  • 如果你要使用CPLEX, 则用CPLEX()替换GLPK().
  • 如果你要使用XPRESS, 则用XPRESS()替换GLPK().
  • 如果你要使用COIN, 则用COIN()替换GLPK(). 在这种情况下,系统中需设置clp和cbc的路径.

prob.solve()

# 打印出已经求解问题的状态
print("Status:", LpStatus[prob.status])

# 打印出最优解
for v in prob.variables():
    print(v.name, "=", v.varValue)

# 打印最优目标函数值
print("objective=", value(prob.objective))

Status: Optimal
x = 4.0
y = -1.0
z = 6.0
objective= 54.0

Windows 7上安装pulp和glpk步骤: 亲测环境: Windows 6.1.7601 Service Pack 1 Build 7601 x64 Python 2.7.11 PuLP 1.6.8 GLPK 4.34 安装步骤: 1、下载PuLP安装包:前提是,已安装python2.6以及2.6以上版本,在网页(https://pythonhosted.org/PuLP/main/installing_pulp_at_home.html)上点击PuLP zipfile下载pulp包,当然,也可以在我的资源里下载 2、安装PuLP:将zipfile解压缩,并在命令行窗口中,进入解压缩的目录,然后输入命令:setup.py install 3、下载glpk安装包:在网页(https://sourceforge.net/projects/gnuwin32/files/glpk/4.34/)上,下载glpk-4.34-setup.exe(也可以在我的资源里下载),然后双击默认安装 4、按照以上步骤,安装完以后,写一个.py的脚本并运行,脚本内容: from pulp import * pulp.pulpTestAll() 然后,会看到以下类似输出结果: D:\002-Task_150524\117-17data_thesis\004-code\testPulp.py Testing zero subtraction Testing inconsistant lp solution Testing continuous LP solution Testing maximize continuous LP solution Testing unbounded continuous LP solution Testing Long Names Testing repeated Names Testing zero constraint Testing zero objective Testing LpVariable (not LpAffineExpression) objective Testing Long lines in LP Testing LpAffineExpression divide Testing MIP solution Testing MIP solution with floats in objective Testing MIP relaxation Testing feasibility problem (no objective) Testing an infeasible problem Testing an integer infeasible problem Testing column based modelling Testing dual variables and slacks reporting Testing fractional constraints Testing elastic constraints (no change) Testing elastic constraints (freebound) Testing elastic constraints (penalty unchanged) Testing elastic constraints (penalty unbounded) * Solver pulp.solvers.PULP_CBC_CMD passed. Solver pulp.solvers.CPLEX_DLL unavailable Solver pulp.solvers.CPLEX_CMD unavailable Solver pulp.solvers.CPLEX_PY unavailable Solver pulp.solvers.COIN_CMD unavailable Solver pulp.solvers.COINMP_DLL unavailable Testing zero subtraction Testing inconsistant lp solution Testing continuous LP solution Testing maximize continuous LP solution Testing unbounded continuous LP solution Testing Long Names Testing repeated Names Testing zero constraint Testing zero objective Testing LpVariable (not LpAffineExpression) objective Testing LpAffineExpression divide Testing MIP solution Testing MIP solution with floats in objective Testing MIP relaxation Testing feasibility problem (no objective) Testing an infeasible problem Testing an integer infeasible problem Testing column based modelling Testing fractional constraints Testing elastic constraints (no change) Testing elastic constraints (freebound) Testing elastic constraints (penalty unchanged) Testing elastic constraints (penalty unbounded) * Solver pulp.solvers.GLPK_CMD passed. Solver pulp.solvers.XPRESS unavailable Solver pulp.solvers.GUROBI unavailable Solver pulp.solvers.GUROBI_CMD unavailable Solver pulp.solvers.PYGLPK unavailable Solver pulp.solvers.YAPOSIB unavailable 表示已经成功安装pulp和glpk
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值