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
00039
00040
00041
00042 namespace Gecode { namespace Int { namespace Sequence {
00043
00044 template<class View, class Val>
00045 forceinline
00046 Sequence<View,Val>::Sequence(Home home, ViewArray<View>& x0, Val s0,
00047 int q0, int l0, int u0)
00048 : Propagator(home), x(x0), s(s0), q(q0), l(l0), u(u0),
00049 vvsamax(home,x,s0,q0), vvsamin(home,x,s0,q0), ac(home),
00050 tofail(false) {
00051 home.notice(*this,AP_DISPOSE);
00052 for (int i=x.size(); i--; ) {
00053 if (undecided(x[i],s)) {
00054 x[i].subscribe(home,*new (home) SupportAdvisor<View>(home,*this,ac,i));
00055 } else {
00056 x[i].schedule(home,*this,x[i].assigned() ? ME_INT_VAL : ME_INT_BND);
00057 }
00058 }
00059 }
00060
00061 namespace {
00063 template<class Val>
00064 class UpdateVal {
00065 public:
00066 static void update(Val& n, Space& home, bool share, Val& old);
00067 };
00069 template<>
00070 class UpdateVal<int> {
00071 public:
00072 static void update(int& n, Space&, bool, int& old) {
00073 n = old;
00074 }
00075 };
00077 template<>
00078 class UpdateVal<IntSet> {
00079 public:
00080 static void update(IntSet& n, Space& home, bool share,
00081 IntSet& old) {
00082 n.update(home,share,old);
00083 }
00084 };
00085 }
00086
00087 template<class View, class Val>
00088 forceinline
00089 Sequence<View,Val>::Sequence(Space& home, bool share, Sequence& p)
00090 : Propagator(home,share,p), q(p.q), l(p.l), u(p.u),
00091 vvsamax(), vvsamin(), tofail(p.tofail) {
00092 UpdateVal<Val>::update(s,home,share,p.s);
00093 x.update(home,share,p.x);
00094 ac.update(home,share,p.ac);
00095 vvsamax.update(home,share,p.vvsamax);
00096 vvsamin.update(home,share,p.vvsamin);
00097 }
00098
00099 template<class View,class Val>
00100 ExecStatus
00101 Sequence<View,Val>::advise(Space& home, Advisor& _a, const Delta& d) {
00102 SupportAdvisor<View>& a = static_cast<SupportAdvisor<View>&>(_a);
00103 ExecStatus status = vvsamax.advise(home,x,s,q,a.i,d);
00104 if ( ES_NOFIX == vvsamin.advise(home,x,s,q,a.i,d) ) {
00105 status = ES_NOFIX;
00106 }
00107
00108 if (!undecided(x[a.i],s)) {
00109 if (!x[a.i].assigned())
00110 x[a.i].cancel(home,a);
00111
00112 if ( ES_NOFIX == status ) {
00113 return home.ES_NOFIX_DISPOSE(ac,a);
00114 } else {
00115 return home.ES_FIX_DISPOSE(ac,a);
00116 }
00117 }
00118
00119 if ((status == ES_FAILED) && disabled()) {
00120 tofail = true;
00121 return ES_FIX;
00122 }
00123
00124 return status;
00125 }
00126
00127 template<class View, class Val>
00128 forceinline size_t
00129 Sequence<View,Val>::dispose(Space& home) {
00130 home.ignore(*this,AP_DISPOSE);
00131 ac.dispose(home);
00132 s.~Val();
00133 (void) Propagator::dispose(home);
00134 return sizeof(*this);
00135 }
00136
00137 template<class View, class Val>
00138 forceinline ExecStatus
00139 Sequence<View,Val>::check(Space& home, ViewArray<View>& x, Val s, int q, int l, int u) {
00140 Region r(home);
00141
00142 int* upper = r.alloc<int>(x.size()+1);
00143 int* lower = r.alloc<int>(x.size()+1);
00144 upper[0] = 0;
00145 lower[0] = 0;
00146 for ( int j=0; j<x.size(); j++ ) {
00147 upper[j+1] = upper[j];
00148 lower[j+1] = lower[j];
00149 if (includes(x[j],s)) {
00150 upper[j+1] += 1;
00151 } else if (excludes(x[j],s)) {
00152 lower[j+1] += 1;
00153 }
00154 if ( j+1 >= q && (q - l < lower[j+1] - lower[j+1-q] || upper[j+1] - upper[j+1-q] > u) ) {
00155 return ES_FAILED;
00156 }
00157 }
00158 return ES_OK;
00159 }
00160
00161 template<class View, class Val>
00162 ExecStatus
00163 Sequence<View,Val>::post(Home home, ViewArray<View>& x, Val s, int q, int l, int u) {
00164 GECODE_ES_CHECK(check(home,x,s,q,l,u));
00165 Sequence<View,Val>* p = new (home) Sequence<View,Val>(home,x,s,q,l,u);
00166
00167 GECODE_ES_CHECK(p->vvsamax.propagate(home,x,s,q,l,u));
00168 GECODE_ES_CHECK(p->vvsamin.propagate(home,x,s,q,l,u));
00169
00170 return ES_OK;
00171 }
00172
00173 template<class View, class Val>
00174 Actor*
00175 Sequence<View,Val>::copy(Space& home, bool share) {
00176 return new (home) Sequence<View,Val>(home,share,*this);
00177 }
00178
00179 template<class View, class Val>
00180 PropCost
00181 Sequence<View,Val>::cost(const Space&, const ModEventDelta&) const {
00182 return PropCost::cubic(PropCost::HI,x.size());
00183 }
00184
00185 template<class View, class Val>
00186 void
00187 Sequence<View,Val>::reschedule(Space& home) {
00188 for (int i=x.size(); i--; )
00189 if (!undecided(x[i],s))
00190 x[i].schedule(home,*this,x[i].assigned() ? ME_INT_VAL : ME_INT_BND);
00191 if (tofail)
00192 View::schedule(home,*this,ME_INT_BND);
00193 }
00194
00195 template<class View, class Val>
00196 ExecStatus
00197 Sequence<View,Val>::propagate(Space& home, const ModEventDelta&) {
00198 if (tofail)
00199 return ES_FAILED;
00200
00201 GECODE_ES_CHECK(vvsamax.propagate(home,x,s,q,l,u));
00202 GECODE_ES_CHECK(vvsamin.propagate(home,x,s,q,l,u));
00203
00204 for (int i=x.size(); i--; )
00205 if (undecided(x[i],s))
00206 return ES_FIX;
00207
00208 return home.ES_SUBSUMED(*this);
00209 }
00210
00211 }}}
00212
00213
00214