, W- l9 i0 s3 E: `# OBinary Variable X(r,c,v) assign value to cell (defined by row and column);& D! G: J0 v3 Y; f4 ~ h
Variable W objectiv value - anything;4 D/ u" k/ g0 `( U" q4 l+ M( j
' Z1 `7 h/ g! a' G* k) e& L0 qequations eq1(r,c) exactly one value for each cell 6 g0 g8 Y- C, l* k- K eq2(c,v) column entries have to be unique ( ^% ]6 X8 t8 j eq3(r,v) row entries have to be unique# d% _ m, d2 r j
eq4(b,v) block entries have to be unique ( k u3 _: _3 a# Z2 K& b/ e nobj definition of objective - anything;9 z8 l9 \3 P: {: l8 B$ |
& @' @. s7 h' \, ]% S7 o5 t5 f' q& rX.fx(r,c,v)$(problem(r,c)=ord(v)) = 1;" w: P: j# w' y
' Y' v4 K+ ~- U1 k J/ ^5 \
eq1(r,c).. sum(v, X(r,c,v)) =E= 1; ; ]2 v6 u) o/ z7 d: Q9 V2 o) d. `eq2(c,v).. sum(r, X(r,c,v)) =E= 1;# ^: N. q+ B$ g* ^+ m/ B
eq3(r,v).. sum(c, X(r,c,v)) =E= 1;+ a4 F% r' z) r3 A; F8 t* r: r. V
eq4(b,v).. sum(brc(b,r,c), X(r,c,v)) =E= 1;! ]9 l! r7 P# q6 {5 O5 _
nobj.. W =E= sum((r,c,v), X(r,c,v)); $ d3 r! i5 R) G W, x3 s. X/ R# Nmodel sudoku / all /; : I* s# X1 x+ [. X: |3 v, c# x& \" a7 }3 F/ }5 L) q9 t: C! C
solve sudoku minimizing w using mip; ( N# N8 A5 f( N最后说一句,其实这个模型不用求什么最大还是最小值,只要得到一个满足所有constraints的feasible solution 就可以了,所以nobj的注释写的"anything"。