1 #include <chuffed/core/propagator.h>
2
regular_check(vec<IntVar * > & x,int q,int s,vec<vec<int>> & d,int q0,vec<int> & f)3 void regular_check(vec<IntVar*>& x, int q, int s, vec<vec<int> >& d, int q0, vec<int>& f) {
4 assert(q >= 1);
5 assert(s >= 1);
6 assert(d.size() == q);
7 for (int i = 0; i < q; i++) {
8 assert(d[i].size() == s);
9 for (int j = 0; j < s; j++) {
10 assert(d[i][j] >= 0 && d[i][j] <= q);
11 }
12 }
13 assert(q0 >= 1 && q0 <= q);
14 for (int i = 0; i < f.size(); i++) assert(f[i] >= 1 && f[i] <= q);
15 }
16
17
regular(vec<IntVar * > & x,int q,int s,vec<vec<int>> & d,int q0,vec<int> & f)18 void regular(vec<IntVar*>& x, int q, int s, vec<vec<int> >& d, int q0, vec<int>& f) {
19 regular_check(x, q, s, d, q0, f);
20 // bool accept[q+1];
21 bool* accept = new bool[q+1];
22 for (int i = 0; i <= q; i++) accept[i] = false;
23 for (int i = 0; i < f.size(); i++) accept[f[i]] = true;
24 vec<vec<int> > start, middle, end;
25 for (int i = 0; i < q; i++) {
26 for (int j = 0; j < s; j++) {
27 if (!d[i][j]) continue;
28 if (i+1 == q0) {
29 start.push();
30 start.last().push(j+1);
31 start.last().push(d[i][j]);
32 }
33 middle.push();
34 middle.last().push(i+1);
35 middle.last().push(j+1);
36 middle.last().push(d[i][j]);
37 if (accept[d[i][j]]) {
38 end.push();
39 end.last().push(i+1);
40 end.last().push(j+1);
41 }
42 }
43 }
44 vec<IntVar*> y;
45 for (int i = 1; i < x.size(); i++) y.push(newIntVar(1,q));
46 vec<IntVar*> sx;
47 sx.push(x[0]); sx.push(y[0]);
48 table(sx, start);
49 for (int i = 1; i < x.size()-1; i++) {
50 vec<IntVar*> mx;
51 mx.push(y[i-1]); mx.push(x[i]); mx.push(y[i]);
52 table(mx, middle);
53 }
54 vec<IntVar*> ex;
55 ex.push(y.last()); ex.push(x.last());
56 table(ex, end);
57 delete[] accept;
58 }
59