首页 > 代码库 > Ubuntu 下 glpk 的安装及使用

Ubuntu 下 glpk 的安装及使用

作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4156204.html

glpk是一个开源的求解线性规划的包。

添加源:

deb http://us.archive.ubuntu.com/ubuntu saucy main universe

更新源并安装:

sudo apt-get update

sudo apt-get install glpk

 

 写入如下glpsolEx.mod 文件

 1 /* Variables */ 2 var x1 >= 0; 3 var x2 >= 0; 4 var x3 >= 0; 5  6 /* Object function */ 7 maximize z: x1 + 14*x2 + 6*x3; 8  9 /* Constrains */10 s.t. con1: x1 + x2 + x3 <= 4;11 s.t. con2: x1  <= 2;12 s.t. con3: x3  <= 3;13 s.t. con4: 3*x2 + x3  <= 6;14 15 end;

 

运行 glpsol -m glpsolEx.mod -o glpsolEx.sol,输出到glpsolEx.sol文件中

结果为:

 

 1 Problem:    glpsolEx 2 Rows:       5 3 Columns:    3 4 Non-zeros:  10 5 Status:     OPTIMAL 6 Objective:  z = 32 (MAXimum) 7  8    No.   Row name   St   Activity     Lower bound   Upper bound    Marginal 9 ------ ------------ -- ------------- ------------- ------------- -------------10      1 z            B             32                             11      2 con1         NU             4                           4             2 12      3 con2         B              0                           2 13      4 con3         B              3                           3 14      5 con4         NU             6                           6             4 15 16    No. Column name  St   Activity     Lower bound   Upper bound    Marginal17 ------ ------------ -- ------------- ------------- ------------- -------------18      1 x1           NL             0             0                          -1 19      2 x2           B              1             0               20      3 x3           B              3             0               21 22 Karush-Kuhn-Tucker optimality conditions:23 24 KKT.PE: max.abs.err = 0.00e+00 on row 025         max.rel.err = 0.00e+00 on row 026         High quality27 28 KKT.PB: max.abs.err = 4.44e-16 on row 429         max.rel.err = 1.11e-16 on row 430         High quality31 32 KKT.DE: max.abs.err = 0.00e+00 on column 033         max.rel.err = 0.00e+00 on column 034         High quality35 36 KKT.DB: max.abs.err = 0.00e+00 on row 037         max.rel.err = 0.00e+00 on row 038         High quality39 40 End of output

 

Ubuntu 下 glpk 的安装及使用