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

langford-number.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00005  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00006  *     Christian Schulte <schulte@gecode.org>
00007  *
00008  *  Copyright:
00009  *     Patrick Pekczynski, 2004
00010  *     Mikael Lagerkvist, 2006
00011  *     Christian Schulte, 2007
00012  *
00013  *  Last modified:
00014  *     $Date: 2008-07-30 22:00:09 +0200 (Wed, 30 Jul 2008) $ by $Author: tack $
00015  *     $Revision: 7474 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */        
00041 
00042 #include "examples/support.hh"
00043 #include "gecode/minimodel.hh"
00044 
00050 class LangfordNumberOptions : public Options {
00051 public:
00052   int k, n; 
00053 
00054   LangfordNumberOptions(const char* s, int k0, int n0) 
00055     : Options(s), k(k0), n(n0) {}
00057   void parse(int& argc, char* argv[]) {
00058     Options::parse(argc,argv);
00059     if (argc < 3)
00060       return;
00061     n = atoi(argv[1]);
00062     k = atoi(argv[2]);
00063   }
00065   virtual void help(void) {
00066     Options::help();
00067     std::cerr << "\t(unsigned int) default: " << n << std::endl
00068               << "\t\tparameter n" << std::endl
00069               << "\t(unsigned int) default: " << k << std::endl
00070               << "\t\tparameter k" << std::endl;
00071   }
00072 };
00073 
00081 class LangfordNumber : public Example {
00082 protected:
00083   int k, n;      
00084   IntVarArray y; 
00085 
00086 public:
00088   enum {
00089     PROP_REIFIED,            
00090     PROP_EXTENSIONAL,        
00091     PROP_EXTENSIONAL_CHANNEL 
00092   };
00094   LangfordNumber(const LangfordNumberOptions& opt)
00095     : k(opt.k), n(opt.n), y(this,k*n,1,n) {
00096 
00097     switch (opt.propagation()) {
00098     case PROP_REIFIED:
00099       {
00101         IntVarArgs p(k*n);
00102         for (int i=k*n; i--; )
00103           p[i].init(this,0,k*n-1);
00104         
00105         /* 
00106          * The occurences of v in the Langford sequence are v numbers apart.
00107          *
00108          * Let \#(i, v) denote the position of the i-th occurence of 
00109          * value v in the Langford Sequence. Then 
00110          *
00111          * \f$ \forall i, j \in \{1, \dots, k\}, i \neq j:
00112          *     \forall v \in \{1, \dots, n\}: \#(i, v) + (v + 1) = \#(j, v)\f$
00113          *
00114          */
00115         for (int i=n; i--; )
00116           for (int j=k-1; j--; )
00117             post(this, p[i*k+j] + (i+2) == p[i*k+j+1]);
00118         
00119         distinct(this, p, opt.icl());
00120         
00121         // Channel positions <-> values
00122         for (int i=n; i--; )
00123           for (int j=k; j--; )
00124             element(this, y, p[i*k+j], i+1);
00125       }
00126       break;
00127     case PROP_EXTENSIONAL:
00128       {
00129         IntArgs a(n-1);
00130         for (int v=2; v<=n; v++)
00131           a[v-2]=v;
00132         for (int v=1; v<=n; v++) {
00133           // Construct regular expression for all symbols but v
00134           if (v > 1)
00135             a[v-2]=v-1;
00136           REG ra(a), rv(v);
00137           extensional(this, y, *ra+rv+(ra(v,v)+rv)(k-1,k-1)+*ra);
00138         }
00139       }
00140       break;
00141     case PROP_EXTENSIONAL_CHANNEL:
00142       {
00143         // Boolean variables for channeling
00144         BoolVarArgs b(k*n*n);
00145         for (int i=n*n*k; i--; )
00146           b[i].init(this,0,1);
00147         
00148         // Post channel constraints
00149         for (int i=n*k; i--; ) {
00150           BoolVarArgs c(n);
00151           for (int j=n; j--; ) 
00152             c[j]=b[i*n+j];
00153           channel(this, c, y[i], 1);
00154         }
00155         
00156         // For placing two numbers three steps apart, we construct the
00157         // regular expression 0*100010*, and apply it to the projection of
00158         // the sequence on the value.
00159         REG r0(0), r1(1);
00160         for (int v=1; v<=n; v++) {
00161           // Projection for value v
00162           BoolVarArgs c(k*n);
00163           for (int i = k*n; i--; )
00164             c[i] = b[i*n+(v-1)];
00165           extensional(this, c, *r0 + r1 + (r0(v,v) + r1)(k-1,k-1) + *r0);
00166         }
00167       }
00168       break;
00169     }
00170       
00171     // Symmetry breaking
00172     rel(this, y[0], IRT_LE, y[n*k-1]);
00173     
00174     // Branching
00175     branch(this, y, INT_VAR_SIZE_MIN, INT_VAL_MAX);
00176   }
00177 
00179   virtual void print(std::ostream& os){
00180     os << "\t" << y << std::endl;
00181   }
00182 
00184   LangfordNumber(bool share, LangfordNumber& l)
00185     : Example(share, l), k(l.k), n(l.n) {
00186     y.update(this, share, l.y);
00187 
00188   }
00190   virtual Space*
00191   copy(bool share) {
00192     return new LangfordNumber(share, *this);
00193   }
00194 };
00195 
00196 
00200 int 
00201 main(int argc, char* argv[]) {
00202   LangfordNumberOptions opt("Langford Numbers",3,9);
00203   opt.icl(ICL_DOM);
00204   opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL);
00205   opt.propagation(LangfordNumber::PROP_REIFIED, 
00206                   "reified");
00207   opt.propagation(LangfordNumber::PROP_EXTENSIONAL, 
00208                   "extensional");
00209   opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL, 
00210                   "extensional-channel");
00211   opt.parse(argc, argv);
00212   if (opt.k < 1) {
00213     std::cerr << "k must be at least 1!" << std::endl;
00214     return 1;
00215   }
00216   if (opt.k > opt.n) {
00217     std::cerr << "n must be at least k!" << std::endl;
00218     return 1;
00219   }
00220   Example::run<LangfordNumber,DFS,LangfordNumberOptions>(opt);
00221   return 0;
00222 }
00223 
00224 // STATISTICS: example-any
00225