运筹系列10:线性规划开源软件GLPK和PyMathProg

1. GLPK

GLPK全称GNU Linear Programming Kit,是一个开源的求解线性规划问题的工具套件,由c写成,可以求解大规模线性规划问题、混合整数规划问题。GLPK是免费的,在大规模问题上的性能要逊色于商用软件,求解的性能可以参考安装文件doc目录下的miplib2.txt,miplib3.txt和netlib.txt这三个文件,下面是截图:
在这里插入图片描述

GLPK安装非常简单,在centos系统下,执行:

sudo yum install glpk

GLPK使用MathProg(mathematical programming language)建模语言,可以使用GLPSOL这个cmd应用来进行交互。作为示例,新建一个文件problem.mod,输入:

var x1;
var x2;
maximize obj: 0.6 * x1 + 0.5 * x2;
s.t. c1: x1 + 2 * x2 <= 1;
s.t. c2: 3 * x1 + x2 <= 2;
solve;
display x1, x2;
end;

然后退出文件编辑,输入glpsol -m p.mod,最后输出了求解结果。

GLPSOL: GLPK LP/MIP Solver, v4.65
Parameter(s) specified in the command line:
 --math short.mod
Reading model section from short.mod...
9 lines were read
Generating obj...
Generating c1...
Generating c2...
Model has been successfully generated
GLPK Simplex Optimizer, v4.65
3 rows, 2 columns, 6 non-zeros
Preprocessing...
2 rows, 2 columns, 4 non-zeros
Scaling...
 A: min|aij| =  1.000e+00  max|aij| =  3.000e+00  ratio =  3.000e+00
Problem data seem to be well scaled
Constructing initial basis...
Size of triangular part is 2
*     0: obj =  -0.000000000e+00 inf =   0.000e+00 (2)
*     2: obj =   4.600000000e-01 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.1 Mb (102250 bytes)
Display statement at line 8
x1.val = 0.6
x2.val = 0.2
Model has been successfully processed

2. PyMathProg

python环境下可以使用PyMathProg进行建模并用GLPK求解。

pip install pymprog

下面是个简单的例子:

from pymprog import *
begin('bike production')
x, y = var('x, y') # variables
maximize(15 * x + 10 * y, 'profit')
x <= 3 # mountain bike limit
y <= 4 # racer production limit
x + y <= 5 # metal finishing limit
solve()

下面是几种定义变量的方式:

x,y = var('x','y') #默认变量是非负连续变量
z = var('z',3) #非负连续变量数组
a = var('a',kind=bool) #0/1变量。其他还有:float (默认): 连续变量;int: 整数变量;bool: 0/1变量
b = var('b',bounds = (0,5)) #限制上下界的连续变量
cindex = ('c1','c2','c3')
c = var('c',cindex) #使用自定义index set来定义变量

下面是关于index的一些示例:

A = (1, 2, 3) #interable的python类型(比如tuple,list,set……)都可以作为index。
B = (6, 7, 8)
C = iprod(A, B) #可以用这种方式形成更大的index集合。
x = var('x',C) #使用index集合创建变量
x[2,7].name

下面是关于parameter的一些示例:

k = par('k', [2,3,4]) #内容是list/tuple的话,那生成的就是一个parameter的list
p = par('P', {'east':0, 'west':2, 'south':3, 'north':1}) #内容是dict的话,生成的是以dict的key为下标的list
a, b, c = par('a,b,c', 3, (5,2)) #可以一次定义多个参数
r = [{(3,4):3, (1,2):4}, 5]
R = par('R', r) #可以recusive定义参数
x = var('x', 3)
x.value = 10 #可以重新定义参数值

下面是关于contraint的一些示例:

x = var('x', 3)
y = var('y')
c = [6,4,3]
sum(p*q for p,q in zip(c,x)) <= y #可以直接添加约束
st( sum(p*q for p,q in zip(c,x)) <= y ) #也可以用st表明这是一个约束

目标函数定义很简单:

maximize(sum(c[i]*x[i] for i in range(3)), 'Profit')

可以调用的solver有四种,线性规划有simplex、exact、interior三种(默认是simplex),整数规划是intopt,可以显式调用,例如:

solve(simplex)
print(vobj()) #获取目标函数值
print(x.primal) #获取变量求解值

下面是营养问题的综合示例:

from pymprog import *
minlev = group('minlev')
fruits = ('apple', 'pear', 'orange')
nutrit = ('fat', 'fibre', 'vitamin')
ntrmin = ( 10, 50, 30 ) # min nutrition intake
#nutrition ingredients of each fruit
ingred = ('apple': (3,4,5), 'pear': (2,4,1),
          'orange': (2,3,4))
diet = var('diet', fruits, int)
minimize(sum(diet[frt] for frt in fruits), 'mindiet')
for n, ntr in enumerate(nutrit):
   minlev[ntr] = sum( diet[frt] * ingred[frt][n] for frt in fruits) >= ntrmin[n]

点击这里可以参考更多例子。

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值