Generated on Tue May 22 09:39:38 2018 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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/driver.hh>
00035 #include <gecode/int.hh>
00036 #include <gecode/minimodel.hh>
00037 
00038 #include <iomanip>
00039 
00040 using namespace Gecode;
00041 
00062 class GolombRuler : public IntMinimizeScript {
00063 protected:
00065   IntVarArray m;
00066 public:
00068   GolombRuler(const SizeOptions& opt)
00069     : IntMinimizeScript(opt),
00070       m(*this,opt.size(),0,
00071         (opt.size() < 31) ? (1 << (opt.size()-1))-1 : Int::Limits::max) {
00072 
00073     // Assume first mark to be zero
00074     rel(*this, m[0], IRT_EQ, 0);
00075 
00076     // Order marks
00077     rel(*this, m, IRT_LE);
00078 
00079     // Number of marks and differences
00080     const int n = m.size();
00081     const int n_d = (n*n-n)/2;
00082 
00083     // Array of differences
00084     IntVarArgs d(n_d);
00085 
00086     // Setup difference constraints
00087     for (int k=0, i=0; i<n-1; i++)
00088       for (int j=i+1; j<n; j++, k++)
00089         // d[k] is m[j]-m[i] and must be at least sum of first j-i integers
00090         rel(*this, d[k] = expr(*this, m[j]-m[i]),
00091                    IRT_GQ, (j-i)*(j-i+1)/2);
00092 
00093     distinct(*this, d, opt.ipl());
00094 
00095     // Symmetry breaking
00096     if (n > 2)
00097       rel(*this, d[0], IRT_LE, d[n_d-1]);
00098 
00099     branch(*this, m, INT_VAR_NONE(), INT_VAL_MIN());
00100   }
00101 
00103   virtual IntVar cost(void) const {
00104     return m[m.size()-1];
00105   }
00106 
00108   virtual void
00109   print(std::ostream& os) const {
00110     os << "\tm[" << m.size() << "] = " << m << std::endl;
00111   }
00112 
00114   GolombRuler(GolombRuler& s)
00115     : IntMinimizeScript(s) {
00116     m.update(*this, s.m);
00117   }
00119   virtual Space*
00120   copy(void) {
00121     return new GolombRuler(*this);
00122   }
00123 };
00124 
00128 int
00129 main(int argc, char* argv[]) {
00130   SizeOptions opt("GolombRuler");
00131   opt.solutions(0);
00132   opt.size(10);
00133   opt.ipl(IPL_BND);
00134   opt.parse(argc,argv);
00135   if (opt.size() > 0)
00136     IntMinimizeScript::run<GolombRuler,BAB,SizeOptions>(opt);
00137   return 0;
00138 }
00139 
00140 // STATISTICS: example-any
00141