/ A, z& K6 @7 R. l+ N6 W' A在学习和工作之余写点东西,这种想法由来已久了。只是总是觉得很忙,抽不出时间做一点点自己喜欢做的事情。突然有一天发现,你必须牺牲某些东西才能得到某些东西。所以,就索性写上几句话聊以**吧。 % _5 S/ f B$ X9 K最开始,只想翻译一下GAMS的用户手册。但是又觉得这样写很枯燥,不光读者不喜欢看,连自己也要打瞌睡了。如果想催眠的话,还不如去听评书。但是本书还是以解释GAMS的用法为主。中间夹杂着一些自己做modelling 的体验,还可能还有一些毫不相关的东西。可能有很多错误,请读者们原谅。想学习GAMS的同学,想必一定是数学,运筹学或计算机相关专业的了。让大家失望了,其实我的专业是chemical engineering, 本科时候做的是量子化学。阴差阳错上了贼船。但是觉得自己用了这么多年了,总该有点体会,能过帮帮大家。' q2 d9 [ Q4 P0 X7 e1 ^
; L- S( p7 o9 u q进入正题之前,还想啰嗦几句题外的话。6 D5 Q; g9 e9 [: K: I2 d
能看到这篇文字的同学,恐怕大多数是在灯下苦读的学子。实际上学习某种工具并不重要。真正要学习的是知识,是思维的方法。就像很多计算机系的学生一样,把数据结构和算法精通了,任何工具都是一样的。所以,你用GAMS或者不用GAMS,都没有关系,要把握事物的本质,最关键的东西抓住了,任何语言就是一种输入法。另外,学习一定要有兴趣,要认识到他的beauty,不要把什么都换算**民币,那样你会觉得很累。除了,coding 以外,还要博览群书,文史哲,拿来翻翻,就当休息了。 * F+ ?+ Q) }/ X* o1 U' S最后,男子汉们,别忘了,谈一场轰轰烈烈的恋爱!! ! m+ c7 Q/ S4 y . l8 b- `; [& ] @- k. I # ]7 S+ w% X5 r& z' X3 \( O( X: w2 Q* C F( Y8 Y5 y3 {
# a; s( x$ G D9 J6 L
" m/ j( A" O& y% g R 0 }$ M/ j4 J$ c3 o3 o4 M, q) Y5 G第一章,基本概况 * Q- j9 q/ B1 S- F/ @+ Q5 a! T( D3 N3 X9 W 1 v) g7 S2 N2 R; w! X! NGAMS是一种非常简单易学的运筹学建模语言 (www.gams.com),是由WORLD BANK 开发的。由于近几十年计算机软硬件的的高速发展,世界银行决定投资开发GAMS, 来提供: . a' m7 q) h1 h; _& e+ n1. 建立复杂模型的高级语言# O1 T9 {6 w$ i3 Y. T
2. 简单易学- S2 x8 \. i( b/ {
3. 强大的描述代数及逻辑关系# x2 M1 f1 `9 f3 w7 W
4. 模型可以独立于算法系统. B9 H5 G) f0 n4 J
GAMS 可以让使用者专心致力于模型的建立而省略了解法方面的考虑因为GAMS包含了解决通用的LP,NLP,MILP,MINLP等各类模型所需要的solver. 另外,GAMS提供了和外界进行交互的输入输出借口。使得用户很方便的导入导出data。 7 ^, x4 R9 ]' g下面当然是tutorial了,就想学习任何一种编程语言一样,最开始都要学习输出 hello world. 我们这里当然要复杂一点了,举一个最简单的例子,运输问题.3 W) R: t* T4 H
这是一个经典的LP问题(不用解释什么是LP吧,呵呵)+ N. c1 U; T, O# p# A4 g
用点英语,呵呵, 0 d f$ d2 G- Z/ \2 C" u G$ uIn 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 N+ Q' z5 H& |3 d6 v# E! m, v就是说,我们已知有一些工厂,生产出的产品要运往一些市场来满足他们的需求,每一个工厂的生产能力(每个工厂能提供产品的数量)以及每个市场的需求(每个市场需要产品的数量)是已知的。另外,我们还知道从特定的工厂到特定的市场,运输一件产品的运费。那么我们怎么样设计从每个工厂到每个市场运送货物的数量,使得总运费最小?6 N3 \ L; S! E4 }4 g$ d
Indices: 0 p" Q5 _) y2 Q! q5 W5 {: xi = plants# _% H3 _0 @# i" P0 M' h0 J, m
j = markets + I g* s, u" g8 p w! r! J. @8 pGiven Data: / ^ E+ K. \. ^ai = supply of commodity of plant i (in cases) p7 O* C+ J8 cbj = demand for commodity at market j (cases)2 m" {% o; }/ s# e
cij = cost per unit shipment between plant i and market j ($/case) & c* m3 c1 V1 l6 [! B9 }5 cDecision Variables:- k8 |7 j6 N" U7 {
Xij = amount of commodity to ship from plant i to market j 8 X- F- l/ z& E7 T* P在这里唯一的连续变量是Xi,j, ; C( F- O$ Y) ~7 ?4 a1 _模型就不用我说了吧。。。。% Z; z. _3 O1 p/ `5 G) X" M
0 l3 T# b `* l+ J
这是一个典型的线性规划问题,他的GAMS code is2 k5 l/ e! y! `6 n9 y+ }
_; l9 [: A8 L1 C5 jSets2 `+ U7 B& U' n" R; p# c. d8 u
i canning plants / seattle, san-diego / , L0 a2 t( k/ I: J% f" V+ \j markets / new-york, chicago, topeka / ; ) d% i" z, W5 Z, Y) }3 aParameters $ u$ K' N3 ? Y U# Ua(i) capacity of plant i in cases+ `' r d3 | T) ~
/ seattle 350 7 m: n3 n4 A+ M! V& R% I/ v$ O; X. wsan-diego 600 / % S, f0 g5 A% ^ O# l1 z6 m' V9 s- @b(j) demand at market j in cases 8 B$ D3 H% e k# @/ new-york 325% N7 O3 V" i# S- G# v$ z1 m p
chicago 300 k' m: n% {: V" Z
topeka 275 / ; # }5 |7 d+ _( d$ p$ Z x8 f/ jTable d(i,j) distance in thousands of miles; q% I4 x, Y0 f8 ?% c& _; \" d; ]0 w
new-york chicago topeka; @4 l1 u2 r) U$ L
seattle 2.5 1.7 1.8' n$ i/ |: C: h) Y2 X
san-diego 2.5 1.8 1.4 ; % |9 G6 y( T5 U; v! f" {Scalar f freight in dollars per case per thousand miles /90/ ; ) M; ?0 ?8 N% W) {4 jParameter c(i,j) transport cost in thousands of dollars per case ;, i I/ z+ G/ [: p+ `' x
c(i,j) = f * d(i,j) / 1000 ;/ q/ ^( P- G) k$ d$ }3 |
Variables G( x! ~# S$ e9 w: x( |$ j
x(i,j) shipment quantities in cases+ g0 o* t- I/ q% Y, G* F
z total transportation costs in thousands of dollars ; * d3 L4 ]; T# \) Z4 q, f/ pPositive Variable x ; ! s; P1 d. r8 f! M' D* _% zEquations- ?; w0 ~$ d# Q9 A( G# e# U
cost define objective function ) G* J! F! M- B6 O4 ~: Y4 rsupply(i) observe supply limit at plant i 6 l+ ?1 V$ l2 s6 {- E$ ?demand(j) satisfy demand at market j ; " n) e+ n- z% L1 M& _6 N. bcost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;! V! X9 B8 G% b, U3 C! ~3 C* x
supply(i) .. sum(j, x(i,j)) =l= a(i) ;, V0 a K: B! P6 d
demand(j) .. sum(i, x(i,j)) =g= b(j) ; 7 h0 W; w# ~! v/ h7 OModel transport /all/ ;' O% x8 D- i6 a" v. w
Solve transport using lp minimizing z ;+ U. |) W( n" H0 W1 S( t% B% K' Q
Display x.l, x.m ; : P% {; D9 a) g) R* J1 F9 o这是一个非常典型的LP(linear programming)问题,所有的变量和约束都是线性相关的。