Generated on Tue Apr 18 10:21:29 2017 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: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
00011  *     $Revision: 14672 $
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     : IntMinimizeScript(opt),
00074       m(*this,opt.size(),0,
00075         (opt.size() < 31) ? (1 << (opt.size()-1))-1 : Int::Limits::max) {
00076 
00077     // Assume first mark to be zero
00078     rel(*this, m[0], IRT_EQ, 0);
00079 
00080     // Order marks
00081     rel(*this, m, IRT_LE);
00082 
00083     // Number of marks and differences
00084     const int n = m.size();
00085     const int n_d = (n*n-n)/2;
00086 
00087     // Array of differences
00088     IntVarArgs d(n_d);
00089 
00090     // Setup difference constraints
00091     for (int k=0, i=0; i<n-1; i++)
00092       for (int j=i+1; j<n; j++, k++)
00093         // d[k] is m[j]-m[i] and must be at least sum of first j-i integers
00094         rel(*this, d[k] = expr(*this, m[j]-m[i]),
00095                    IRT_GQ, (j-i)*(j-i+1)/2);
00096 
00097     distinct(*this, d, opt.ipl());
00098 
00099     // Symmetry breaking
00100     if (n > 2)
00101       rel(*this, d[0], IRT_LE, d[n_d-1]);
00102 
00103     branch(*this, m, INT_VAR_NONE(), INT_VAL_MIN());
00104   }
00105 
00107   virtual IntVar cost(void) const {
00108     return m[m.size()-1];
00109   }
00110 
00112   virtual void
00113   print(std::ostream& os) const {
00114     os << "\tm[" << m.size() << "] = " << m << std::endl;
00115   }
00116 
00118   GolombRuler(bool share, GolombRuler& s)
00119     : IntMinimizeScript(share,s) {
00120     m.update(*this, share, s.m);
00121   }
00123   virtual Space*
00124   copy(bool share) {
00125     return new GolombRuler(share,*this);
00126   }
00127 };
00128 
00132 int
00133 main(int argc, char* argv[]) {
00134   SizeOptions opt("GolombRuler");
00135   opt.solutions(0);
00136   opt.size(10);
00137   opt.ipl(IPL_BND);
00138   opt.parse(argc,argv);
00139   if (opt.size() > 0)
00140     IntMinimizeScript::run<GolombRuler,BAB,SizeOptions>(opt);
00141   return 0;
00142 }
00143 
00144 // STATISTICS: example-any
00145