Generated on Wed Nov 5 2014 05:18:15 for Gecode by doxygen 1.7.6.1
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: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
00011  *     $Revision: 13820 $
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 IntMinimizeScript {
00067 protected:
00069   IntVarArray m;
00070 public:
00072   GolombRuler(const SizeOptions& opt)
00073     : m(*this,opt.size(),0,
00074         (opt.size() < 31) ? (1 << (opt.size()-1))-1 : Int::Limits::max) {
00075 
00076     // Assume first mark to be zero
00077     rel(*this, m[0], IRT_EQ, 0);
00078 
00079     // Order marks
00080     rel(*this, m, IRT_LE);
00081 
00082     // Number of marks and differences
00083     const int n = m.size();
00084     const int n_d = (n*n-n)/2;
00085 
00086     // Array of differences
00087     IntVarArgs d(n_d);
00088 
00089     // Setup difference constraints
00090     for (int k=0, i=0; i<n-1; i++)
00091       for (int j=i+1; j<n; j++, k++)
00092         // d[k] is m[j]-m[i] and must be at least sum of first j-i integers
00093         rel(*this, d[k] = expr(*this, m[j]-m[i]),
00094                    IRT_GQ, (j-i)*(j-i+1)/2);
00095 
00096     distinct(*this, d, opt.icl());
00097 
00098     // Symmetry breaking
00099     if (n > 2)
00100       rel(*this, d[0], IRT_LE, d[n_d-1]);
00101 
00102     branch(*this, m, INT_VAR_NONE(), INT_VAL_MIN());
00103   }
00104 
00106   virtual IntVar cost(void) const {
00107     return m[m.size()-1];
00108   }
00109 
00111   virtual void
00112   print(std::ostream& os) const {
00113     os << "\tm[" << m.size() << "] = " << m << std::endl;
00114   }
00115 
00117   GolombRuler(bool share, GolombRuler& s)
00118     : IntMinimizeScript(share,s) {
00119     m.update(*this, share, s.m);
00120   }
00122   virtual Space*
00123   copy(bool share) {
00124     return new GolombRuler(share,*this);
00125   }
00126 };
00127 
00131 int
00132 main(int argc, char* argv[]) {
00133   SizeOptions opt("GolombRuler");
00134   opt.solutions(0);
00135   opt.size(10);
00136   opt.icl(ICL_BND);
00137   opt.parse(argc,argv);
00138   if (opt.size() > 0)
00139     IntMinimizeScript::run<GolombRuler,BAB,SizeOptions>(opt);
00140   return 0;
00141 }
00142 
00143 // STATISTICS: example-any
00144