Generated on Mon Aug 25 11:35:32 2008 for Gecode by doxygen 1.5.6

magic-sequence.cc

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  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2001
00009  *     Guido Tack, 2006
00010  *
00011  *  Last modified:
00012  *     $Date: 2007-11-30 13:58:34 +0100 (Fri, 30 Nov 2007) $ by $Author: tack $
00013  *     $Revision: 5524 $
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 #include "examples/support.hh"
00041 #include "gecode/minimodel.hh"
00042 
00057 class MagicSequence : public Example {
00058 private:
00060   const int n;
00062   IntVarArray s;
00063 public:
00065   enum {
00066     PROP_REIFIED, 
00067     PROP_COUNT,   
00068     PROP_GCC      
00069   };
00071   void
00072   exactly(IntVarArray& v, IntVar& x, int i) {
00073     // I occurs in V X times
00074     BoolVarArgs b(v.size());
00075     for (int j = v.size(); j--; )
00076       b[j] = post(this, ~(v[j] == i));
00077     linear(this, b, IRT_EQ, x);
00078   }
00080   MagicSequence(const SizeOptions& opt)
00081     : n(opt.size()), s(this,n,0,n-1) {
00082     switch (opt.propagation()) {
00083     case PROP_REIFIED:
00084       for (int i=n; i--; )
00085         exactly(s, s[i], i);
00086       linear(this, s, IRT_EQ, n);
00087       break;
00088     case PROP_COUNT:
00089       for (int i=n; i--; )
00090         count(this, s, i, IRT_EQ, s[i]);
00091       linear(this, s, IRT_EQ, n);
00092       break;
00093     case PROP_GCC:
00094       count(this, s, s, opt.icl());
00095       break;
00096     }
00097     IntArgs c(n);
00098     for (int j = n; j--; )
00099       c[j] = j-1;
00100     linear(this, c, s, IRT_EQ, 0);
00101     branch(this, s, INT_VAR_NONE, INT_VAL_SPLIT_MAX);
00102   }
00103 
00105   MagicSequence(bool share, MagicSequence& e) : Example(share,e), n(e.n) {
00106     s.update(this, share, e.s);
00107   }
00109   virtual Space*
00110   copy(bool share) {
00111     return new MagicSequence(share,*this);
00112   }
00114   virtual
00115   void print(std::ostream& os) {
00116     os << "\t";
00117     for (int i = 0; i<n; i++) {
00118       os << s[i] << ", ";
00119       if ((i+1) % 20 == 0)
00120         os << std::endl << "\t";
00121     }
00122     os << std::endl;
00123   }
00124 
00125 };
00126 
00130 int
00131 main(int argc, char* argv[]) {
00132   SizeOptions opt("MagicSequence");
00133   opt.solutions(0);
00134   opt.iterations(4);
00135   opt.size(500);
00136   opt.propagation(MagicSequence::PROP_COUNT);
00137   opt.propagation(MagicSequence::PROP_REIFIED, "reified");
00138   opt.propagation(MagicSequence::PROP_COUNT,   "count");
00139   opt.propagation(MagicSequence::PROP_GCC,     "gcc");
00140   opt.parse(argc,argv);
00141   Example::run<MagicSequence,DFS,SizeOptions>(opt);
00142   return 0;
00143 }
00144 
00145 // STATISTICS: example-any
00146