写在前面的话 ' H$ G! r6 Y! G: R# G7 Y$ L" A! p. n) A) K, A+ \& \
在学习和工作之余写点东西,这种想法由来已久了。只是总是觉得很忙,抽不出时间做一点点自己喜欢做的事情。突然有一天发现,你必须牺牲某些东西才能得到某些东西。所以,就索性写上几句话聊以**吧。 9 y m' p2 R( b E最开始,只想翻译一下GAMS的用户手册。但是又觉得这样写很枯燥,不光读者不喜欢看,连自己也要打瞌睡了。如果想催眠的话,还不如去听评书。但是本书还是以解释GAMS的用法为主。中间夹杂着一些自己做modelling 的体验,还可能还有一些毫不相关的东西。可能有很多错误,请读者们原谅。想学习GAMS的同学,想必一定是数学,运筹学或计算机相关专业的了。让大家失望了,其实我的专业是chemical engineering, 本科时候做的是量子化学。阴差阳错上了贼船。但是觉得自己用了这么多年了,总该有点体会,能过帮帮大家。 $ N: I0 l% \4 K- Y G- C/ J2 F$ a 5 i7 E* ~# e4 j1 X/ Y* M3 D进入正题之前,还想啰嗦几句题外的话。7 T+ K0 Z- |9 a. ^
能看到这篇文字的同学,恐怕大多数是在灯下苦读的学子。实际上学习某种工具并不重要。真正要学习的是知识,是思维的方法。就像很多计算机系的学生一样,把数据结构和算法精通了,任何工具都是一样的。所以,你用GAMS或者不用GAMS,都没有关系,要把握事物的本质,最关键的东西抓住了,任何语言就是一种输入法。另外,学习一定要有兴趣,要认识到他的beauty,不要把什么都换算**民币,那样你会觉得很累。除了,coding 以外,还要博览群书,文史哲,拿来翻翻,就当休息了。 1 O0 d$ }% G& ]8 @# O3 X最后,男子汉们,别忘了,谈一场轰轰烈烈的恋爱!! h8 }* m5 z: z
0 ?; e9 P, Z$ }$ U
* S4 N, c" U# b& F% ~3 b 1 ^; D* S2 W! r& ` O # W' h) ?" _, u0 P/ U& [+ l, w2 k6 e- |/ v$ {: s& E
% O" X/ H* B% J
第一章,基本概况 / X$ @- F S, q ' ]& L0 \ r! Y0 Y. Y0 G; b6 TGAMS是一种非常简单易学的运筹学建模语言 (www.gams.com),是由WORLD BANK 开发的。由于近几十年计算机软硬件的的高速发展,世界银行决定投资开发GAMS, 来提供:0 b1 ^- ]1 c* Z. d! O. t) Z; B1 t/ c" W
1. 建立复杂模型的高级语言 5 D+ E5 [! h7 y8 V8 x3 _% v7 K2. 简单易学 : k ]( C: {) \. y B3. 强大的描述代数及逻辑关系 0 x# Y0 ]+ P3 r5 S! ]. `, @0 ~7 k4. 模型可以独立于算法系统4 K- G4 J$ i d+ M5 R
GAMS 可以让使用者专心致力于模型的建立而省略了解法方面的考虑因为GAMS包含了解决通用的LP,NLP,MILP,MINLP等各类模型所需要的solver. 另外,GAMS提供了和外界进行交互的输入输出借口。使得用户很方便的导入导出data。" O6 h0 C. @* f. l6 s
下面当然是tutorial了,就想学习任何一种编程语言一样,最开始都要学习输出 hello world. 我们这里当然要复杂一点了,举一个最简单的例子,运输问题. 8 z6 E, i' t* g) D$ g5 q这是一个经典的LP问题(不用解释什么是LP吧,呵呵): I- m! {: C4 @7 Y" K
用点英语,呵呵, ! X+ X$ x& x- q' v2 zIn the familiar transportation problem, we are given the supplies at several plants and the demands at several markets for a single commodity, and we are given the unit costs of shipping the commodity from plants to markets. The economic question is: how much shipment should there be between each plant and each market so as to minimize total transport cost? 9 I) i! `7 N4 Q, Q. a* c! X1 @就是说,我们已知有一些工厂,生产出的产品要运往一些市场来满足他们的需求,每一个工厂的生产能力(每个工厂能提供产品的数量)以及每个市场的需求(每个市场需要产品的数量)是已知的。另外,我们还知道从特定的工厂到特定的市场,运输一件产品的运费。那么我们怎么样设计从每个工厂到每个市场运送货物的数量,使得总运费最小?' ]2 g6 O! z+ v8 i7 h$ v) Y" Y
Indices: * [; b+ A: o1 h" vi = plants - p( \$ H# G* L4 f) p7 |# [j = markets6 h( O/ m$ d3 u
Given Data:" _* W! d& {/ B$ C
ai = supply of commodity of plant i (in cases)8 m$ y# o$ @1 ^: y
bj = demand for commodity at market j (cases)- ^0 A: c- S5 [/ d) a7 B
cij = cost per unit shipment between plant i and market j ($/case) 4 p, @( O5 P8 g, y& zDecision Variables: . y4 Z6 P' h2 l7 }Xij = amount of commodity to ship from plant i to market j. O' T. X% Y1 X* y, M9 L
在这里唯一的连续变量是Xi,j, 5 {* `1 y1 B) R5 }模型就不用我说了吧。。。。# C4 I/ \6 a! `2 m' G9 R4 E
4 j N X/ Z5 `$ [1 f1 i, N% l这是一个典型的线性规划问题,他的GAMS code is ; g; x2 i; }0 z$ w8 _: _" d" ^1 n2 v. w
Sets + V+ t) ? j. o2 B; e+ w; y+ Ni canning plants / seattle, san-diego /. ]! s1 [" A6 r
j markets / new-york, chicago, topeka / ; # u4 n' j6 t* R' f1 {6 {6 t9 Z9 B. IParameters # ]& L$ d7 D `# va(i) capacity of plant i in cases5 Z- o; m; q/ S U4 F4 _
/ seattle 350* s6 T( |$ j2 y& Y; z
san-diego 600 /) a' d. S7 Q' @3 P2 f
b(j) demand at market j in cases g, }- J% _4 b% y/ new-york 3259 F T) @6 n: M. E
chicago 300 ) E$ `/ D7 c" |- Jtopeka 275 / ; & k0 u4 G3 ?) sTable d(i,j) distance in thousands of miles 6 r$ N3 T! E6 l) S) s) anew-york chicago topeka* U7 {8 n$ E9 n+ e( I+ @
seattle 2.5 1.7 1.8 6 o: \7 k- a- \4 w4 Tsan-diego 2.5 1.8 1.4 ; 1 M! [5 V1 u" t+ a' e7 {Scalar f freight in dollars per case per thousand miles /90/ ;. \2 B7 X' w) M5 H7 ~9 q+ c
Parameter c(i,j) transport cost in thousands of dollars per case ; 0 d. W# b3 T+ j. |9 Kc(i,j) = f * d(i,j) / 1000 ;- ~# X' q5 R/ U8 a. ^
Variables9 x+ x, @- v! c% |8 p1 S! Q3 E# `
x(i,j) shipment quantities in cases/ ?0 U x( K4 K: d# b% T
z total transportation costs in thousands of dollars ; 4 V, @" F8 @' J& H1 bPositive Variable x ; _/ ^% A+ U# N8 sEquations+ V$ ]/ a' [! {+ F
cost define objective function& f. b; _& q/ {0 P5 W y
supply(i) observe supply limit at plant i 7 P; o. [ H( Ddemand(j) satisfy demand at market j ;/ A& P5 t, Y# z1 f* W) ~
cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; + ~6 P% ~( s2 V4 }supply(i) .. sum(j, x(i,j)) =l= a(i) ;* t* B" K! n+ J$ V3 Y r- L+ Q( J$ m
demand(j) .. sum(i, x(i,j)) =g= b(j) ; % M$ O% I( \+ EModel transport /all/ ; 3 ]7 `$ s. _! s2 RSolve transport using lp minimizing z ; . G8 s" A- J9 T7 V, B- TDisplay x.l, x.m ; . y, W! d; q! S这是一个非常典型的LP(linear programming)问题,所有的变量和约束都是线性相关的。