Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00077 rel(*this, m[0], IRT_EQ, 0);
00078
00079
00080 rel(*this, m, IRT_LE);
00081
00082
00083 const int n = m.size();
00084 const int n_d = (n*n-n)/2;
00085
00086
00087 IntVarArgs d(n_d);
00088
00089
00090 for (int k=0, i=0; i<n-1; i++)
00091 for (int j=i+1; j<n; j++, k++)
00092
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
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
00144