📄 ac5.co
字号:
/*Constraint X[i] = (Y == i) */import cotfd;class Channel extends UserConstraint<CP>{ var<CP>{int}[] _x; var<CP>{int} _y; var<CP>{int}[] _vars; int n; Channel(var<CP>{int}[] x, var<CP>{int} y):UserConstraint<CP>(){ n = x.getSize(); _vars = new var<CP>{int}[1..n+1]; _x = new var<CP>{int}[1..n]; forall(i in 1..n){ _x[i] = x[i]; _vars[i] = x[i]; } _y = y; _vars[n+1] = y; } var<CP>{int}[] getIntVariables(){ return _vars; } Outcome<CP> post(Consistency<CP> cl){ //do a first propagation Outcome<CP> resPropag = propagate(); forall( i in 1..n){ _x[i].addAC5Index(this,i); } _y.addAC5Index(this,1); return resPropag; } //addAC5Index; Outcome<CP> valRemoveIdx(var<CP>{int}z, int idx, int val){ //when remove value val of _x[idx] if(z.getId() == _x[idx].getId()){ if(val == 1){ if(_y.removeValue(idx) == Failure) return Failure; } else { if(_y.bindValue(idx) == Failure) return Failure; } } //when remove value val of _y if(z.getId() == _y.getId()){ if(_x[val].removeValue(1) == Failure) return Failure; } return Suspend; } }Solver<CP> m();int t0 = System.getCPUTime();int n = 8; //1000;range S = 1..n;var<CP>{int} q[i in S](m,S);var<CP>{int}[] x[1..n];forall(i in S) x[i] = new var<CP>{int}[1..n](m,0..1);Integer np(m.getNPropag());cout << "Initiating search..." << endl;Integer c(0);solveall<m> { forall(i in S) m.post(Channel(x[i],q[i])); m.post(alldifferent(all(i in S) q[i] + i),onBounds); m.post(alldifferent(all(i in S) q[i] - i),onBounds); m.post(alldifferent(q),onBounds); } using { forall(i in S) label(x[i]); c := c + 1; cout << q << endl;}cout << "Nb : " << c << endl;int t1 = System.getCPUTime();cout << "time: " << t1 - t0 << endl;cout << "#choices = " << m.getNChoice() << endl;cout << "#fail = " << m.getNFail() << endl;cout << "#propag = " << m.getNPropag() - np << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -