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

partition.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *     Christian Schulte <schulte@gecode.org>
00006  *
00007  *  Contributing authors:
00008  *     Gabor Szokoli <szokoli@gecode.org>
00009  *
00010  *  Copyright:
00011  *     Guido Tack, 2004
00012  *     Christian Schulte, 2004
00013  *     Gabor Szokoli, 2004
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 Set { namespace RelOp {
00041 
00042   /*
00043    * "Nary partition" propagator
00044    *
00045    */
00046 
00047   template<class View0, class View1>
00048   forceinline
00049   PartitionN<View0,View1>::PartitionN(Home home, ViewArray<View0>& x, View1 y)
00050     : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home, x, y) {
00051     shared = Gecode::shared(x) || viewarrayshared(x,y);
00052   }
00053 
00054   template<class View0, class View1>
00055   forceinline
00056   PartitionN<View0,View1>::PartitionN(Home home, ViewArray<View0>& x,
00057                                       const IntSet& z, View1 y)
00058     : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home, x, y) {
00059     shared = Gecode::shared(x) || viewarrayshared(x,y);
00060     IntSetRanges rz(z);
00061     unionOfDets.includeI(home, rz);
00062   }
00063 
00064   template<class View0, class View1>
00065   forceinline
00066   PartitionN<View0,View1>::PartitionN(Space& home, PartitionN& p)
00067     : MixNaryOnePropagator<View0,PC_SET_ANY,View1,PC_SET_ANY>(home,p),
00068       shared(p.shared) {
00069     unionOfDets.update(home,p.unionOfDets);
00070   }
00071 
00072   template<class View0, class View1>
00073   Actor*
00074   PartitionN<View0,View1>::copy(Space& home) {
00075     return new (home) PartitionN(home,*this);
00076   }
00077 
00078   template<class View0, class View1>
00079   ExecStatus PartitionN<View0,View1>::post(Home home, ViewArray<View0>& x,
00080                                            View1 y) {
00081     switch (x.size()) {
00082     case 0:
00083       GECODE_ME_CHECK(y.cardMax(home, 0));
00084       return ES_OK;
00085     case 1:
00086       return Rel::Eq<View0,View1>::post(home, x[0], y);
00087     default:
00088       (void) new (home) PartitionN<View0,View1>(home,x,y);
00089       return ES_OK;
00090     }
00091   }
00092 
00093   template<class View0, class View1>
00094   ExecStatus PartitionN<View0,View1>::post(Home home, ViewArray<View0>& x,
00095                                            const IntSet& z, View1 y) {
00096     (void) new (home) PartitionN<View0,View1>(home,x,z,y);
00097     return ES_OK;
00098   }
00099 
00100   template<class View0, class View1>
00101   PropCost PartitionN<View0,View1>::cost(const Space&, const ModEventDelta&) const {
00102     return PropCost::quadratic(PropCost::LO, x.size()+1);
00103   }
00104 
00105   template<class View0, class View1>
00106   ExecStatus
00107   PartitionN<View0,View1>::propagate(Space& home, const ModEventDelta& med) {
00108 
00109     ModEvent me0 = View0::me(med);
00110     ModEvent me1 = View1::me(med);
00111     bool ubevent = Rel::testSetEventUB(me0, me1);
00112     bool lbevent = Rel::testSetEventLB(me0, me1);
00113     bool anybevent = Rel::testSetEventAnyB(me0, me1);
00114     bool cardevent = Rel::testSetEventCard(me0, me1);
00115 
00116     bool modified = false;
00117     bool oldModified = false;
00118 
00119     do {
00120       oldModified = modified;
00121       modified = false;
00122       if (oldModified || anybevent)
00123         GECODE_ES_CHECK(partitionNXiUB(home,modified, x, y,unionOfDets));
00124       if (modified || oldModified || anybevent)
00125         GECODE_ES_CHECK(partitionNXiLB(home,modified, x, y,unionOfDets));
00126       if (modified || oldModified || ubevent)
00127         GECODE_ES_CHECK(partitionNYUB(home,modified, x, y,unionOfDets));
00128       if (modified || oldModified || lbevent)
00129         GECODE_ES_CHECK(partitionNYLB(home,modified, x, y,unionOfDets));
00130       if (modified || oldModified || ubevent)
00131         GECODE_ES_CHECK(unionNXiUB(home,modified, x, y,unionOfDets));
00132       if (modified || oldModified || cardevent)
00133         GECODE_ES_CHECK(partitionNCard(home,modified, x, y,unionOfDets));
00134     } while (modified);
00135 
00136     //removing assigned sets from x, accumulating the value:
00137     for(int i=0;i<x.size();i++) {
00138       //Do not reverse! Eats away the end of the array!
00139       while (i<x.size() && x[i].assigned()) {
00140         GlbRanges<View0> det(x[i]);
00141         unionOfDets.includeI(home,det);
00142         x.move_lst(i);
00143       }
00144     }
00145     // When we run out of variables, make a final check and disolve:
00146     if (x.size()==0) {
00147       BndSetRanges all1(unionOfDets);
00148       GECODE_ME_CHECK( y.intersectI(home,all1) );
00149       BndSetRanges all2(unionOfDets);
00150       GECODE_ME_CHECK( y.includeI(home,all2) );
00151       unionOfDets.dispose(home);
00152       return home.ES_SUBSUMED(*this);
00153     }
00154 
00155     return shared ? ES_NOFIX : ES_FIX;
00156   }
00157 
00158 }}}
00159 
00160 // STATISTICS: set-prop