GLPK - GNU 线性规划工具包(安装、例子、使用手记)

来源:互联网 发布:政府办事大厅网络建设 编辑:程序博客网 时间:2024/05/21 17:55
GNU Linear Programming Kit - GNU 线性规划工具包可以用来解线性规划、混合整数规划以及相关的一些问题,按照官方说明,它的功能如下:

The GLPK (GNU Linear Programming Kit) package is intended for solving large-scale linear programming (LP), mixed integer programming (MIP), and other related problems. It is a set of routines written in ANSI C and organized in the form of a callable library.

GLPK supports the GNU MathProg language, which is a subset of the AMPL language.

The GLPK package includes the following main components:

  • Revised simplex method.
  • Primal-dual interior point method.
  • Branch-and-bound method.
  • Translator for GNU MathProg.
  • Application program interface (API).
  • Stand-alone LP/MIP solver.
官方网站:http://www.gnu.org/software/glpk/glpk.html
GNU MP Bignum Library:http://gmplib.org/
本文参考IBM网站的几篇文章。

GLPK的安装

    如果懂得如何编译,那么直接到官方网站下载然后用优化参数进行编译会获得更好的性能。另外, 在configure的时候它会告诉你,如果有装MP Library的话,处理某些问题的时候性能会有更大的提升。所以,先去下载MP Library来装(当然也是自己优化编译),接着再优化编译GLPK。
    注意,虽然我在Linux下使用它,不过GLPK它是跨平台的,不局限于Linux。

GLPK的使用

    GLPK支持几种语言,包括GNU MathProg。它可以对模型进行求解。懒得写了,具体使用方法参见这里。写好MathProg文件后,直接用这种方法就可以用了。
glpsol -m file_to_solve -o file_solved

GLPK的精彩

    GLPK真是太强了!对综合形式的表达式也能用!看下面:
# basket ball

set players;
set func_name;
set positions;

param values { i in players 
, j in func_name };
param pos_values { i in players
, j in positions };

var x { i in players } binary >=0;

maximize z
: sum{ i in players } ( sum{j in func_name} x[i]*values[i,j] );

s
.t. back       : sum{ i in players} x[i]*pos_values[i,"is back"]>=3;
s
.t. front      : sum{ i in players} x[i]*pos_values[i,"is front"]>=1;
s
.t. middle     : sum{ i in players} x[i]*pos_values[i,"is middle"]>=2;
s
.t. ability{ j in func_name } : (sum{ i in players } x[i]*values[i,j])/5>=2;
s
.t. a3_6       : x[3]+x[6]<=1;
s
.t. a1_4       : x[4]>=x[1];
s
.t. a1_5       : x[5]>=x[1];
s
.t. a2_3       : x[2]+x[3]=1;
s
.t. all_5      : sum{i in players} x[i]=5;





data;

set players
:=1 2 3 4 5 6 7;
set func_name
:=zg tl lb guard;
set positions
:="is front" "is back" "is middle";

param values
:   zg      tl      lb      guard:=
1               3       3       1       3
2               2       1       3       2
3               2       3       2       2
4               1       3       3       1
5               1       3       1       2
6               3       1       2       3
7               3       2       2       1;

param pos_values
:       "is front"      "is back"       "is middle":=
1                       0               1               0
2                       0               0               1
3                       0               1               1
4                       1               1               0
5                       1               1               0
6                       1               0               1
7                       1               1               0;

end;

    另外,它的数据段也能分开在单独的文件,虽然我不知道怎么做,真棒!另外,sum的语法
sum{ i in SET_NAME } expression
刚好跟大sigma是对应的,{}对应sigma的下边,expression对应右边,真是爽到不行了!!!跟我在Mathematica里面用差不多,哈哈。顺便说一下,Mathematica出6.0了。










原创粉丝点击