glpk 使用简介

本文只是简介glpk的简单使用~不涉及原理~做一个简单的备忘~~

GLPK 使用简介:

这几天第一次接触glpk,对于输入文件的格式,有一些繁琐,但不得不承认glpk真的很强大。

通过下面的一个简单(无实际意义的)例子,说明在glpk中的一些简单的格式。

简单示例:

var x1; //每一行以分号结尾
var x2;
var x3;
var x4;
var x5;
var e13;
var e14;
var e15;
var r13;
var r14;
var r15;

var y1 binary; //二值变量,0/1,加上关键字binary,类似关键字还有Integer等。
var y2 binary;

minimize z: y1 + y2;  // 目标函数

s.t. con1 : -x1+ x3 + e13 + r13 = 327;//约束条件
s.t. con2 : -x1+ x4 + e14 + r14 = 275; 
s.t. con3 : -x1+ x5 + e15 + r15 = 277;
s.t. con19 : e13 - 5 <= 0 ;
s.t. con20 : -e13 - 5 <= 0 ;
s.t. con21 : e14 - 5 <= 0 ;
s.t. con22 : -e14 - 5 <= 0 ;
s.t. con23 : e15 - 5 <= 0 ;
s.t. con24 : -e15 - 5 <= 0 ;

在命令行输入:glpsol -m temp.mod -o temp.s ol //指定输入文件为temp.mod。结果文件为temp.sol中。


结果文件为:

Problem:    temp
Rows:       10
Columns:    12 (2 integer, 2 binary)
Non-zeros:  20
Status:     INTEGER OPTIMAL
Objective:  z = 0 (MINimum)

   No.   Row name        Activity     Lower bound   Upper bound
------ ------------    ------------- ------------- -------------
     1 z                           0                             
     2 con1                      327           327             = 
     3 con2                      275           275             = 
     4 con3                      277           277             = 
     5 con19                      -5                           5 
     6 con20                       5                           5 
     7 con21                      -5                           5 
     8 con22                       5                           5 
     9 con23                      -5                           5 
    10 con24                       5                           5 

   No. Column name       Activity     Lower bound   Upper bound
------ ------------    ------------- ------------- -------------
     1 x1                       -282                             
     2 x3                         50                             
     3 x4                         -2                             
     4 x5                          0                             
     5 e13                        -5                             
     6 e14                        -5                             
     7 e15                        -5                             
     8 r13                         0                             
     9 r14                         0                             
    10 r15                         0                             
    11 y1           *              0             0             1 
    12 y2           *              0             0             1 

Integer feasibility conditions:

KKT.PE: max.abs.err = 0.00e+00 on row 0
        max.rel.err = 0.00e+00 on row 0
        High quality

KKT.PB: max.abs.err = 0.00e+00 on row 0
        max.rel.err = 0.00e+00 on row 0
        High quality

End of output


输出文件的详细格式参见:

glpk result

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、付费专栏及课程。

余额充值