Generated on Thu Mar 22 10:39:31 2012 for Gecode by doxygen 1.6.3

golomb-ruler.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: 2011-08-29 12:13:11 +0200 (Mon, 29 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12357 $
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 #include <iomanip>
00043 
00044 using namespace Gecode;
00045 
00066 class GolombRuler : public MinimizeScript {
00067 protected:
00069   IntVarArray m;
00070 public:
00072   enum {
00073     SEARCH_BAB,    
00074     SEARCH_RESTART 
00075   };
00077   GolombRuler(const SizeOptions& opt)
00078     : m(*this,opt.size(),0,
00079         (opt.size() < 31) ? (1 << (opt.size()-1))-1 : Int::Limits::max) {
00080 
00081     // Assume first mark to be zero
00082     rel(*this, m[0], IRT_EQ, 0);
00083 
00084     // Order marks
00085     rel(*this, m, IRT_LE);
00086 
00087     // Number of marks and differences
00088     const int n = m.size();
00089     const int n_d = (n*n-n)/2;
00090 
00091     // Array of differences
00092     IntVarArgs d(n_d);
00093 
00094     // Setup difference constraints
00095     for (int k=0, i=0; i<n-1; i++)
00096       for (int j=i+1; j<n; j++, k++)
00097         // d[k] is m[j]-m[i] and must be at least sum of first j-i integers
00098         rel(*this, d[k] = expr(*this, m[j]-m[i]),
00099                    IRT_GQ, (j-i)*(j-i+1)/2);
00100 
00101     distinct(*this, d, opt.icl());
00102 
00103     // Symmetry breaking
00104     if (n > 2)
00105       rel(*this, d[0], IRT_LE, d[n_d-1]);
00106 
00107     branch(*this, m, INT_VAR_NONE, INT_VAL_MIN);
00108   }
00109 
00111   virtual IntVar cost(void) const {
00112     return m[m.size()-1];
00113   }
00114 
00116   virtual void
00117   print(std::ostream& os) const {
00118     os << "\tm[" << m.size() << "] = " << m << std::endl;
00119   }
00120 
00122   GolombRuler(bool share, GolombRuler& s)
00123     : MinimizeScript(share,s) {
00124     m.update(*this, share, s.m);
00125   }
00127   virtual Space*
00128   copy(bool share) {
00129     return new GolombRuler(share,*this);
00130   }
00131 };
00132 
00136 int
00137 main(int argc, char* argv[]) {
00138   SizeOptions opt("GolombRuler");
00139   opt.solutions(0);
00140   opt.size(10);
00141   opt.icl(ICL_BND);
00142   opt.search(GolombRuler::SEARCH_BAB);
00143   opt.search(GolombRuler::SEARCH_BAB, "bab");
00144   opt.search(GolombRuler::SEARCH_RESTART, "restart");
00145   opt.parse(argc,argv);
00146   if (opt.size() > 0)
00147     switch (opt.search()) {
00148     case GolombRuler::SEARCH_BAB:
00149       MinimizeScript::run<GolombRuler,BAB,SizeOptions>(opt); break;
00150     case GolombRuler::SEARCH_RESTART:
00151       MinimizeScript::run<GolombRuler,Restart,SizeOptions>(opt); break;
00152     }
00153   return 0;
00154 }
00155 
00156 // STATISTICS: example-any
00157