money.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2001 00008 * 00009 * Last modified: 00010 * $Date: 2010-05-08 12:56:03 +0200 (Sat, 08 May 2010) $ by $Author: tack $ 00011 * $Revision: 10906 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 #include <gecode/driver.hh> 00039 #include <gecode/int.hh> 00040 #include <gecode/minimodel.hh> 00041 00042 using namespace Gecode; 00043 00053 class Money : public Script { 00054 protected: 00056 static const int nl = 8; 00058 IntVarArray le; 00059 public: 00061 enum { 00062 MODEL_SINGLE, 00063 MODEL_CARRY 00064 }; 00066 Money(const Options& opt) : le(*this,nl,0,9) { 00067 IntVar 00068 s(le[0]), e(le[1]), n(le[2]), d(le[3]), 00069 m(le[4]), o(le[5]), r(le[6]), y(le[7]); 00070 00071 rel(*this, s, IRT_NQ, 0); 00072 rel(*this, m, IRT_NQ, 0); 00073 00074 distinct(*this, le, opt.icl()); 00075 00076 switch (opt.model()) { 00077 case MODEL_SINGLE: 00078 rel(*this, 1000*s+100*e+10*n+d 00079 + 1000*m+100*o+10*r+e 00080 == 10000*m+1000*o+100*n+10*e+y, 00081 opt.icl()); 00082 break; 00083 case MODEL_CARRY: 00084 { 00085 IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1), c3(*this,0,1); 00086 rel(*this, d+e == y+10*c0, opt.icl()); 00087 rel(*this, c0+n+r == e+10*c1, opt.icl()); 00088 rel(*this, c1+e+o == n+10*c2, opt.icl()); 00089 rel(*this, c2+s+m == o+10*c3, opt.icl()); 00090 rel(*this, c3 == m, opt.icl()); 00091 } 00092 break; 00093 default: GECODE_NEVER; 00094 } 00095 00096 branch(*this, le, INT_VAR_SIZE_MIN, INT_VAL_MIN); 00097 } 00099 virtual void 00100 print(std::ostream& os) const { 00101 os << "\t" << le << std::endl; 00102 } 00103 00105 Money(bool share, Money& s) : Script(share,s) { 00106 le.update(*this, share, s.le); 00107 } 00109 virtual Space* 00110 copy(bool share) { 00111 return new Money(share,*this); 00112 } 00113 }; 00114 00118 int 00119 main(int argc, char* argv[]) { 00120 Options opt("SEND+MORE=MONEY"); 00121 opt.model(Money::MODEL_SINGLE); 00122 opt.model(Money::MODEL_SINGLE, "single", "use single linear equation"); 00123 opt.model(Money::MODEL_CARRY, "carry", "use carry"); 00124 opt.solutions(0); 00125 opt.iterations(20000); 00126 opt.parse(argc,argv); 00127 Script::run<Money,DFS,Options>(opt); 00128 return 0; 00129 } 00130 00131 // STATISTICS: example-any 00132