Generated on Thu Apr 11 13:59:07 2019 for Gecode by doxygen 1.6.3

bnd.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00005  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *     Guido Tack <tack@gecode.org>
00009  *
00010  *  Copyright:
00011  *     Patrick Pekczynski, 2004/2005
00012  *     Christian Schulte, 2009
00013  *     Guido Tack, 2009
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 namespace Gecode { namespace Int { namespace GCC {
00041 
00042   template<class Card>
00043   forceinline
00044   Bnd<Card>::
00045   Bnd(Home home, ViewArray<IntView>& x0, ViewArray<Card>& k0,
00046          bool cf, bool nolbc) :
00047     Propagator(home), x(x0), y(home, x0), k(k0),
00048     card_fixed(cf), skip_lbc(nolbc) {
00049     y.subscribe(home, *this, PC_INT_BND);
00050     k.subscribe(home, *this, PC_INT_BND);
00051   }
00052 
00053   template<class Card>
00054   forceinline
00055   Bnd<Card>::
00056   Bnd(Space& home, Bnd<Card>& p)
00057     : Propagator(home, p),
00058       card_fixed(p.card_fixed), skip_lbc(p.skip_lbc) {
00059     x.update(home, p.x);
00060     y.update(home, p.y);
00061     k.update(home, p.k);
00062   }
00063 
00064   template<class Card>
00065   forceinline size_t
00066   Bnd<Card>::dispose(Space& home) {
00067     y.cancel(home,*this, PC_INT_BND);
00068     k.cancel(home,*this, PC_INT_BND);
00069     (void) Propagator::dispose(home);
00070     return sizeof(*this);
00071   }
00072 
00073   template<class Card>
00074   Actor*
00075   Bnd<Card>::copy(Space& home) {
00076     return new (home) Bnd<Card>(home,*this);
00077   }
00078 
00079   template<class Card>
00080   PropCost
00081   Bnd<Card>::cost(const Space&,
00082                             const ModEventDelta& med) const {
00083     int n_k = Card::propagate ? k.size() : 0;
00084     if (IntView::me(med) == ME_INT_VAL)
00085       return PropCost::linear(PropCost::LO, y.size() + n_k);
00086     else
00087       return PropCost::quadratic(PropCost::LO, x.size() + n_k);
00088   }
00089 
00090 
00091   template<class Card>
00092   void
00093   Bnd<Card>::reschedule(Space& home) {
00094     y.reschedule(home, *this, PC_INT_BND);
00095     k.reschedule(home, *this, PC_INT_BND);
00096   }
00097 
00098   template<class Card>
00099   forceinline ExecStatus
00100   Bnd<Card>::lbc(Space& home, int& nb,
00101                            HallInfo hall[], Rank rank[], int mu[], int nu[]) {
00102     int n = x.size();
00103 
00104     /*
00105      *  Let I(S) denote the number of variables whose domain intersects
00106      *  the set S and C(S) the number of variables whose domain is containded
00107      *  in S. Let further  min_cap(S) be the minimal number of variables
00108      *  that must be assigned to values, that is
00109      *  min_cap(S) is the sum over all l[i] for a value v_i that is an
00110      *  element of S.
00111      *
00112      *  A failure set is a set F if
00113      *       I(F) < min_cap(F)
00114      *  An unstable set is a set U if
00115      *       I(U) = min_cap(U)
00116      *  A stable set is a set S if
00117      *      C(S) > min_cap(S) and S intersetcs nor
00118      *      any failure set nor any unstable set
00119      *      forall unstable and failure sets
00120      *
00121      *  failure sets determine the satisfiability of the LBC
00122      *  unstable sets have to be pruned
00123      *  stable set do not have to be pruned
00124      *
00125      * hall[].ps ~ stores the unstable
00126      *             sets that have to be pruned
00127      * hall[].s  ~ stores sets that must not be pruned
00128      * hall[].h  ~ contains stable and unstable sets
00129      * hall[].d  ~ contains the difference between interval bounds, i.e.
00130      *             the minimal capacity of the interval
00131      * hall[].t  ~ contains the critical capacity pointer, pointing to the
00132      *             values
00133      */
00134 
00135     // LBC lower bounds
00136 
00137     int i = 0;
00138     int j = 0;
00139     int w = 0;
00140     int z = 0;
00141     int v = 0;
00142 
00143     //initialization of the tree structure
00144     int rightmost = nb + 1; // rightmost accesible value in bounds
00145     int bsize     = nb + 2;
00146     w = rightmost;
00147 
00148     // test
00149     // unused but uninitialized
00150     hall[0].d = 0;
00151     hall[0].s = 0;
00152     hall[0].ps = 0;
00153 
00154     for (i = bsize; --i; ) { // i must not be zero
00155       int pred = i - 1;
00156       hall[i].s = pred;
00157       hall[i].ps = pred;
00158       hall[i].d = lps.sumup(hall[pred].bounds, hall[i].bounds - 1);
00159 
00160       /* Let [hall[i].bounds,hall[i-1].bounds]=:I
00161        * If the capacity is zero => min_cap(I) = 0
00162        * => I cannot be a failure set
00163        * => I is an unstable set
00164        */
00165       if (hall[i].d == 0) {
00166         hall[pred].h = w;
00167       } else {
00168         hall[w].h = pred;
00169         w = pred;
00170       }
00171     }
00172 
00173     w = rightmost;
00174     for (i = bsize; i--; ) { // i can be zero
00175       hall[i].t = i - 1;
00176       if (hall[i].d == 0) {
00177         hall[i].t = w;
00178       } else {
00179         hall[w].t = i;
00180         w = i;
00181       }
00182     }
00183 
00184     /*
00185      * The algorithm assigns to each value v in bounds
00186      * empty buckets corresponding to the minimal capacity l[i] to be
00187      * filled for v. (the buckets correspond to hall[].d containing the
00188      * difference between the interval bounds) Processing it
00189      * searches for the smallest value v in dom(x_i) that has an
00190      * empty bucket, i.e. if all buckets are filled it is guaranteed
00191      * that there are at least l[i] variables that will be
00192      * instantiated to v. Since the buckets are initially empty,
00193      * they are considered as FAILURE SETS
00194      */
00195 
00196     for (i = 0; i < n; i++) {
00197       // visit intervals in increasing max order
00198       int x0 = rank[mu[i]].min;
00199       int y = rank[mu[i]].max;
00200       int succ = x0 + 1;
00201       z = pathmax_t(hall, succ);
00202       j = hall[z].t;
00203 
00204       /*
00205        * POTENTIALLY STABLE SET:
00206        *  z \neq succ \Leftrigharrow z>succ, i.e.
00207        *  min(D_{\mu(i)}) is guaranteed to occur min(K_i) times
00208        *  \Rightarrow [x0, min(y,z)] is potentially stable
00209        */
00210 
00211       if (z != succ) {
00212         w = pathmax_ps(hall, succ);
00213         v = hall[w].ps;
00214         pathset_ps(hall, succ, w, w);
00215         w = std::min(y, z);
00216         pathset_ps(hall, hall[w].ps, v, w);
00217         hall[w].ps = v;
00218       }
00219 
00220       /*
00221        * STABLE SET:
00222        *   being stable implies being potentially stable, i.e.
00223        *   [hall[y].ps, hall[y].bounds-1] is the largest stable subset of
00224        *   [hall[j].bounds, hall[y].bounds-1].
00225        */
00226 
00227       if (hall[z].d <= lps.sumup(hall[y].bounds, hall[z].bounds - 1)) {
00228         w = pathmax_s(hall, hall[y].ps);
00229         pathset_s(hall, hall[y].ps, w, w);
00230         // Path compression
00231         v = hall[w].s;
00232         pathset_s(hall, hall[y].s, v, y);
00233         hall[y].s = v;
00234       } else {
00235         /*
00236          * FAILURE SET:
00237          * If the considered interval [x0,y] is neither POTENTIALLY STABLE
00238          * nor STABLE there are still buckets that can be filled,
00239          * therefore d can be decreased. If d equals zero the intervals
00240          * minimum capacity is met and thepath can be compressed to the
00241          * next value having an empty bucket.
00242          * see DOMINATION in "ubc"
00243          */
00244         if (--hall[z].d == 0) {
00245           hall[z].t = z + 1;
00246           z = pathmax_t(hall, hall[z].t);
00247           hall[z].t = j;
00248         }
00249 
00250         /*
00251          * FINDING NEW LOWER BOUND:
00252          * If the lower bound belongs to an unstable or a stable set,
00253          * remind the new value we might assigned to the lower bound
00254          * in case the variable doesn't belong to a stable set.
00255          */
00256         if (hall[x0].h > x0) {
00257           hall[i].newBound = pathmax_h(hall, x0);
00258           w = hall[i].newBound;
00259           pathset_h(hall, x0, w, w); // path compression
00260         } else {
00261           // Do not shrink the variable: take old min as new min
00262           hall[i].newBound = x0;
00263         }
00264 
00265         /* UNSTABLE SET
00266          * If an unstable set is discovered
00267          * the difference between the interval bounds is equal to the
00268          * number of variables whose domain intersect the interval
00269          * (see ZEROTEST in "gcc")
00270          */
00271         // CLEARLY THIS WAS NOT STABLE == UNSTABLE
00272         if (hall[z].d == lps.sumup(hall[y].bounds, hall[z].bounds - 1)) {
00273           if (hall[y].h > y)
00274             /*
00275              * y is not the end of the potentially stable set
00276              * thus ensure that the potentially stable superset is marked
00277              */
00278             y = hall[y].h;
00279           // Equivalent to pathmax since the path is fully compressed
00280           pathset_h(hall, hall[y].h, j-1, y);
00281           // mark the new unstable set [j,y]
00282           hall[y].h = j-1;
00283         }
00284       }
00285       pathset_t(hall, succ, z, z); // path compression
00286     }
00287 
00288     /* If there is a FAILURE SET left the minimum occurences of the values
00289      * are not guaranteed. In order to satisfy the LBC the last value
00290      * in the stable and unstable datastructure hall[].h must point to
00291      * the sentinel at the beginning of bounds.
00292      */
00293     if (hall[nb].h != 0)
00294       return ES_FAILED;
00295 
00296     // Perform path compression over all elements in
00297     // the stable interval data structure. This data
00298     // structure will no longer be modified and will be
00299     // accessed n or 2n times. Therefore, we can afford
00300     // a linear time compression.
00301     for (i = bsize; --i;)
00302       if (hall[i].s > i)
00303         hall[i].s = w;
00304       else
00305         w = i;
00306 
00307     /*
00308      * UPDATING LOWER BOUND:
00309      * For all variables that are not a subset of a stable set,
00310      * shrink the lower bound, i.e. forall stable sets S we have:
00311      * x0 < S_min <= y <=S_max  or S_min <= x0 <= S_max < y
00312      * that is [x0,y] is NOT a proper subset of any stable set S
00313      */
00314     for (i = n; i--; ) {
00315       int x0 = rank[mu[i]].min;
00316       int y = rank[mu[i]].max;
00317       // update only those variables that are not contained in a stable set
00318       if ((hall[x0].s <= x0) || (y > hall[x0].s)) {
00319         // still have to check this out, how skipping works (consider dominated indices)
00320         int m = lps.skipNonNullElementsRight(hall[hall[i].newBound].bounds);
00321         GECODE_ME_CHECK(x[mu[i]].gq(home, m));
00322       }
00323     }
00324 
00325     // LBC narrow upper bounds
00326     w = 0;
00327     for (i = 0; i <= nb; i++) {
00328       hall[i].d = lps.sumup(hall[i].bounds, hall[i + 1].bounds - 1);
00329       if (hall[i].d == 0) {
00330         hall[i].t = w;
00331       } else {
00332         hall[w].t = i;
00333         w = i;
00334       }
00335     }
00336     hall[w].t = i;
00337 
00338     w = 0;
00339     for (i = 1; i <= nb; i++)
00340       if (hall[i - 1].d == 0) {
00341         hall[i].h = w;
00342       } else {
00343         hall[w].h = i;
00344         w = i;
00345       }
00346     hall[w].h = i;
00347 
00348     for (i = n; i--; ) {
00349       // visit intervals in decreasing min order
00350       // i.e. minsorted from right to left
00351       int x0 = rank[nu[i]].max;
00352       int y = rank[nu[i]].min;
00353       int pred = x0 - 1; // predecessor of x0 in the indices
00354       z = pathmin_t(hall, pred);
00355       j = hall[z].t;
00356 
00357       /* If the variable is not in a discovered stable set
00358        * (see above condition for STABLE SET)
00359        */
00360       if (hall[z].d > lps.sumup(hall[z].bounds, hall[y].bounds - 1)) {
00361         // FAILURE SET
00362         if (--hall[z].d == 0) {
00363           hall[z].t = z - 1;
00364           z = pathmin_t(hall, hall[z].t);
00365           hall[z].t = j;
00366         }
00367         // FINDING NEW UPPER BOUND
00368         if (hall[x0].h < x0) {
00369           w = pathmin_h(hall, hall[x0].h);
00370           hall[i].newBound = w;
00371           pathset_h(hall, x0, w, w); // path compression
00372         } else {
00373           hall[i].newBound = x0;
00374         }
00375         // UNSTABLE SET
00376         if (hall[z].d == lps.sumup(hall[z].bounds, hall[y].bounds - 1)) {
00377           if (hall[y].h < y) {
00378             y = hall[y].h;
00379           }
00380           int succj = j + 1;
00381           // mark new unstable set [y,j]
00382           pathset_h(hall, hall[y].h, succj, y);
00383           hall[y].h = succj;
00384         }
00385       }
00386       pathset_t(hall, pred, z, z);
00387     }
00388 
00389     // UPDATING UPPER BOUND
00390     for (i = n; i--; ) {
00391       int x0 =  rank[nu[i]].min;
00392       int y  =  rank[nu[i]].max;
00393       if ((hall[x0].s <= x0) || (y > hall[x0].s)) {
00394         int m = lps.skipNonNullElementsLeft(hall[hall[i].newBound].bounds - 1);
00395         GECODE_ME_CHECK(x[nu[i]].lq(home, m));
00396       }
00397     }
00398     return ES_OK;
00399   }
00400 
00401 
00402   template<class Card>
00403   forceinline ExecStatus
00404   Bnd<Card>::ubc(Space& home, int& nb,
00405                            HallInfo hall[], Rank rank[], int mu[], int nu[]) {
00406     int rightmost = nb + 1; // rightmost accesible value in bounds
00407     int bsize = nb + 2; // number of unique bounds including sentinels
00408 
00409     //Narrow lower bounds (UBC)
00410 
00411     /*
00412      * Initializing tree structure with the values from bounds
00413      * and the interval capacities of neighboured values
00414      * from left to right
00415      */
00416 
00417 
00418     hall[0].h = 0;
00419     hall[0].t = 0;
00420     hall[0].d = 0;
00421 
00422     for (int i = bsize; --i; ) {
00423       hall[i].h = hall[i].t = i-1;
00424       hall[i].d = ups.sumup(hall[i-1].bounds, hall[i].bounds - 1);
00425     }
00426 
00427     int n = x.size();
00428 
00429     for (int i = 0; i < n; i++) {
00430       // visit intervals in increasing max order
00431       int x0   = rank[mu[i]].min;
00432       int succ = x0 + 1;
00433       int y    = rank[mu[i]].max;
00434       int z    = pathmax_t(hall, succ);
00435       int j    = hall[z].t;
00436 
00437       /* DOMINATION:
00438        *     v^i_j denotes
00439        *     unused values in the current interval. If the difference d
00440        *     between to critical capacities v^i_j and v^i_z
00441        *     is equal to zero, j dominates z
00442        *
00443        *     i.e. [hall[l].bounds, hall[nb+1].bounds] is not left-maximal and
00444        *     [hall[j].bounds, hall[l].bounds] is a Hall set iff
00445        *     [hall[j].bounds, hall[l].bounds] processing a variable x_i uses up a value in the interval
00446        *     [hall[z].bounds,hall[z+1].bounds] according to the intervals
00447        *     capacity. Therefore, if d = 0
00448        *     the considered value has already been used by processed variables
00449        *     m-times, where m = u[i] for value v_i. Since this value must not
00450        *     be reconsidered the path can be compressed
00451        */
00452       if (--hall[z].d == 0) {
00453         hall[z].t = z + 1;
00454         z = pathmax_t(hall, hall[z].t);
00455         if (z >= bsize)
00456           z--;
00457         hall[z].t = j;
00458       }
00459       pathset_t(hall, succ, z, z); // path compression
00460 
00461       /* NEGATIVE CAPACITY:
00462        *     A negative capacity results in a failure.Since a
00463        *     negative capacity signals that the number of variables
00464        *     whose domain is contained in the set S is larger than
00465        *     the maximum capacity of S => UBC is not satisfiable,
00466        *     i.e. there are more variables than values to instantiate them
00467        */
00468       if (hall[z].d < ups.sumup(hall[y].bounds, hall[z].bounds - 1))
00469         return ES_FAILED;
00470 
00471       /* UPDATING LOWER BOUND:
00472        *   If the lower bound min_i lies inside a Hall interval [a,b]
00473        *   i.e. a <= min_i <=b <= max_i
00474        *   min_i is set to  min_i := b+1
00475        */
00476       if (hall[x0].h > x0) {
00477         int w = pathmax_h(hall, hall[x0].h);
00478         int m = hall[w].bounds;
00479         GECODE_ME_CHECK(x[mu[i]].gq(home, m));
00480         pathset_h(hall, x0, w, w); // path compression
00481       }
00482 
00483       /* ZEROTEST:
00484        *     (using the difference between capacity pointers)
00485        *     zero capacity => the difference between critical capacity
00486        *     pointers is equal to the maximum capacity of the interval,i.e.
00487        *     the number of variables whose domain is contained in the
00488        *     interval is equal to the sum over all u[i] for a value v_i that
00489        *     lies in the Hall-Intervall which can also be thought of as a
00490        *     Hall-Set
00491        *
00492        *    ZeroTestLemma: Let k and l be succesive critical indices.
00493        *          v^i_k=0  =>  v^i_k = max_i+1-l+d
00494        *                   <=> v^i_k = y + 1 - z + d
00495        *                   <=> d = z-1-y
00496        *    if this equation holds the interval [j,z-1] is a hall intervall
00497        */
00498 
00499       if (hall[z].d == ups.sumup(hall[y].bounds, hall[z].bounds - 1)) {
00500         /*
00501          *mark hall interval [j,z-1]
00502          * hall pointers form a path to the upper bound of the interval
00503          */
00504         int predj = j - 1;
00505         pathset_h(hall, hall[y].h, predj, y);
00506         hall[y].h = predj;
00507       }
00508     }
00509 
00510     /* Narrow upper bounds (UBC)
00511      * symmetric to the narrowing of the lower bounds
00512      */
00513     for (int i = 0; i < rightmost; i++) {
00514       hall[i].h = hall[i].t = i+1;
00515       hall[i].d = ups.sumup(hall[i].bounds, hall[i+1].bounds - 1);
00516     }
00517 
00518     for (int i = n; i--; ) {
00519       // visit intervals in decreasing min order
00520       int x0 = rank[nu[i]].max;
00521       int pred = x0 - 1;
00522       int y = rank[nu[i]].min;
00523       int z = pathmin_t(hall, pred);
00524       int j = hall[z].t;
00525 
00526       // DOMINATION:
00527       if (--hall[z].d == 0) {
00528         hall[z].t = z - 1;
00529         z = pathmin_t(hall, hall[z].t);
00530         hall[z].t = j;
00531       }
00532       pathset_t(hall, pred, z, z);
00533 
00534       // NEGATIVE CAPACITY:
00535       if (hall[z].d < ups.sumup(hall[z].bounds,hall[y].bounds-1))
00536         return ES_FAILED;
00537 
00538       /* UPDATING UPPER BOUND:
00539        *   If the upper bound max_i lies inside a Hall interval [a,b]
00540        *   i.e. min_i <= a <= max_i < b
00541        *   max_i is set to  max_i := a-1
00542        */
00543       if (hall[x0].h < x0) {
00544         int w = pathmin_h(hall, hall[x0].h);
00545         int m = hall[w].bounds - 1;
00546         GECODE_ME_CHECK(x[nu[i]].lq(home, m));
00547         pathset_h(hall, x0, w, w);
00548       }
00549 
00550       // ZEROTEST
00551       if (hall[z].d == ups.sumup(hall[z].bounds, hall[y].bounds - 1)) {
00552         //mark hall interval [y,j]
00553         pathset_h(hall, hall[y].h, j+1, y);
00554         hall[y].h = j+1;
00555       }
00556     }
00557     return ES_OK;
00558   }
00559 
00560   template<class Card>
00561   ExecStatus
00562   Bnd<Card>::pruneCards(Space& home) {
00563     // Remove all values with 0 max occurrence
00564     // and remove corresponding occurrence variables from k
00565 
00566     // The number of zeroes
00567     int n_z = 0;
00568     for (int i=k.size(); i--;)
00569       if (k[i].max() == 0)
00570         n_z++;
00571 
00572     if (n_z > 0) {
00573       Region r;
00574       int* z = r.alloc<int>(n_z);
00575       n_z = 0;
00576       int n_k = 0;
00577       for (int i=0; i<k.size(); i++)
00578         if (k[i].max() == 0) {
00579           z[n_z++] = k[i].card();
00580         } else {
00581           k[n_k++] = k[i];
00582         }
00583       k.size(n_k);
00584       Support::quicksort(z,n_z);
00585       for (int i=x.size(); i--;) {
00586         Iter::Values::Array zi(z,n_z);
00587         GECODE_ME_CHECK(x[i].minus_v(home,zi,false));
00588       }
00589       lps.reinit(); ups.reinit();
00590     }
00591     return ES_OK;
00592   }
00593 
00594   template<class Card>
00595   ExecStatus
00596   Bnd<Card>::propagate(Space& home, const ModEventDelta& med) {
00597     if (IntView::me(med) == ME_INT_VAL) {
00598       GECODE_ES_CHECK(prop_val<Card>(home,*this,y,k));
00599       return home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_BND));
00600     }
00601 
00602     if (Card::propagate)
00603       GECODE_ES_CHECK(pruneCards(home));
00604 
00605     Region r;
00606     int* count = r.alloc<int>(k.size());
00607 
00608     for (int i = k.size(); i--; )
00609       count[i] = 0;
00610     bool all_assigned = true;
00611     int noa = 0;
00612     for (int i = x.size(); i--; ) {
00613       if (x[i].assigned()) {
00614         noa++;
00615         int idx;
00616         // reduction is essential for order on value nodes in dom
00617         // hence introduce test for failed lookup
00618         if (!lookupValue(k,x[i].val(),idx))
00619           return ES_FAILED;
00620         count[idx]++;
00621       } else {
00622         all_assigned = false;
00623         // We only need the counts in the view case or when all
00624         // x are assigned
00625         if (!Card::propagate)
00626           break;
00627       }
00628     }
00629 
00630     if (Card::propagate) {
00631       // before propagation performs inferences on cardinality variables:
00632       if (noa > 0)
00633         for (int i = k.size(); i--; )
00634           if (!k[i].assigned()) {
00635             GECODE_ME_CHECK(k[i].lq(home, x.size() - (noa - count[i])));
00636             GECODE_ME_CHECK(k[i].gq(home, count[i]));
00637           }
00638 
00639       if (!card_consistent<Card>(x, k))
00640         return ES_FAILED;
00641 
00642       GECODE_ES_CHECK(prop_card<Card>(home, x, k));
00643 
00644       // Cardinalities may have been modified, so recompute
00645       // count and all_assigned
00646       for (int i = k.size(); i--; )
00647         count[i] = 0;
00648       all_assigned = true;
00649       for (int i = x.size(); i--; ) {
00650         if (x[i].assigned()) {
00651           int idx;
00652           // reduction is essential for order on value nodes in dom
00653           // hence introduce test for failed lookup
00654           if (!lookupValue(k,x[i].val(),idx))
00655             return ES_FAILED;
00656           count[idx]++;
00657         } else {
00658           // We won't need the remaining counts, they're only used when
00659           // all x are assigned
00660           all_assigned = false;
00661           break;
00662         }
00663       }
00664     }
00665 
00666     if (all_assigned) {
00667       for (int i = k.size(); i--; )
00668         GECODE_ME_CHECK(k[i].eq(home, count[i]));
00669       return home.ES_SUBSUMED(*this);
00670     }
00671 
00672     if (Card::propagate)
00673       GECODE_ES_CHECK(pruneCards(home));
00674 
00675     int n = x.size();
00676 
00677     int* mu = r.alloc<int>(n);
00678     int* nu = r.alloc<int>(n);
00679 
00680     for (int i = n; i--; )
00681       nu[i] = mu[i] = i;
00682 
00683     // Create sorting permutation mu according to the variables upper bounds
00684     MaxInc<IntView> max_inc(x);
00685     Support::quicksort<int, MaxInc<IntView> >(mu, n, max_inc);
00686 
00687     // Create sorting permutation nu according to the variables lower bounds
00688     MinInc<IntView> min_inc(x);
00689     Support::quicksort<int, MinInc<IntView> >(nu, n, min_inc);
00690 
00691     // Sort the cardinality bounds by index
00692     MinIdx<Card> min_idx;
00693     Support::quicksort<Card, MinIdx<Card> >(&k[0], k.size(), min_idx);
00694 
00695     if (!lps) {
00696       assert(!ups);
00697       lps.init(home, k, false);
00698       ups.init(home, k, true);
00699     } else if (Card::propagate) {
00700       // if there has been a change to the cardinality variables
00701       // reconstruction of the partial sum structure is necessary
00702       if (lps.check_update_min(k))
00703         lps.init(home, k, false);
00704       if (ups.check_update_max(k))
00705         ups.init(home, k, true);
00706     }
00707 
00708     // assert that the minimal value of the partial sum structure for
00709     // LBC is consistent with the smallest value a variable can take
00710     assert(lps.minValue() <= x[nu[0]].min());
00711     // assert that the maximal value of the partial sum structure for
00712     // UBC is consistent with the largest value a variable can take
00713 
00714     /*
00715      *  Setup rank and bounds info
00716      *  Since this implementation is based on the theory of Hall Intervals
00717      *  additional datastructures are needed in order to represent these
00718      *  intervals and the "partial-sum" data structure (cf."gcc/bnd-sup.hpp")
00719      *
00720      */
00721 
00722     HallInfo* hall = r.alloc<HallInfo>(2 * n + 2);
00723     Rank* rank = r.alloc<Rank>(n);
00724 
00725     int nb = 0;
00726     // setup bounds and rank
00727     int min        = x[nu[0]].min();
00728     int max        = x[mu[0]].max() + 1;
00729     int last       = lps.firstValue + 1; //equivalent to last = min -2
00730     hall[0].bounds = last;
00731 
00732     /*
00733      * First the algorithm merges the arrays minsorted and maxsorted
00734      * into bounds i.e. hall[].bounds contains the ordered union
00735      * of the lower and upper domain bounds including two sentinels
00736      * at the beginning and at the end of the set
00737      * ( the upper variable bounds in this union are increased by 1 )
00738      */
00739 
00740     {
00741       int i = 0;
00742       int j = 0;
00743       while (true) {
00744         if (i < n && min < max) {
00745           if (min != last) {
00746             last = min;
00747             hall[++nb].bounds = last;
00748           }
00749           rank[nu[i]].min = nb;
00750           if (++i < n)
00751             min = x[nu[i]].min();
00752         } else {
00753           if (max != last) {
00754             last = max;
00755             hall[++nb].bounds = last;
00756           }
00757           rank[mu[j]].max = nb;
00758           if (++j == n)
00759             break;
00760           max = x[mu[j]].max() + 1;
00761         }
00762       }
00763     }
00764 
00765     int rightmost = nb + 1; // rightmost accesible value in bounds
00766     hall[rightmost].bounds = ups.lastValue + 1 ;
00767 
00768     if (Card::propagate) {
00769       skip_lbc = true;
00770       for (int i = k.size(); i--; )
00771         if (k[i].min() != 0) {
00772           skip_lbc = false;
00773           break;
00774         }
00775     }
00776 
00777     if (!card_fixed && !skip_lbc)
00778       GECODE_ES_CHECK((lbc(home, nb, hall, rank, mu, nu)));
00779 
00780     GECODE_ES_CHECK((ubc(home, nb, hall, rank, mu, nu)));
00781 
00782     if (Card::propagate)
00783       GECODE_ES_CHECK(prop_card<Card>(home, x, k));
00784 
00785     for (int i = k.size(); i--; )
00786       count[i] = 0;
00787     for (int i = x.size(); i--; )
00788       if (x[i].assigned()) {
00789         int idx;
00790         if (!lookupValue(k,x[i].val(),idx))
00791           return ES_FAILED;
00792         count[idx]++;
00793       } else {
00794         // We won't need the remaining counts, they're only used when
00795         // all x are assigned
00796         return ES_NOFIX;
00797       }
00798 
00799     for (int i = k.size(); i--; )
00800       GECODE_ME_CHECK(k[i].eq(home, count[i]));
00801 
00802     return home.ES_SUBSUMED(*this);
00803   }
00804 
00805 
00806   template<class Card>
00807   ExecStatus
00808   Bnd<Card>::post(Home home,
00809                   ViewArray<IntView>& x, ViewArray<Card>& k) {
00810     bool cardfix = true;
00811     for (int i=k.size(); i--; )
00812       if (!k[i].assigned()) {
00813         cardfix = false; break;
00814       }
00815     bool nolbc = true;
00816     for (int i=k.size(); i--; )
00817       if (k[i].min() != 0) {
00818         nolbc = false; break;
00819       }
00820 
00821     GECODE_ES_CHECK(postSideConstraints<Card>(home, x, k));
00822 
00823     if (isDistinct<Card>(x,k))
00824       return Distinct::Bnd<IntView>::post(home,x);
00825 
00826     (void) new (home) Bnd<Card>(home,x,k,cardfix,nolbc);
00827     return ES_OK;
00828   }
00829 
00830 }}}
00831 
00832 // STATISTICS: int-prop