00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "examples/support.hh"
00023 #include "gecode/minimodel.hh"
00024
00032 class ExtOptions : public Options {
00033 public:
00034 ExtOptions(const char* s) : Options(s) {}
00035 int n;
00036 int k;
00037 };
00038
00050 class LangfordNum : public Example {
00051 private:
00052 int n;
00053 int k;
00054
00057 IntVarArray pos;
00058 IntVarArray y;
00060 IntVarArray pi;
00061
00062 public:
00063
00070 void adiff_skn(Space* home, IntVarArray& x, IntVarArray& pi) {
00071
00072 IntVarArray skn(home, k * n);
00073 for (int i = k * n; i--; ) {
00074 skn[i].init(home, i, i);
00075 }
00076
00077 for (int i = k * n; i--; ) {
00078 pi[i].init (home, 0, k * n - 1);
00079 }
00080
00081 sortedness(home, x, skn, pi);
00082 }
00083
00088 IntVar& p(int i, int j) {
00089 return pos[i * k + j];
00090 }
00091
00092 IntVar& ys(int i, int j) {
00093 return y[i * k + j];
00094 }
00095
00106 void distance(Space* home) {
00107
00108 for (int i = 0; i < n; i++) {
00109 int v = i + 1;
00110 for (int j = 0; j < k - 1; j++) {
00111 post(home, p(i, j) + (v + 1) == p(i, j + 1));
00112 }
00113 }
00114 }
00115
00116 LangfordNum(const Options& op) {
00117
00118 const ExtOptions* eop = NULL;
00119 eop = reinterpret_cast<const ExtOptions*> (&op);
00120 n = eop->n;
00121 k = eop->k;
00122
00123
00124 y = IntVarArray(this, k * n);
00125 pos = IntVarArray(this, k * n);
00126 pi = IntVarArray(this, k * n);
00127
00128 IntSet dom_val(1, n);
00129 IntSet dom_idx(0, k * n - 1);
00130
00131 for (int i = 0; i < n; i++) {
00132 for (int j = 0; j < k; j++) {
00133 p(i, j).init(this, dom_idx);
00134 y[i*k + j].init(this, dom_val);
00135 }
00136 }
00137
00138 distance(this);
00139 adiff_skn(this, pos, pi);
00140
00141 rel(this, y[0], IRT_LE, y[n*k - 1]);
00142
00143
00144
00145
00146
00147
00148 for (int i = 0; i < n; i++) {
00149 for (int j = 0; j < k; j++) {
00150 element(this, y, p(i, j), IntVar(this, i + 1, i + 1));
00151 }
00152 }
00153
00154 branch(this, pos, BVAR_SIZE_MIN, BVAL_MIN);
00155 }
00156
00157 LangfordNum(bool share, LangfordNum& l)
00158 : Example(share, l), n(l.n), k(l.k) {
00159 pos.update(this, share, l.pos);
00160 pi.update (this, share, l.pi);
00161 y.update(this, share, l.y);
00162
00163 }
00164
00165 virtual Space*
00166 copy(bool share) {
00167 return new LangfordNum(share, *this);
00168 }
00169
00170 virtual void print(void){
00171 std::cout << "\nL(" << k << "," << n <<"):\n";
00172
00173 GECODE_AUTOARRAY(int, values, k*n);
00174 for (int i = 0; i < n; i++) {
00175 for (int j = 0; j < k; j++) {
00176 values[i * k + j] = i + 1;
00177 }
00178 }
00179
00180 std::cout << "seq: ";
00181 for(int c = 0; c < k * n; c++) {
00182 int i = 0;
00183 while (pos[i].assigned() && pos[i].val() != c) {
00184 i++;
00185 }
00186 std::cout << values[i] << " ";
00187 }
00188 std::cout <<"\n";
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 std::cout << "pos: ";
00203 for (int i = 0; i < k * n; i++) {
00204 std::cout << pos[i] << " ";
00205 }
00206 std::cout <<"\n";
00207
00208
00209
00210
00211
00212
00213
00214 }
00215 };
00216
00217 int main(int argc, char** argv){
00218 ExtOptions o("Langford Numbers");
00219 if (argc < 2) {
00220 std::cerr << "specify parameters k and n\n";
00221 std::cerr << "usage is: ./langfordnum k n [gecode options] \n";
00222 return -1;
00223 }
00224 char* name = argv[0];
00225 o.k = atoi(argv[1]);
00226 o.n = atoi(argv[2]);
00227 argv[2] = name;
00228 argv++;
00229 argv++;
00230 if (o.k < 1) {
00231 std::cerr << "k must be at least 1!\n";
00232 return -1;
00233 }
00234 if (o.k > o.n) {
00235 std::cerr << "n must be at least k!\n";
00236 return -1;
00237 }
00238 o.parse(argc - 2, argv);
00239 Example::run<LangfordNum, DFS>(o);
00240 return 0;
00241 }
00242
00243
00244