随便发一个sudoku的gams代码。最主要的是请大家理解set 里面mapping 的写法。' G# D: f5 G* \: ?6 e3 [1 Y8 I
Sets r rows / r1*r9 /. i7 v P( l$ Z( T- T# n
c columns / c1*c9 / H4 q; J8 F2 W* b: S+ k) \; T b blocks / b1*b9 / 2 d1 w4 D& s% ? v values / v1*v9 /1 b- d/ |) v, Z8 n; W. |/ C' h
br(b,r) / b1*b3 .r1*r3, b4*b6 .r4*r6, b7*b9 .r7*r9 /8 ]6 ~9 E/ O) W9 o
bc(b,c) / (b1,b4,b7).c1*c3, (b2,b5,b8).c4*c6, (b3,b6,b9).c7*c9 / " i0 A" ~+ |2 D4 I brc(b,r,c) block definitions ;; P, x; b$ u) A: a! Z( U
) T. H, V" k. Z2 p4 K
brc(b,r,c) = br(b,r)*bc(b,c); 2 A, \: m0 Y$ T2 o# x/ s # O" P& b& t+ s+ E0 Q( `Table problem(r,c) Hard problem with non-unique solution# ], z! }4 }2 ?- V3 ^% i( p. @6 `
c1 c2 c3 c4 c5 c6 c7 c8 c9# v, h8 i. I+ j
r1 2 6 7 ' F- ~9 f1 u% p7 Z& kr2 6 2 / F2 C( y# Y' tr3 4 8 11 V2 V6 K K& E. `2 D0 n
r4 5 9 3+ s* q" P+ X9 r, w( t( E
r5 3 53 W, [: L) o+ K, |1 n* K$ V
r6 2 8 7 : d, s2 q4 Q; Y# i, P9 Dr7 1 + `$ i" K" l! u9 b2 O, S- Yr8 7 8 6 " J- a9 r ` v" |" gr9 5 3 8 ;( s9 _1 _6 {1 P. z
: a8 K+ {3 [, U' \6 W% T, C `( N7 T+ {7 u7 Q
Binary Variable X(r,c,v) assign value to cell (defined by row and column);" U% Q6 {" t0 J4 Y( e& d
Variable W objectiv value - anything; ' G1 |8 G0 X: f% R# J1 h ! j" S) Y2 N$ U6 ^( Y" \equations eq1(r,c) exactly one value for each cell ( S6 S5 C; f* y3 P eq2(c,v) column entries have to be unique8 o0 k+ N2 x0 P2 _" I
eq3(r,v) row entries have to be unique$ r3 Y8 V. `4 J2 K3 L3 I4 B& r2 Z
eq4(b,v) block entries have to be unique , |1 m3 N0 r, | nobj definition of objective - anything; ) ]# v; \$ e8 ` % }+ F' e' {) R$ U5 I+ H! vX.fx(r,c,v)$(problem(r,c)=ord(v)) = 1;: T% a h8 W7 j7 L" X! s- Y