00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 package examples;
00041
00042 import static org.gecode.Gecode.*;
00043 import static org.gecode.GecodeEnumConstants.*;
00044
00045 import org.gecode.*;
00046
00051 public class Money extends Space {
00052 public VarArray<IntVar> letters;
00053
00056 public Money(Options opt) {
00057 super("Money");
00058
00059
00060
00061
00062 letters = new VarArray<IntVar>(8);
00063 for (char c: "SENDMORY".toCharArray())
00064 letters.add(new IntVar(this, ""+c, 0, 9));
00065
00066
00067 IntVar s = letters.get(0);
00068 IntVar e = letters.get(1);
00069 IntVar n = letters.get(2);
00070 IntVar d = letters.get(3);
00071 IntVar m = letters.get(4);
00072 IntVar o = letters.get(5);
00073 IntVar r = letters.get(6);
00074 IntVar y = letters.get(7);
00075
00076
00077 rel(this, s, IRT_NQ, 0, opt.icl);
00078 rel(this, m, IRT_NQ, 0, opt.icl);
00079
00080
00081 if(opt.naive) {
00082
00083 int c[] = { 1000, 100, 10, 1,
00084 1000, 100, 10, 1,
00085 -10000, -1000, -100, -10, -1};
00086 VarArray<IntVar> x =
00087 new VarArray<IntVar>(s, e, n, d, m, o, r, e,
00088 m, o, n, e, y);
00089 linear(this, c, x, IRT_EQ, 0, opt.icl);
00090 } else {
00091
00092 post(this,
00093 new Expr()
00094 .p(1000, s).p(100, e).p(10, n).p(d)
00095 .p(1000, m).p(100, o).p(10, r).p(e),
00096 IRT_EQ,
00097 new Expr()
00098 .p(10000, m).p(1000, o).p(100, n).p(10, e).p(y));
00099 }
00100
00101 distinct(this, letters, opt.icl);
00102
00103
00104 branch(this, letters, INT_VAR_SIZE_MIN, INT_VAL_MIN);
00105 }
00106
00109 public Money(Boolean share, Money money) {
00110 super(share, money);
00111 letters = new VarArray<IntVar>(this, share, money.letters);
00112 }
00113
00116 public static void main(String[] args) {
00117 Options opt = new Options("SEND+MORE=MONEY");
00118 opt.gui = true;
00119 opt.naive = false;
00120 opt.icl = ICL_BND;
00121 opt.parse(args);
00122
00123 Money money = new Money(opt);
00124 opt.doSearch(money);
00125 }
00126 }