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 "examples/support.hh"
00039 #include "gecode/minimodel.hh"
00040
00041 #include <iomanip>
00042
00057 class GolombRuler : public Example {
00058 protected:
00060 const int n;
00062 IntVarArray m;
00063 public:
00065 enum {
00066 MODEL_NONE,
00067 MODEL_SUM,
00068 MODEL_RULER
00069 };
00071 enum {
00072 SEARCH_DFS,
00073 SEARCH_BAB,
00074 SEARCH_RESTART
00075 };
00077 int
00078 diag(int i, int j) {
00079 return (i*(2*n-i-1)) / 2 + j - i - 1;
00080 }
00081
00083 GolombRuler(const SizeOptions& opt)
00084 : n(opt.size()), m(this,n,0,n*n) {
00085 const int dn = (n*n-n)/2;
00086
00087 IntVarArgs d(dn);
00088
00089
00090 rel(this, m[0], IRT_EQ, 0);
00091
00092
00093 for (int j=1; j<n; j++)
00094 d[diag(0,j)] = m[j];
00095 for (int i=1; i<n-1; i++)
00096 for (int j=i+1; j<n; j++)
00097 d[diag(i,j)] = minus(this, m[j], m[i]);
00098
00099
00100 rel(this, m, IRT_LE);
00101
00102 switch (opt.model()) {
00103 case MODEL_NONE:
00104 break;
00105 case MODEL_SUM:
00106
00107 for (int i=0; i<n; i++)
00108 for (int j=i+1; j<n; j++)
00109 rel(this, d[diag(i,j)], IRT_GQ, (j-i)*(j-i+1)/2);
00110 break;
00111 case MODEL_RULER:
00112 {
00113 static const int length[] = {
00114
00115 0,0,1,3,6,11,17,25,34,44,
00116
00117 55,72,85,106,127};
00118
00119 for (int i=0; i<n; i++)
00120 for (int j=i+1; j<n; j++)
00121 rel(this, d[diag(i,j)], IRT_GQ, length[j-i+1]);
00122 }
00123 break;
00124 }
00125
00126 distinct(this, d, opt.icl());
00127
00128 if (n > 2)
00129 rel(this, d[diag(0,1)], IRT_LE, d[diag(n-2,n-1)]);
00130
00131 if (opt.search() == SEARCH_DFS) {
00132 IntVarArgs max(1);
00133 max[0]=m[n-1];
00134 branch(this, max, INT_VAR_NONE, INT_VAL_SPLIT_MIN);
00135 }
00136
00137 branch(this, m, INT_VAR_NONE, INT_VAL_MIN);
00138 }
00139
00141 void
00142 constrain(Space* s) {
00143 rel(this, m[n-1], IRT_LE, static_cast<GolombRuler*>(s)->m[n-1].val());
00144 }
00145
00147 virtual void
00148 print(std::ostream& os) {
00149 os << "\tm[" << n << "] = " << m << std::endl;
00150 }
00151
00153 GolombRuler(bool share, GolombRuler& s)
00154 : Example(share,s), n(s.n) {
00155 m.update(this, share, s.m);
00156 }
00158 virtual Space*
00159 copy(bool share) {
00160 return new GolombRuler(share,*this);
00161 }
00162
00163 };
00164
00168 int
00169 main(int argc, char* argv[]) {
00170 SizeOptions opt("GolombRuler");
00171 opt.solutions(0);
00172 opt.size(10);
00173 opt.icl(ICL_BND);
00174 opt.model(GolombRuler::MODEL_SUM);
00175 opt.model(GolombRuler::MODEL_NONE, "none",
00176 "no lower bound");
00177 opt.model(GolombRuler::MODEL_SUM, "sum",
00178 "use sum of ticks as lower bound");
00179 opt.model(GolombRuler::MODEL_RULER, "ruler",
00180 "use size of smaller rulers as lower bound");
00181 opt.search(GolombRuler::SEARCH_BAB);
00182 opt.search(GolombRuler::SEARCH_DFS, "dfs");
00183 opt.search(GolombRuler::SEARCH_BAB, "bab");
00184 opt.search(GolombRuler::SEARCH_RESTART, "restart");
00185 opt.parse(argc,argv);
00186 if (opt.size() > 0)
00187 switch (opt.search()) {
00188 case GolombRuler::SEARCH_DFS:
00189 Example::run<GolombRuler,DFS,SizeOptions>(opt); break;
00190 case GolombRuler::SEARCH_BAB:
00191 Example::run<GolombRuler,BAB,SizeOptions>(opt); break;
00192 case GolombRuler::SEARCH_RESTART:
00193 Example::run<GolombRuler,Restart,SizeOptions>(opt); break;
00194 }
00195 return 0;
00196 }
00197
00198
00199