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 <gecode/int/rel.hh>
00039 #include <gecode/int/distinct.hh>
00040
00041 namespace Gecode { namespace Int { namespace Sorted {
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00075 template<class View, bool Perm>
00076 ExecStatus
00077 bounds_propagation(Space& home, Propagator& p,
00078 ViewArray<View>& x,
00079 ViewArray<View>& y,
00080 ViewArray<View>& z,
00081 bool& repairpass,
00082 bool& nofix,
00083 bool& match_fixed){
00084
00085 int n = x.size();
00086
00087 Region r(home);
00088 int* tau = r.alloc<int>(n);
00089 int* phi = r.alloc<int>(n);
00090 int* phiprime = r.alloc<int>(n);
00091 OfflineMinItem* sequence = r.alloc<OfflineMinItem>(n);
00092 int* vertices = r.alloc<int>(n);
00093
00094 if (match_fixed) {
00095
00096 for (int i=n; i--; )
00097 tau[z[i].val()] = i;
00098 } else {
00099 for (int i = n; i--; )
00100 tau[i] = i;
00101 }
00102
00103 if (Perm) {
00104
00105
00106
00107 Rank* allbnd = r.alloc<Rank>(x.size());
00108 #ifndef NDEBUG
00109 for (int i=n; i--;)
00110 allbnd[i].min = allbnd[i].max = -1;
00111 #endif
00112 for (int i=n; i--;) {
00113 int min = x[i].min();
00114 int max = x[i].max();
00115 for (int j=0; j<n; j++) {
00116 if ( (y[j].min() > min) ||
00117 (y[j].min() <= min && min <= y[j].max()) ) {
00118 allbnd[i].min = j;
00119 break;
00120 }
00121 }
00122 for (int j=n; j--;) {
00123 if (y[j].min() > max) {
00124 allbnd[i].max = j-1;
00125 break;
00126 } else if (y[j].min() <= max && min <= y[j].max()) {
00127 allbnd[i].max = j;
00128 break;
00129 }
00130 }
00131 }
00132
00133 for (int i = n; i--; ) {
00134
00135 int minr = allbnd[i].min;
00136 assert(minr != -1);
00137 int maxr = allbnd[i].max;
00138 assert(maxr != -1);
00139
00140 ModEvent me = x[i].gq(home, y[minr].min());
00141 if (me_failed(me))
00142 return ES_FAILED;
00143 nofix |= (me_modified(me) && (x[i].min() != y[minr].min()));
00144
00145 me = x[i].lq(home, y[maxr].max());
00146 if (me_failed(me))
00147 return ES_FAILED;
00148 nofix |= (me_modified(me) && (x[i].min() != y[maxr].max()));
00149
00150 me = z[i].gq(home, minr);
00151 if (me_failed(me))
00152 return ES_FAILED;
00153 nofix |= (me_modified(me) && (z[i].min() != minr));
00154
00155 me = z[i].lq(home, maxr);
00156 if (me_failed(me))
00157 return ES_FAILED;
00158 nofix |= (me_modified(me) && (z[i].max() != maxr));
00159 }
00160
00161
00162 if (!channel(home,x,y,z,nofix))
00163 return ES_FAILED;
00164 if (nofix)
00165 return ES_NOFIX;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 if (!normalize(home, y, x, nofix))
00178 return ES_FAILED;
00179
00180 if (Perm) {
00181
00182 if (!channel(home,x,y,z,nofix))
00183 return ES_FAILED;
00184 if (nofix)
00185 return ES_NOFIX;
00186 }
00187
00188
00189
00190
00191
00192 sort_sigma<View,Perm>(home,x,z);
00193
00194 bool subsumed = true;
00195 bool array_subs = false;
00196 int dropfst = 0;
00197 bool noperm_bc = false;
00198
00199 if (!check_subsumption<View,Perm>(x,y,z,subsumed,dropfst) ||
00200 !array_assigned<View,Perm>(home,x,y,z,array_subs,match_fixed,nofix,noperm_bc))
00201 return ES_FAILED;
00202
00203 if (subsumed || array_subs)
00204 return home.ES_SUBSUMED(p);
00205
00206
00207
00208
00209
00210
00211
00212 sort_tau<View,Perm>(x,z,tau);
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 if (!match_fixed) {
00225 if (!glover(x,y,tau,phi,sequence,vertices))
00226 return ES_FAILED;
00227 } else {
00228 for (int i = x.size(); i--; ) {
00229 phi[i] = z[i].val();
00230 phiprime[i] = phi[i];
00231 }
00232 }
00233
00234 for (int i = n; i--; )
00235 if (!y[i].assigned()) {
00236
00237 if (!match_fixed &&
00238 !revglover(x,y,tau,phiprime,sequence,vertices))
00239 return ES_FAILED;
00240
00241 if (!narrow_domy(home,x,y,phi,phiprime,nofix))
00242 return ES_FAILED;
00243
00244 if (nofix && !match_fixed) {
00245
00246
00247 for (int j = y.size(); j--; )
00248 phi[j]=phiprime[j]=0;
00249
00250 if (!glover(x,y,tau,phi,sequence,vertices))
00251 return ES_FAILED;
00252
00253 if (!revglover(x,y,tau,phiprime,sequence,vertices))
00254 return ES_FAILED;
00255
00256 if (!narrow_domy(home,x,y,phi,phiprime,nofix))
00257 return ES_FAILED;
00258 }
00259 break;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 int* scclist = r.alloc<int>(n);
00271 SccComponent* sinfo = r.alloc<SccComponent>(n);
00272
00273 for(int i = n; i--; )
00274 sinfo[i].left=sinfo[i].right=sinfo[i].rightmost=sinfo[i].leftmost= i;
00275
00276 computesccs(home,x,y,phi,sinfo,scclist);
00277
00278
00279
00280
00281
00282
00283
00284
00285 if (!narrow_domx<View,Perm>(home,x,y,z,tau,phi,scclist,sinfo,nofix))
00286 return ES_FAILED;
00287
00288 if (Perm) {
00289 if (!noperm_bc &&
00290 !perm_bc<View>
00291 (home, tau, sinfo, scclist, x,z, repairpass, nofix))
00292 return ES_FAILED;
00293
00294
00295
00296 if (!channel(home,x,y,z,nofix))
00297 return ES_FAILED;
00298 if (nofix)
00299 return ES_NOFIX;
00300 }
00301
00302 sort_tau<View,Perm>(x,z,tau);
00303
00304 if (Perm) {
00305
00306
00307
00308 for (int i = x.size() - 1; i--; ) {
00309
00310 if (z[tau[i]].min() == z[tau[i+1]].min() &&
00311 z[tau[i]].max() == z[tau[i+1]].max() &&
00312 z[tau[i]].size() == 2 && z[tau[i]].range()) {
00313
00314 if (x[tau[i]].max() < x[tau[i+1]].max()) {
00315 ModEvent me = y[z[tau[i]].min()].lq(home, x[tau[i]].max());
00316 if (me_failed(me))
00317 return ES_FAILED;
00318 nofix |= (me_modified(me) &&
00319 y[z[tau[i]].min()].max() != x[tau[i]].max());
00320
00321 me = y[z[tau[i+1]].max()].lq(home, x[tau[i+1]].max());
00322 if (me_failed(me))
00323 return ES_FAILED;
00324 nofix |= (me_modified(me) &&
00325 y[z[tau[i+1]].max()].max() != x[tau[i+1]].max());
00326 }
00327 }
00328 }
00329 }
00330 return nofix ? ES_NOFIX : ES_FIX;
00331 }
00332
00333 template<class View, bool Perm>
00334 forceinline Sorted<View,Perm>::
00335 Sorted(Space& home, bool share, Sorted<View,Perm>& p):
00336 Propagator(home, share, p),
00337 reachable(p.reachable) {
00338 x.update(home, share, p.x);
00339 y.update(home, share, p.y);
00340 z.update(home, share, p.z);
00341 w.update(home, share, p.w);
00342 }
00343
00344 template<class View, bool Perm>
00345 Sorted<View,Perm>::
00346 Sorted(Home home,
00347 ViewArray<View>& x0, ViewArray<View>& y0, ViewArray<View>& z0) :
00348 Propagator(home), x(x0), y(y0), z(z0), w(home,y0), reachable(-1) {
00349 x.subscribe(home, *this, PC_INT_BND);
00350 y.subscribe(home, *this, PC_INT_BND);
00351 if (Perm)
00352 z.subscribe(home, *this, PC_INT_BND);
00353 }
00354
00355 template<class View, bool Perm>
00356 forceinline size_t
00357 Sorted<View,Perm>::dispose(Space& home) {
00358 x.cancel(home,*this, PC_INT_BND);
00359 y.cancel(home,*this, PC_INT_BND);
00360 if (Perm)
00361 z.cancel(home,*this, PC_INT_BND);
00362 (void) Propagator::dispose(home);
00363 return sizeof(*this);
00364 }
00365
00366 template<class View, bool Perm>
00367 Actor* Sorted<View,Perm>::copy(Space& home, bool share) {
00368 return new (home) Sorted<View,Perm>(home, share, *this);
00369 }
00370
00371 template<class View, bool Perm>
00372 PropCost Sorted<View,Perm>::cost(const Space&, const ModEventDelta&) const {
00373 return PropCost::linear(PropCost::LO, x.size());
00374 }
00375
00376 template<class View, bool Perm>
00377 ExecStatus
00378 Sorted<View,Perm>::propagate(Space& home, const ModEventDelta&) {
00379 int n = x.size();
00380 bool secondpass = false;
00381 bool nofix = false;
00382 int dropfst = 0;
00383
00384 bool subsumed = false;
00385 bool array_subs = false;
00386 bool match_fixed = false;
00387
00388
00389 if (!normalize(home, y, x, nofix))
00390 return ES_FAILED;
00391
00392
00393 sort_sigma<View,Perm>(home,x,z);
00394
00395 bool noperm_bc = false;
00396 if (!array_assigned<View,Perm>
00397 (home, x, y, z, array_subs, match_fixed, nofix, noperm_bc))
00398 return ES_FAILED;
00399
00400 if (array_subs)
00401 return home.ES_SUBSUMED(*this);
00402
00403 sort_sigma<View,Perm>(home,x,z);
00404
00405
00406
00407
00408 if (!check_subsumption<View,Perm>(x,y,z,subsumed,dropfst))
00409 return ES_FAILED;
00410
00411 if (subsumed)
00412 return home.ES_SUBSUMED(*this);
00413
00414 if (Perm) {
00415
00416 if (dropfst) {
00417 reachable = w[dropfst - 1].max();
00418 bool unreachable = true;
00419 for (int i = x.size(); unreachable && i-- ; ) {
00420 unreachable &= (reachable < x[i].min());
00421 }
00422
00423 if (unreachable) {
00424 x.drop_fst(dropfst, home, *this, PC_INT_BND);
00425 y.drop_fst(dropfst, home, *this, PC_INT_BND);
00426 z.drop_fst(dropfst, home, *this, PC_INT_BND);
00427 } else {
00428 dropfst = 0;
00429 }
00430 }
00431
00432 n = x.size();
00433
00434 if (n < 2) {
00435 if (x[0].max() < y[0].min() || y[0].max() < x[0].min())
00436 return ES_FAILED;
00437 if (Perm) {
00438 GECODE_ME_CHECK(z[0].eq(home, w.size() - 1));
00439 }
00440 GECODE_REWRITE(*this,(Rel::EqBnd<View,View>::post(home(*this), x[0], y[0])));
00441 }
00442
00443
00444
00445
00446 int valid = n - 1;
00447 int index = 0;
00448 int shift = 0;
00449
00450 for (int i = n; i--; ){
00451 if (z[i].max() > index)
00452 index = z[i].max();
00453 if (index > valid)
00454 shift = index - valid;
00455 }
00456
00457 if (shift) {
00458 ViewArray<OffsetView> ox(home,n), oy(home,n), oz(home,n);
00459
00460 for (int i = n; i--; ) {
00461 GECODE_ME_CHECK(z[i].gq(home, shift));
00462
00463 oz[i] = OffsetView(z[i], -shift);
00464 ox[i] = OffsetView(x[i], 0);
00465 oy[i] = OffsetView(y[i], 0);
00466 }
00467
00468 GECODE_ES_CHECK((bounds_propagation<OffsetView,Perm>
00469 (home,*this,ox,oy,oz,secondpass,nofix,match_fixed)));
00470
00471 if (secondpass) {
00472 GECODE_ES_CHECK((bounds_propagation<OffsetView,Perm>
00473 (home,*this,ox,oy,oz,secondpass,nofix,match_fixed)));
00474 }
00475 } else {
00476 GECODE_ES_CHECK((bounds_propagation<View,Perm>
00477 (home,*this,x,y,z,secondpass,nofix,match_fixed)));
00478
00479 if (secondpass) {
00480 GECODE_ES_CHECK((bounds_propagation<View,Perm>
00481 (home,*this,x,y,z,secondpass,nofix,match_fixed)));
00482 }
00483 }
00484 } else {
00485
00486 if (dropfst) {
00487 x.drop_fst(dropfst, home, *this, PC_INT_BND);
00488 y.drop_fst(dropfst, home, *this, PC_INT_BND);
00489 }
00490
00491 n = x.size();
00492
00493 if (n < 2) {
00494 if (x[0].max() < y[0].min() || y[0].max() < x[0].min())
00495 return ES_FAILED;
00496 GECODE_REWRITE(*this,(Rel::EqBnd<View,View>::post(home(*this), x[0], y[0])));
00497 }
00498
00499 GECODE_ES_CHECK((bounds_propagation<View,Perm>
00500 (home, *this, x, y, z,secondpass, nofix, match_fixed)));
00501
00502 }
00503
00504 if (!normalize(home, y, x, nofix))
00505 return ES_FAILED;
00506
00507 Region r(home);
00508 int* tau = r.alloc<int>(n);
00509 if (match_fixed) {
00510
00511
00512 for (int i = x.size(); i--; ) {
00513 int pi = z[i].val();
00514 tau[pi] = i;
00515 }
00516 } else {
00517 for (int i = n; i--; ) {
00518 tau[i] = i;
00519 }
00520 }
00521
00522 sort_tau<View,Perm>(x,z,tau);
00523
00524
00525 bool xbassigned = true;
00526 for (int i = x.size(); i--; ) {
00527 if (x[tau[i]].assigned() && xbassigned) {
00528 GECODE_ME_CHECK(y[i].eq(home, x[tau[i]].val()));
00529 } else {
00530 xbassigned = false;
00531 }
00532 }
00533
00534 subsumed = true;
00535 array_subs = false;
00536 noperm_bc = false;
00537
00538
00539 sort_sigma<View,Perm>(home,x,z);
00540
00541 if (Perm) {
00542 for (int i = 0; i < x.size() - 1; i++) {
00543
00544
00545 if (z[i].min() == z[i+1].min() &&
00546 z[i].max() == z[i+1].max() &&
00547 z[i].size() == 2 && z[i].range()) {
00548 if (x[i].min() < x[i+1].min()) {
00549 ModEvent me = y[z[i].min()].gq(home, x[i].min());
00550 GECODE_ME_CHECK(me);
00551 nofix |= (me_modified(me) &&
00552 y[z[i].min()].min() != x[i].min());
00553
00554 me = y[z[i+1].max()].gq(home, x[i+1].min());
00555 GECODE_ME_CHECK(me);
00556 nofix |= (me_modified(me) &&
00557 y[z[i+1].max()].min() != x[i+1].min());
00558 }
00559 }
00560 }
00561 }
00562
00563
00564
00565 bool xassigned = true;
00566 for (int i = 0; i < x.size(); i++) {
00567 if (x[i].assigned() && xassigned) {
00568 GECODE_ME_CHECK(y[i].eq(home,x[i].val()));
00569 } else {
00570 xassigned = false;
00571 }
00572 }
00573
00574
00575
00576
00577 int tlb = std::min(x[0].min(), y[0].min());
00578 int tub = std::max(x[x.size() - 1].max(), y[y.size() - 1].max());
00579 for (int i = x.size(); i--; ) {
00580 ModEvent me = y[i].lq(home, tub);
00581 GECODE_ME_CHECK(me);
00582 nofix |= me_modified(me) && (y[i].max() != tub);
00583
00584 me = y[i].gq(home, tlb);
00585 GECODE_ME_CHECK(me);
00586 nofix |= me_modified(me) && (y[i].min() != tlb);
00587
00588 me = x[i].lq(home, tub);
00589 GECODE_ME_CHECK(me);
00590 nofix |= me_modified(me) && (x[i].max() != tub);
00591
00592 me = x[i].gq(home, tlb);
00593 GECODE_ME_CHECK(me);
00594 nofix |= me_modified(me) && (x[i].min() != tlb);
00595 }
00596
00597 if (!array_assigned<View,Perm>
00598 (home, x, y, z, array_subs, match_fixed, nofix, noperm_bc))
00599 return ES_FAILED;
00600
00601 if (array_subs)
00602 return home.ES_SUBSUMED(*this);
00603
00604 if (!check_subsumption<View,Perm>(x,y,z,subsumed,dropfst))
00605 return ES_FAILED;
00606
00607 if (subsumed)
00608 return home.ES_SUBSUMED(*this);
00609
00610 return nofix ? ES_NOFIX : ES_FIX;
00611 }
00612
00613 template<class View, bool Perm>
00614 ExecStatus
00615 Sorted<View,Perm>::
00616 post(Home home,
00617 ViewArray<View>& x0, ViewArray<View>& y0, ViewArray<View>& z0) {
00618 int n = x0.size();
00619 if (n < 2) {
00620 if ((x0[0].max() < y0[0].min()) || (y0[0].max() < x0[0].min()))
00621 return ES_FAILED;
00622 GECODE_ES_CHECK((Rel::EqBnd<View,View>::post(home,x0[0],y0[0])));
00623 if (Perm) {
00624 GECODE_ME_CHECK(z0[0].eq(home,0));
00625 }
00626 } else {
00627 if (Perm) {
00628 ViewArray<View> z(home,n);
00629 for (int i=n; i--; ) {
00630 z[i]=z0[i];
00631 GECODE_ME_CHECK(z[i].gq(home,0));
00632 GECODE_ME_CHECK(z[i].lq(home,n-1));
00633 }
00634 GECODE_ES_CHECK(Distinct::Bnd<View>::post(home,z));
00635 }
00636 new (home) Sorted<View,Perm>(home,x0,y0,z0);
00637 }
00638 return ES_OK;
00639 }
00640
00641 }}}
00642
00643
00644
00645
00646