5 \2 p z5 Q9 M. N+ }. w: W4 ^8 K8 c/ A8 |! [8 U/ @
, l' l0 G7 U8 n6 [0 \8 T
2 B' f/ p; L( U3 c4 B$ }
6 g' g: L% a$ p Y* ?9 Z3 L7 v 8 Q. o5 X$ i' {" R% p$ {- s第一章,基本概况: I9 y9 \ B& w# u
6 W# I8 [3 \4 H* e2 l5 ^: [# P( KGAMS是一种非常简单易学的运筹学建模语言 (www.gams.com),是由WORLD BANK 开发的。由于近几十年计算机软硬件的的高速发展,世界银行决定投资开发GAMS, 来提供: & P4 n D3 c3 F1. 建立复杂模型的高级语言 " v( v* R: a9 b, l$ g; {2. 简单易学 2 |9 x- Z, _! D2 M8 m3. 强大的描述代数及逻辑关系 . x# `4 W( z" W$ P; n& y4. 模型可以独立于算法系统 5 o( ?% K& U- h! k3 }0 BGAMS 可以让使用者专心致力于模型的建立而省略了解法方面的考虑因为GAMS包含了解决通用的LP,NLP,MILP,MINLP等各类模型所需要的solver. 另外,GAMS提供了和外界进行交互的输入输出借口。使得用户很方便的导入导出data。% a' t5 X5 P- ]
下面当然是tutorial了,就想学习任何一种编程语言一样,最开始都要学习输出 hello world. 我们这里当然要复杂一点了,举一个最简单的例子,运输问题., @6 V6 Z5 [8 {) M' W5 w( A# x
这是一个经典的LP问题(不用解释什么是LP吧,呵呵) : x+ l6 m% W7 v( s7 C$ B' m* G用点英语,呵呵,( w- W3 ]" K1 y% @
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?$ \ h. l) S; W2 K
就是说,我们已知有一些工厂,生产出的产品要运往一些市场来满足他们的需求,每一个工厂的生产能力(每个工厂能提供产品的数量)以及每个市场的需求(每个市场需要产品的数量)是已知的。另外,我们还知道从特定的工厂到特定的市场,运输一件产品的运费。那么我们怎么样设计从每个工厂到每个市场运送货物的数量,使得总运费最小? : b9 j$ o, A8 PIndices: , f4 |7 s+ _6 W: v2 l9 v1 i' ii = plants5 I/ F4 h+ I( e: }' ^
j = markets * u1 W2 a$ S7 }, L7 ^ G, YGiven Data: + C( D& @. z# ~ w( {# a7 S+ F4 Kai = supply of commodity of plant i (in cases) R& }2 L9 k9 d1 M) xbj = demand for commodity at market j (cases) 2 S: F! O: N ]: \' ecij = cost per unit shipment between plant i and market j ($/case) ! \1 n* J5 ]/ t: @& n; LDecision Variables:1 H( r# z* l+ C9 E1 a# W$ }& U( l
Xij = amount of commodity to ship from plant i to market j: A9 S" r: O2 h( |
在这里唯一的连续变量是Xi,j, 6 }' d3 f3 j- l: Q+ \1 H6 A模型就不用我说了吧。。。。 Z" R" W$ Q; G: {' G; s+ V) j. k C. N$ u
3 F7 v' R" c* E0 }9 U! |这是一个典型的线性规划问题,他的GAMS code is 9 l- ^9 q4 ?$ q8 Z! t: ] 2 D$ z1 o5 m# E x5 P% ISets % r1 e; _/ l8 S# m: _" R6 Ii canning plants / seattle, san-diego / 7 ~9 v* O" h$ l. S* zj markets / new-york, chicago, topeka / ; 6 P6 t: i, X9 F1 o6 SParameters3 B; z1 B8 f1 M! p1 _, p
a(i) capacity of plant i in cases ; i- T# E" `4 g/ seattle 350/ E/ G1 A4 q, F3 B$ n
san-diego 600 / ' r, w, |2 E0 D* Ub(j) demand at market j in cases / {% d' [4 V# ]& M' Y; u/ new-york 325" O- Q2 i8 S, Y* {+ C7 w& h. o5 E
chicago 3006 O6 X* w- }, e0 z5 w: r
topeka 275 / ; 2 v2 x% n) v uTable d(i,j) distance in thousands of miles/ w. A' }. L v
new-york chicago topeka: [( F- [- @, }9 Z1 z" }6 y
seattle 2.5 1.7 1.8 * m) Q1 s" c5 o; u+ zsan-diego 2.5 1.8 1.4 ; ! {- ?1 H: A c& TScalar f freight in dollars per case per thousand miles /90/ ; - J1 R! W3 k ?Parameter c(i,j) transport cost in thousands of dollars per case ; , @! x: f( a2 C D# sc(i,j) = f * d(i,j) / 1000 ; 2 g* L. ~4 e- x, g% tVariables % D" G) W# Y# Px(i,j) shipment quantities in cases! w" ~' R/ e, k9 H5 u) [7 o: _6 E
z total transportation costs in thousands of dollars ;: C1 G5 i# @; x& T3 s. v" C
Positive Variable x ;- V3 M* J V- G- @
Equations5 q. I2 N1 W; m I7 x- e
cost define objective function ) x; E" N- O% wsupply(i) observe supply limit at plant i $ t: f( O0 i* Cdemand(j) satisfy demand at market j ;2 I4 [7 k: e+ ?* D. I" W0 @, P
cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;6 [; r0 E2 k0 A
supply(i) .. sum(j, x(i,j)) =l= a(i) ; 8 T, Q7 T1 M) {demand(j) .. sum(i, x(i,j)) =g= b(j) ; 0 v: y: ?) r& m0 ?8 ?9 H( r0 J% V, |Model transport /all/ ;: ] B; ^1 X- D+ E$ E3 b. U
Solve transport using lp minimizing z ;5 N( d& f/ f9 N- h
Display x.l, x.m ;: o* M! G( {) L& O7 N" O
这是一个非常典型的LP(linear programming)问题,所有的变量和约束都是线性相关的。