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