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

golomb-ruler.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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2001
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-11-30 13:58:34 +0100 (Fri, 30 Nov 2007) $ by $Author: tack $
00011  *     $Revision: 5524 $
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 "examples/support.hh"
00039 #include "gecode/minimodel.hh"
00040 
00041 #include <iomanip>
00042 
00057 class GolombRuler : public Example {
00058 protected:
00060   const int n;
00062   IntVarArray m;
00063 public:
00065   enum {
00066     MODEL_NONE, 
00067     MODEL_SUM,  
00068     MODEL_RULER 
00069   };
00071   enum {
00072     SEARCH_DFS,    
00073     SEARCH_BAB,    
00074     SEARCH_RESTART 
00075   };
00077   int
00078   diag(int i, int j) {
00079     return (i*(2*n-i-1)) / 2 + j - i - 1;
00080   }
00081 
00083   GolombRuler(const SizeOptions& opt)
00084     : n(opt.size()), m(this,n,0,n*n) {
00085     const int dn = (n*n-n)/2;
00086 
00087     IntVarArgs d(dn);
00088 
00089     // Assume first mark to be zero
00090     rel(this, m[0], IRT_EQ, 0);
00091 
00092     // Setup difference constraints
00093     for (int j=1; j<n; j++)
00094       d[diag(0,j)] = m[j];
00095     for (int i=1; i<n-1; i++)
00096       for (int j=i+1; j<n; j++)
00097         d[diag(i,j)] = minus(this, m[j], m[i]);
00098 
00099     // Order marks
00100     rel(this, m, IRT_LE);
00101 
00102     switch (opt.model()) {
00103     case MODEL_NONE:
00104       break;
00105     case MODEL_SUM:
00106       // d[diag(i,j)] must be at least sum of first j-i integers
00107       for (int i=0; i<n; i++)
00108         for (int j=i+1; j<n; j++)
00109           rel(this, d[diag(i,j)], IRT_GQ, (j-i)*(j-i+1)/2);
00110       break;
00111     case MODEL_RULER:
00112       {
00113         static const int length[] = {
00114           // Length 0-9
00115           0,0,1,3,6,11,17,25,34,44,
00116           // Length 10-
00117           55,72,85,106,127};
00118         // Marks from i to j must be ruler of length j-1+i
00119         for (int i=0; i<n; i++)
00120           for (int j=i+1; j<n; j++)
00121             rel(this, d[diag(i,j)], IRT_GQ, length[j-i+1]);
00122       }
00123       break;
00124     }
00125 
00126     distinct(this, d, opt.icl());
00127 
00128     if (n > 2)
00129       rel(this, d[diag(0,1)], IRT_LE, d[diag(n-2,n-1)]);
00130 
00131     if (opt.search() == SEARCH_DFS) {
00132       IntVarArgs max(1);
00133       max[0]=m[n-1];
00134       branch(this, max, INT_VAR_NONE, INT_VAL_SPLIT_MIN);
00135     }
00136 
00137     branch(this, m, INT_VAR_NONE, INT_VAL_MIN);
00138   }
00139 
00141   void
00142   constrain(Space* s) {
00143     rel(this, m[n-1], IRT_LE, static_cast<GolombRuler*>(s)->m[n-1].val());
00144   }
00145 
00147   virtual void
00148   print(std::ostream& os) {
00149     os << "\tm[" << n << "] = " << m << std::endl;
00150   }
00151 
00153   GolombRuler(bool share, GolombRuler& s)
00154     : Example(share,s), n(s.n) {
00155     m.update(this, share, s.m);
00156   }
00158   virtual Space*
00159   copy(bool share) {
00160     return new GolombRuler(share,*this);
00161   }
00162 
00163 };
00164 
00168 int
00169 main(int argc, char* argv[]) {
00170   SizeOptions opt("GolombRuler");
00171   opt.solutions(0);
00172   opt.size(10);
00173   opt.icl(ICL_BND);
00174   opt.model(GolombRuler::MODEL_SUM);
00175   opt.model(GolombRuler::MODEL_NONE, "none",
00176             "no lower bound");
00177   opt.model(GolombRuler::MODEL_SUM, "sum",
00178             "use sum of ticks as lower bound");
00179   opt.model(GolombRuler::MODEL_RULER, "ruler",
00180             "use size of smaller rulers as lower bound");
00181   opt.search(GolombRuler::SEARCH_BAB);
00182   opt.search(GolombRuler::SEARCH_DFS, "dfs");
00183   opt.search(GolombRuler::SEARCH_BAB, "bab");
00184   opt.search(GolombRuler::SEARCH_RESTART, "restart");
00185   opt.parse(argc,argv);
00186   if (opt.size() > 0)
00187     switch (opt.search()) {
00188     case GolombRuler::SEARCH_DFS:
00189       Example::run<GolombRuler,DFS,SizeOptions>(opt); break;
00190     case GolombRuler::SEARCH_BAB:
00191       Example::run<GolombRuler,BAB,SizeOptions>(opt); break;
00192     case GolombRuler::SEARCH_RESTART:
00193       Example::run<GolombRuler,Restart,SizeOptions>(opt); break;
00194     }
00195   return 0;
00196 }
00197 
00198 // STATISTICS: example-any
00199