线性规划工具 GLPK 的安装及基本使用

线性规划工具 GLPK 的安装及基本使用

1 概述

GLPK 全称GNU Linear Programming Kit.顾名思义,这是GNU计划下一个用于解线性规 划(Linear Programming)的工具包。它可以方便的描述线性规划问题,并给出相应解。 

GLPK 的主页为:http://www.gnu.org/software/glpk/

2 安装

Ubuntu下一条命令即可: sudo apt-get install glpk

3 基本使用

  1. 线性规划问题描述 首先需要一个文件glpsolEx.mod来描述你的线性规划问题,示例如下
    /* Variables */
    var x1 >= 0;
    var x2 >= 0;
    var x3 >= 0;
    
    /* Object function */
    maximize z: 3*x1 + x2 +2*x3;
    
    /* Constrains */
    s.t. con1: x1 + x2 + 3*x3 <= 30;
    s.t. con2: 2*x1 +2*x2 + 5*x3 <= 24;
    s.t. con3: 4*x1 + x2 + 2*x3 <= 36;
    
    end;
    

  2. 使用glpk解此问题 glpsol -m glpsolEx.mod -o glpsolEx.sol  
    -m filename: 指定描述问题的文件 
    -o filename: 指定输出结果保存在哪个文件
  3. 结果 
    Problem:    glpsolEx
    Rows:       4
    Columns:    3
    Non-zeros:  12
    Status:     OPTIMAL
    Objective:  z = 28 (MAXimum)
    
       No.   Row name   St   Activity     Lower bound   Upper bound    Marginal
    ------ ------------ -- ------------- ------------- ------------- -------------
         1 z            B             28                             
         2 a            B             12                          30 
         3 b            NU            24                          24      0.166667 
         4 c            NU            36                          36      0.666667 
    
       No. Column name  St   Activity     Lower bound   Upper bound    Marginal
    ------ ------------ -- ------------- ------------- ------------- -------------
         1 x1           B              8             0               
         2 x2           B              4             0               
         3 x3           NL             0             0                   -0.166667 
    
    Karush-Kuhn-Tucker optimality 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
    
    KKT.DE: max.abs.err = 2.22e-16 on column 1
            max.rel.err = 3.17e-17 on column 1
            High quality
    
    KKT.DB: max.abs.err = 0.00e+00 on row 0
            max.rel.err = 0.00e+00 on row 0
            High quality
    
    End of output
    

    结果中x1 x2 x3对应的activity就是他们最终的结果,即取此结果,目标值最大。z的Activity为28,即最大值为28。

4 用途

glpk的用途是可以快速验证一个新建立的线性规划模型是否正确。在我们对一类实际问题进行抽象后,准备用线性规划来解,可以先做一些小的case,写出相应的线性规划方程,然后用glpk来快速得到结果,以便验证我们的想法,并给我们一些新的想法和直觉。一旦这些直觉得以证明。我们就可以用线性规划解决更大规模的问题,并用glpk来求出结果。

参考资料:

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

余额充值