Generated on Mon Nov 30 23:53:17 2009 for Gecode by doxygen 1.6.1

langford-number.cpp

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: 2009-04-02 20:58:46 +0200 (Thu, 02 Apr 2009) $ by $Author: schulte $
00015  *     $Revision: 8649 $
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 <gecode/driver.hh>
00043 #include <gecode/int.hh>
00044 #include <gecode/minimodel.hh>
00045 
00046 using namespace Gecode;
00047 
00053 class LangfordNumberOptions : public Options {
00054 public:
00055   int k, n; 
00056 
00057   LangfordNumberOptions(const char* s, int k0, int n0)
00058     : Options(s), k(k0), n(n0) {}
00060   void parse(int& argc, char* argv[]) {
00061     Options::parse(argc,argv);
00062     if (argc < 3)
00063       return;
00064     n = atoi(argv[1]);
00065     k = atoi(argv[2]);
00066   }
00068   virtual void help(void) {
00069     Options::help();
00070     std::cerr << "\t(unsigned int) default: " << n << std::endl
00071               << "\t\tparameter n" << std::endl
00072               << "\t(unsigned int) default: " << k << std::endl
00073               << "\t\tparameter k" << std::endl;
00074   }
00075 };
00076 
00084 class LangfordNumber : public Script {
00085 protected:
00086   int k, n;      
00087   IntVarArray y; 
00088 
00089 public:
00091   enum {
00092     PROP_REIFIED,            
00093     PROP_EXTENSIONAL,        
00094     PROP_EXTENSIONAL_CHANNEL 
00095   };
00097   LangfordNumber(const LangfordNumberOptions& opt)
00098     : k(opt.k), n(opt.n), y(*this,k*n,1,n) {
00099 
00100     switch (opt.propagation()) {
00101     case PROP_REIFIED:
00102       {
00104         IntVarArgs p(k*n);
00105         for (int i=k*n; i--; )
00106           p[i].init(*this,0,k*n-1);
00107 
00108         /*
00109          * The occurences of v in the Langford sequence are v numbers apart.
00110          *
00111          * Let \#(i, v) denote the position of the i-th occurence of
00112          * value v in the Langford Sequence. Then
00113          *
00114          * \f$ \forall i, j \in \{1, \dots, k\}, i \neq j:
00115          *     \forall v \in \{1, \dots, n\}: \#(i, v) + (v + 1) = \#(j, v)\f$
00116          *
00117          */
00118         for (int i=n; i--; )
00119           for (int j=k-1; j--; )
00120             post(*this, p[i*k+j] + (i+2) == p[i*k+j+1]);
00121 
00122         distinct(*this, p, opt.icl());
00123 
00124         // Channel positions <-> values
00125         for (int i=n; i--; )
00126           for (int j=k; j--; )
00127             element(*this, y, p[i*k+j], i+1);
00128       }
00129       break;
00130     case PROP_EXTENSIONAL:
00131       {
00132         IntArgs a(n-1);
00133         for (int v=2; v<=n; v++)
00134           a[v-2]=v;
00135         for (int v=1; v<=n; v++) {
00136           // Construct regular expression for all symbols but v
00137           if (v > 1)
00138             a[v-2]=v-1;
00139           REG ra(a), rv(v);
00140           extensional(*this, y, *ra+rv+(ra(v,v)+rv)(k-1,k-1)+*ra);
00141         }
00142       }
00143       break;
00144     case PROP_EXTENSIONAL_CHANNEL:
00145       {
00146         // Boolean variables for channeling
00147         BoolVarArgs b(k*n*n);
00148         for (int i=n*n*k; i--; )
00149           b[i].init(*this,0,1);
00150 
00151         // Post channel constraints
00152         for (int i=n*k; i--; ) {
00153           BoolVarArgs c(n);
00154           for (int j=n; j--; )
00155             c[j]=b[i*n+j];
00156           channel(*this, c, y[i], 1);
00157         }
00158 
00159         // For placing two numbers three steps apart, we construct the
00160         // regular expression 0*100010*, and apply it to the projection of
00161         // the sequence on the value.
00162         REG r0(0), r1(1);
00163         for (int v=1; v<=n; v++) {
00164           // Projection for value v
00165           BoolVarArgs c(k*n);
00166           for (int i = k*n; i--; )
00167             c[i] = b[i*n+(v-1)];
00168           extensional(*this, c, *r0 + r1 + (r0(v,v) + r1)(k-1,k-1) + *r0);
00169         }
00170       }
00171       break;
00172     }
00173 
00174     // Symmetry breaking
00175     rel(*this, y[0], IRT_LE, y[n*k-1]);
00176 
00177     // Branching
00178     branch(*this, y, INT_VAR_SIZE_MIN, INT_VAL_MAX);
00179   }
00180 
00182   virtual void print(std::ostream& os) const {
00183     os << "\t" << y << std::endl;
00184   }
00185 
00187   LangfordNumber(bool share, LangfordNumber& l)
00188     : Script(share, l), k(l.k), n(l.n) {
00189     y.update(*this, share, l.y);
00190 
00191   }
00193   virtual Space*
00194   copy(bool share) {
00195     return new LangfordNumber(share, *this);
00196   }
00197 };
00198 
00199 
00203 int
00204 main(int argc, char* argv[]) {
00205   LangfordNumberOptions opt("Langford Numbers",3,9);
00206   opt.icl(ICL_DOM);
00207   opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL);
00208   opt.propagation(LangfordNumber::PROP_REIFIED,
00209                   "reified");
00210   opt.propagation(LangfordNumber::PROP_EXTENSIONAL,
00211                   "extensional");
00212   opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL,
00213                   "extensional-channel");
00214   opt.parse(argc, argv);
00215   if (opt.k < 1) {
00216     std::cerr << "k must be at least 1!" << std::endl;
00217     return 1;
00218   }
00219   if (opt.k > opt.n) {
00220     std::cerr << "n must be at least k!" << std::endl;
00221     return 1;
00222   }
00223   Script::run<LangfordNumber,DFS,LangfordNumberOptions>(opt);
00224   return 0;
00225 }
00226 
00227 // STATISTICS: example-any
00228