Generated on Mon Aug 25 15:13:13 2008 for Gecode/J by doxygen 1.5.4

Money.java

Go to the documentation of this file.
00001 /* -*- indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Mikael Lagerkvist, 2006
00009  *     Guido Tack, 2006
00010  *
00011  *  Last modified:
00012  *     $Date: 2007-12-05 14:47:42 +0100 (Wed, 05 Dec 2007) $ by $Author: zayenz $
00013  *     $Revision: 5594 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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     // We need 8 variables. Each one gets the name 
00060     // of it's corresponding letter, and an initial domain
00061     // of [0..9]
00062     letters = new VarArray<IntVar>(8);
00063     for (char c: "SENDMORY".toCharArray())
00064       letters.add(new IntVar(this, ""+c, 0, 9));
00065        
00066     // Refer to the letters by name
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     // Initial letters are non-zero
00077     rel(this, s, IRT_NQ, 0, opt.icl); 
00078     rel(this, m, IRT_NQ, 0, opt.icl);
00079 
00080     // Construct linear system representing the equation...
00081     if(opt.naive) {
00082       // ...using coefficient/variable representation of the equation
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       // ...using dynamic Expr-structures
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     // All letters must have distinct values.
00101     distinct(this, letters, opt.icl);
00102 
00103     // Find values for the letters using first-fail heuristic
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 }