% b2 ?$ Z; F s% v% Q+ H进入正题之前,还想啰嗦几句题外的话。 * R/ M+ F4 G' a. ] E. C9 g能看到这篇文字的同学,恐怕大多数是在灯下苦读的学子。实际上学习某种工具并不重要。真正要学习的是知识,是思维的方法。就像很多计算机系的学生一样,把数据结构和算法精通了,任何工具都是一样的。所以,你用GAMS或者不用GAMS,都没有关系,要把握事物的本质,最关键的东西抓住了,任何语言就是一种输入法。另外,学习一定要有兴趣,要认识到他的beauty,不要把什么都换算**民币,那样你会觉得很累。除了,coding 以外,还要博览群书,文史哲,拿来翻翻,就当休息了。, p& R. Y; s$ p. m5 ^& d! X$ o
最后,男子汉们,别忘了,谈一场轰轰烈烈的恋爱!!) N: |. V* a6 O8 d9 C
^# q6 Y6 H# n& K7 W3 b 2 k( F/ p5 P* }" C$ M + i) P; B5 S3 O l( s- c/ o. C; f2 @" l8 N% G1 s
( e! S; W( Y3 N+ C! s! N& ?( a6 U+ _ c+ p% z+ i
第一章,基本概况3 J+ N4 a3 A+ O4 S# E
7 w; h( ~) f* }$ G( ?" n
GAMS是一种非常简单易学的运筹学建模语言 (www.gams.com),是由WORLD BANK 开发的。由于近几十年计算机软硬件的的高速发展,世界银行决定投资开发GAMS, 来提供:# ]; a0 ~# _8 r, N
1. 建立复杂模型的高级语言/ i, K* T. e1 E; q, x" q! d# n1 Y7 ^
2. 简单易学0 K4 ]2 S' A: r, b( l# P5 w
3. 强大的描述代数及逻辑关系 ( }) J: G& `9 b4. 模型可以独立于算法系统 & I1 z, ~4 I$ \' k4 F% _GAMS 可以让使用者专心致力于模型的建立而省略了解法方面的考虑因为GAMS包含了解决通用的LP,NLP,MILP,MINLP等各类模型所需要的solver. 另外,GAMS提供了和外界进行交互的输入输出借口。使得用户很方便的导入导出data。! E+ q( M0 Y: t
下面当然是tutorial了,就想学习任何一种编程语言一样,最开始都要学习输出 hello world. 我们这里当然要复杂一点了,举一个最简单的例子,运输问题." W- r: `: F' K' ]
这是一个经典的LP问题(不用解释什么是LP吧,呵呵) . j2 ~) K" J7 J用点英语,呵呵, 5 D# s" ]3 X$ X/ C; ]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? 8 I6 q8 m7 S* |- s9 s就是说,我们已知有一些工厂,生产出的产品要运往一些市场来满足他们的需求,每一个工厂的生产能力(每个工厂能提供产品的数量)以及每个市场的需求(每个市场需要产品的数量)是已知的。另外,我们还知道从特定的工厂到特定的市场,运输一件产品的运费。那么我们怎么样设计从每个工厂到每个市场运送货物的数量,使得总运费最小? 6 ?! X0 x3 Z+ J2 OIndices: : b! m- ?, r. ~& P' Q6 ?i = plants- j0 I. Y6 M" I
j = markets: L2 g5 F7 e' U; V5 W
Given Data:& I) l, U; Q1 k) R
ai = supply of commodity of plant i (in cases) B4 x0 m1 k7 L2 Nbj = demand for commodity at market j (cases) 9 Y* @& }2 u l2 M1 ~! qcij = cost per unit shipment between plant i and market j ($/case) % E9 H. A7 p- DDecision Variables:3 i& W' |9 I" K- t- F" y. @5 D
Xij = amount of commodity to ship from plant i to market j7 m a3 B1 k) M5 D' A
在这里唯一的连续变量是Xi,j, 6 q$ ?) a4 R* C模型就不用我说了吧。。。。 $ v1 `& i+ S4 W8 n6 u% V ( D7 X4 f0 k6 [' P& s这是一个典型的线性规划问题,他的GAMS code is 2 s! p& V9 M9 o3 i6 \$ |# o 1 l" _7 Q/ O5 Q6 a& T" h! fSets/ P. h# r0 ?: s, l$ g2 i
i canning plants / seattle, san-diego / ; [: _8 {! }. {j markets / new-york, chicago, topeka / ; + |' t- B7 K. }. c, [5 hParameters " D# z6 w: r- `a(i) capacity of plant i in cases 5 l& b1 I9 u9 j/ seattle 350 C( X+ o) q. K" C! Csan-diego 600 / ! F/ Z2 \4 u1 N5 G& T5 Xb(j) demand at market j in cases6 S1 Z- g% g- a# k3 A. O$ U$ j& l3 _
/ new-york 3254 K/ c) {; C0 @% M4 v
chicago 300- k2 H+ j+ o3 D
topeka 275 / ;' H) c8 L! @; b
Table d(i,j) distance in thousands of miles 4 Q, U) j- @) f1 @/ S7 ^4 @ n* enew-york chicago topeka0 P* o1 [0 R: t/ ?% U6 D2 C# A
seattle 2.5 1.7 1.8 9 H/ f0 b* O% F) [san-diego 2.5 1.8 1.4 ;& t z) ^# d8 I% C% R, N
Scalar f freight in dollars per case per thousand miles /90/ ;9 q& e' c; C9 L4 e+ z
Parameter c(i,j) transport cost in thousands of dollars per case ;/ R( ~4 k' C4 F! o6 {2 h
c(i,j) = f * d(i,j) / 1000 ; 2 W( \- d) K. }* n3 x% Y7 tVariables* k# O% @5 @& X1 L" k6 M( U
x(i,j) shipment quantities in cases$ U" |, w9 v/ \5 i4 o& m% e
z total transportation costs in thousands of dollars ; 9 z. H! w0 G. y& k; q$ ~2 X+ aPositive Variable x ;8 j$ z& Y# b, {/ c+ q. ^
Equations 7 u8 b0 r9 b2 e9 K; ?6 Wcost define objective function A, s0 i! Z0 _" G. Jsupply(i) observe supply limit at plant i$ R8 o1 W2 t9 i5 ~; J6 h
demand(j) satisfy demand at market j ; 7 t7 a x2 T* z5 h4 p2 P% L( R. r; ecost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; $ s( V4 H/ T" g8 B3 Jsupply(i) .. sum(j, x(i,j)) =l= a(i) ;$ j$ }7 G L: v9 f I: e
demand(j) .. sum(i, x(i,j)) =g= b(j) ; 2 k. m9 {& ^" \# jModel transport /all/ ;3 e1 M. D+ Z4 R9 O# y
Solve transport using lp minimizing z ; A* h% S6 o1 M! {Display x.l, x.m ; ( p; O2 ~: k$ D& E. n+ [这是一个非常典型的LP(linear programming)问题,所有的变量和约束都是线性相关的。