, A! J: X2 i* H" [5 Y, SGAMS是一种非常简单易学的运筹学建模语言 (www.gams.com),是由WORLD BANK 开发的。由于近几十年计算机软硬件的的高速发展,世界银行决定投资开发GAMS, 来提供:5 D4 w" }3 k9 d& f
1. 建立复杂模型的高级语言 7 m4 H3 {: d# E) f0 P7 v2. 简单易学: n2 N* X) d" L, B: T8 @
3. 强大的描述代数及逻辑关系* w1 E5 t& M( Y' ?( H( q/ y
4. 模型可以独立于算法系统 + }, r Q8 Y8 kGAMS 可以让使用者专心致力于模型的建立而省略了解法方面的考虑因为GAMS包含了解决通用的LP,NLP,MILP,MINLP等各类模型所需要的solver. 另外,GAMS提供了和外界进行交互的输入输出借口。使得用户很方便的导入导出data。+ h$ |* Y' M, \. v, H2 o- G
下面当然是tutorial了,就想学习任何一种编程语言一样,最开始都要学习输出 hello world. 我们这里当然要复杂一点了,举一个最简单的例子,运输问题.9 G7 o- A: Z! `
这是一个经典的LP问题(不用解释什么是LP吧,呵呵) 6 r# [6 m% y; D' V用点英语,呵呵,3 D8 X8 n* h8 G7 _* Q6 I5 e
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?- u% K6 U/ F- |0 v% e/ v% x
就是说,我们已知有一些工厂,生产出的产品要运往一些市场来满足他们的需求,每一个工厂的生产能力(每个工厂能提供产品的数量)以及每个市场的需求(每个市场需要产品的数量)是已知的。另外,我们还知道从特定的工厂到特定的市场,运输一件产品的运费。那么我们怎么样设计从每个工厂到每个市场运送货物的数量,使得总运费最小? u3 z: _9 `6 }( E: DIndices: . z) x+ o" O& O0 [9 ~i = plants( ]: O, b# _2 G
j = markets $ t3 @$ a4 ~2 S, _& U5 uGiven Data: 3 P; Q# E# c1 Mai = supply of commodity of plant i (in cases)6 u/ J2 w, q! L' l( f5 M' K C! W+ g2 h
bj = demand for commodity at market j (cases)0 S, r9 r; ~" U. e P% S; ?
cij = cost per unit shipment between plant i and market j ($/case) , K' T/ h% z# u) t" LDecision Variables: * d: z P4 q3 c1 @8 \Xij = amount of commodity to ship from plant i to market j 0 {" m2 d% e6 _8 j4 Y9 l2 y在这里唯一的连续变量是Xi,j, $ h- B$ w' _) n: l, {" n模型就不用我说了吧。。。。 7 J5 y8 r; i* w: l4 n# p6 \ & p. f7 ~6 C% @6 E4 I2 j/ o4 ]这是一个典型的线性规划问题,他的GAMS code is* ]3 F; K x+ R4 w
( B" J1 P$ s- C2 N7 GSets 2 i9 Y6 \ N4 _i canning plants / seattle, san-diego / 0 c: ?$ ~* u) p J& Ij markets / new-york, chicago, topeka / ; + c |5 m& }4 FParameters 0 J' C/ v# Y5 a$ F) N$ \+ va(i) capacity of plant i in cases% o- y; D) n1 I2 |" H, u
/ seattle 3508 I- l4 D) w& A
san-diego 600 / , Z: o# \" W, k5 ?% L& t3 Ub(j) demand at market j in cases+ s% I0 F1 z: Y$ F% G0 z, P
/ new-york 325 4 r- o# o- \; ^3 V* ^% v, R& w- \chicago 300/ u; |% C A7 z1 T' U, z
topeka 275 / ;/ _2 Q3 _0 C* w& n* Y% ]: ~7 A
Table d(i,j) distance in thousands of miles " q- c5 P+ B6 j p. f1 ^/ z2 @new-york chicago topeka6 U4 K& n: K/ g
seattle 2.5 1.7 1.8 % B2 s% o: V$ g1 g. {+ B6 o9 Ysan-diego 2.5 1.8 1.4 ;( H5 N4 S: E1 M6 C) A
Scalar f freight in dollars per case per thousand miles /90/ ; 8 v- {; z) {" G# C/ {/ fParameter c(i,j) transport cost in thousands of dollars per case ;7 _. ?% e! F+ v1 U# n/ s
c(i,j) = f * d(i,j) / 1000 ; % v8 @) o: z/ N! KVariables s4 I8 |8 F$ v& t3 j* t5 }
x(i,j) shipment quantities in cases3 x) U& f; H3 u$ M' d
z total transportation costs in thousands of dollars ;& }- P: {$ z7 y* c/ U+ {0 C7 `# q
Positive Variable x ;* w3 J2 a0 ~5 b4 r; P6 ]
Equations- x3 U/ J- r; N
cost define objective function4 ?7 C4 u! ]! P, Y- _# ^; \9 M* u- H
supply(i) observe supply limit at plant i$ M! w+ V) q$ _
demand(j) satisfy demand at market j ; 7 [$ t+ m! R: N; y1 ]" c+ jcost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;$ O- V9 D: e2 N5 W# } _
supply(i) .. sum(j, x(i,j)) =l= a(i) ;% w! u! n& K& P
demand(j) .. sum(i, x(i,j)) =g= b(j) ;, R5 A! F& A% L- W6 Y" W/ u, X$ e+ Y
Model transport /all/ ; 2 V+ ~; m0 @* B9 _) G* E( `5 PSolve transport using lp minimizing z ; * E1 N2 E' Y/ T+ n' b6 RDisplay x.l, x.m ; + e1 x4 C6 Z$ A. H F* A" ]这是一个非常典型的LP(linear programming)问题,所有的变量和约束都是线性相关的。