写在前面的话 1 x! o b8 V0 f/ ~1 n R ( F8 X" P) I' s, O9 Q在学习和工作之余写点东西,这种想法由来已久了。只是总是觉得很忙,抽不出时间做一点点自己喜欢做的事情。突然有一天发现,你必须牺牲某些东西才能得到某些东西。所以,就索性写上几句话聊以**吧。! E; d5 O7 B4 @
最开始,只想翻译一下GAMS的用户手册。但是又觉得这样写很枯燥,不光读者不喜欢看,连自己也要打瞌睡了。如果想催眠的话,还不如去听评书。但是本书还是以解释GAMS的用法为主。中间夹杂着一些自己做modelling 的体验,还可能还有一些毫不相关的东西。可能有很多错误,请读者们原谅。想学习GAMS的同学,想必一定是数学,运筹学或计算机相关专业的了。让大家失望了,其实我的专业是chemical engineering, 本科时候做的是量子化学。阴差阳错上了贼船。但是觉得自己用了这么多年了,总该有点体会,能过帮帮大家。 6 {5 b2 ~( F+ N! M 0 P4 ~- R Y4 l ]1 \) u: |9 t进入正题之前,还想啰嗦几句题外的话。 p$ A( P; e2 N7 l1 ?能看到这篇文字的同学,恐怕大多数是在灯下苦读的学子。实际上学习某种工具并不重要。真正要学习的是知识,是思维的方法。就像很多计算机系的学生一样,把数据结构和算法精通了,任何工具都是一样的。所以,你用GAMS或者不用GAMS,都没有关系,要把握事物的本质,最关键的东西抓住了,任何语言就是一种输入法。另外,学习一定要有兴趣,要认识到他的beauty,不要把什么都换算**民币,那样你会觉得很累。除了,coding 以外,还要博览群书,文史哲,拿来翻翻,就当休息了。 9 f8 g, K3 h3 G) t8 V6 z' c最后,男子汉们,别忘了,谈一场轰轰烈烈的恋爱!!1 K+ U2 | u7 `5 N& r* U& n
3 f8 F/ i* S+ j% t
6 A. H5 E( s- n8 i, Z% r+ ^4 m' r2 U! q0 d. U# o2 r
5 T6 X9 x. G6 R1 w2 p: ?! F
1 r x# F0 w. p
3 X7 r( o8 q0 I
第一章,基本概况 # m# A$ I: C' B+ b# S1 y6 p* v u' A) ?4 n N4 Q$ h+ g
GAMS是一种非常简单易学的运筹学建模语言 (www.gams.com),是由WORLD BANK 开发的。由于近几十年计算机软硬件的的高速发展,世界银行决定投资开发GAMS, 来提供: 7 z* C& R( D/ g/ U; ?1. 建立复杂模型的高级语言 9 P" u( k4 H+ v- Y4 i& }2. 简单易学. B; T& J: ]( K. b9 n5 |4 k
3. 强大的描述代数及逻辑关系# y! K, P- {7 X' n5 E
4. 模型可以独立于算法系统' u N+ Y& @: s$ q9 {
GAMS 可以让使用者专心致力于模型的建立而省略了解法方面的考虑因为GAMS包含了解决通用的LP,NLP,MILP,MINLP等各类模型所需要的solver. 另外,GAMS提供了和外界进行交互的输入输出借口。使得用户很方便的导入导出data。( B" }! \# J& Y/ s6 l3 y
下面当然是tutorial了,就想学习任何一种编程语言一样,最开始都要学习输出 hello world. 我们这里当然要复杂一点了,举一个最简单的例子,运输问题.- r! |& ~; T4 O. J5 t' `
这是一个经典的LP问题(不用解释什么是LP吧,呵呵)+ C8 P- x ]4 G% r5 G1 A
用点英语,呵呵, V+ `; J/ E1 h5 _% G. {& L$ G
In 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? / {0 E/ @; |- n0 q) y3 P就是说,我们已知有一些工厂,生产出的产品要运往一些市场来满足他们的需求,每一个工厂的生产能力(每个工厂能提供产品的数量)以及每个市场的需求(每个市场需要产品的数量)是已知的。另外,我们还知道从特定的工厂到特定的市场,运输一件产品的运费。那么我们怎么样设计从每个工厂到每个市场运送货物的数量,使得总运费最小? 7 z! V% ~+ M1 b/ R) ]& c" A3 FIndices:! A/ l# v7 z" i
i = plants) D2 } Q( Q: t" m8 g% A, [0 j
j = markets ! c4 ~9 u" o! R; J- I- k1 i2 D' C! mGiven Data: 3 {2 U+ w J* Y0 L g/ Iai = supply of commodity of plant i (in cases) 3 N( z; i( w/ y W0 I2 P1 Dbj = demand for commodity at market j (cases) . e4 e! `3 q1 g9 qcij = cost per unit shipment between plant i and market j ($/case). a8 ]: r K! R% t. y- u3 n) Q7 T( e
Decision Variables:& V! ^5 ~: V# _' F; Q: n" S4 z% u
Xij = amount of commodity to ship from plant i to market j1 k0 N1 D' ]: j5 m# H/ M
在这里唯一的连续变量是Xi,j, : Q- p0 e9 p1 a: j模型就不用我说了吧。。。。' S8 q9 d8 i# n1 M. @" c, O
& k- }. H* Q- y7 ^# ~9 X这是一个典型的线性规划问题,他的GAMS code is 3 U* e& U9 i i6 M' U- }1 i0 ? U7 P6 y2 N; E) _0 Z. b
Sets ! t- N4 C% I( O# G+ ?( V( w) Ci canning plants / seattle, san-diego / % a7 B$ y- _9 [) K& R' U* Ej markets / new-york, chicago, topeka / ;( w/ ?$ {8 w3 u& O
Parameters: m/ X- @% I: s1 V" I- a
a(i) capacity of plant i in cases9 A$ Z& _4 c, L- l% g: Q
/ seattle 350 4 ]% b% ^0 a( }. {san-diego 600 /7 Z6 R+ Z1 I; [' ?& I C9 W
b(j) demand at market j in cases ' x, {, U# Y: ~( D7 m# F' l/ new-york 325/ S* l1 c2 ^& G6 T' w. u
chicago 300 5 r) f) z8 U$ gtopeka 275 / ; 0 [& j" [0 n. b, [/ h7 {# S) v0 sTable d(i,j) distance in thousands of miles; V) y' |: O1 U2 {" H! W
new-york chicago topeka$ [6 n3 @3 R. \. V
seattle 2.5 1.7 1.89 {* C! _$ k" M1 ]
san-diego 2.5 1.8 1.4 ; 0 b* p7 ?+ n0 h3 b8 q6 CScalar f freight in dollars per case per thousand miles /90/ ; / b, @5 f- z, D! b9 uParameter c(i,j) transport cost in thousands of dollars per case ;) J3 n7 i& a. e3 `
c(i,j) = f * d(i,j) / 1000 ;( K1 S% R( z8 K& F' n& ?
Variables/ d$ O, y2 p$ n, e, [$ X9 ^" U
x(i,j) shipment quantities in cases : Q7 j4 ?2 }1 J# r0 g% r( q# t: Jz total transportation costs in thousands of dollars ; ( k o& f9 a$ ]( {4 A [Positive Variable x ; . a* k8 H9 \2 s4 IEquations G- Y* e- ^8 H, {6 G4 D. H6 n4 Ncost define objective function3 T5 ]/ T+ M7 E! d5 e* {* o
supply(i) observe supply limit at plant i P/ e3 b) l& i6 C: bdemand(j) satisfy demand at market j ; % D% G/ s6 B/ Hcost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;6 d }5 O( }% W4 k$ B
supply(i) .. sum(j, x(i,j)) =l= a(i) ; # W( @' j8 H$ j. W3 l2 qdemand(j) .. sum(i, x(i,j)) =g= b(j) ; . F7 q. [% J7 P# z# b! vModel transport /all/ ;5 p9 e0 v2 Y% A
Solve transport using lp minimizing z ;0 H7 U$ |9 q$ `& z
Display x.l, x.m ; 3 q" C) a9 R5 e这是一个非常典型的LP(linear programming)问题,所有的变量和约束都是线性相关的。