Generated on Tue Apr 18 10:22:11 2017 for Gecode by doxygen 1.6.3

sorted.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  *     Gabor Szokoli <szokoli@gecode.org>
00007  *
00008  *  Copyright:
00009  *     Guido Tack, 2004
00010  *     Christian Schulte, 2004
00011  *     Gabor Szokoli, 2004
00012  *
00013  *  Last modified:
00014  *     $Date: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
00015  *     $Revision: 15623 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 
00043 
00044 #include <gecode/set.hh>
00045 #include <gecode/int.hh>
00046 #include <gecode/set/rel.hh>
00047 
00048 namespace Gecode { namespace Set { namespace Channel {
00049 
00050   template<class View>
00051   forceinline
00052   ChannelSorted<View>::ChannelSorted(Home home, View y0,
00053                                      ViewArray< Gecode::Int::IntView >& ys)
00054     : Propagator(home), x0(y0), xs(ys) {
00055     x0.subscribe(home,*this, PC_SET_ANY);
00056     xs.subscribe(home,*this, Gecode::Int::PC_INT_BND);
00057   }
00058 
00059   template<class View>
00060   forceinline
00061   ChannelSorted<View>::ChannelSorted(Space& home, bool share, ChannelSorted& p)
00062     : Propagator(home,share,p) {
00063     x0.update(home,share,p.x0);
00064     xs.update(home,share,p.xs);
00065   }
00066 
00067   template<class View>
00068   forceinline ExecStatus
00069   ChannelSorted<View>::post(Home home, View x0,
00070                             ViewArray<Gecode::Int::IntView>& xs) {
00071     unsigned int xs_size = static_cast<unsigned int>(xs.size());
00072     GECODE_ME_CHECK(x0.cardMin(home,xs_size));
00073     GECODE_ME_CHECK(x0.cardMax(home,xs_size));
00074     if (xs_size == 1) {
00075       SingletonView sv(xs[0]);
00076       GECODE_ES_CHECK((Rel::Eq<View,
00077                        SingletonView>::post(home,x0, sv)));
00078     } else {
00079       // Sharing in xs is handled correctly in the propagator:
00080       // if two views in xs are shared, this leads to failure.
00081       (void) new (home) ChannelSorted(home,x0,xs);
00082     }
00083     return ES_OK;
00084   }
00085 
00086   template<class View>
00087   PropCost
00088   ChannelSorted<View>::cost(const Space&, const ModEventDelta&) const {
00089     return PropCost::linear(PropCost::LO, xs.size()+1);
00090   }
00091 
00092   template<class View>
00093   void
00094   ChannelSorted<View>::reschedule(Space& home) {
00095     x0.reschedule(home,*this, PC_SET_ANY);
00096     xs.reschedule(home,*this, Gecode::Int::PC_INT_BND);
00097   }
00098 
00099   template<class View>
00100   forceinline size_t
00101   ChannelSorted<View>::dispose(Space& home) {
00102     x0.cancel(home,*this, PC_SET_ANY);
00103     xs.cancel(home,*this, Gecode::Int::PC_INT_BND);
00104     (void) Propagator::dispose(home);
00105     return sizeof(*this);
00106   }
00107 
00108   template<class View>
00109   Actor*
00110   ChannelSorted<View>::copy(Space& home, bool share) {
00111     return new (home) ChannelSorted(home,share,*this);
00112   }
00113 
00114   template<class View>
00115   ExecStatus
00116   ChannelSorted<View>::propagate(Space& home, const ModEventDelta&) {
00117 
00118     int xs_size = xs.size();
00119 
00120     bool loopFlag;
00121 
00122     do {
00123       loopFlag = false;
00124 
00125       // Order int vars in xs
00126       GECODE_ME_CHECK(xs[0].gq(home,x0.lubMin()));
00127       for (int i=xs_size-1; i--; ) {
00128         GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i+1].gq(home,xs[i].min() + 1));
00129       }
00130 
00131       GECODE_ME_CHECK_MODIFIED(loopFlag, xs[xs_size-1].lq(home,x0.lubMax()));
00132       for (int i=xs_size-2; i--; ) {
00133         GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].lq(home,xs[i+1].max() - 1));
00134       }
00135 
00136       // if y from xs is assigned, add to glb(x0)
00137       for (int i=xs_size; i--; ) {
00138         if (xs[i].assigned()) {
00139           GECODE_ME_CHECK_MODIFIED(loopFlag, x0.include(home,xs[i].val()));
00140         }
00141       }
00142 
00143       // intersect every y in xs with lub(x0)
00144       for (int i=xs_size; i--; ) {
00145         LubRanges<View> ub(x0);
00146         GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].inter_r(home,ub,false));
00147       }
00148 
00149       // remove gaps between vars in xs from lub(x0)
00150       GECODE_ME_CHECK_MODIFIED(loopFlag,
00151                         x0.exclude(home,Limits::min,xs[0].min()-1));
00152       GECODE_ME_CHECK_MODIFIED(loopFlag,
00153                         x0.exclude(home,xs[xs_size-1].max()+1,
00154                                    Limits::max));
00155 
00156       for (int i=xs_size-1; i--; ) {
00157         int start = xs[i].max() + 1;
00158         int end   = xs[i+1].min() - 1;
00159         if (start<=end) {
00160           GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,start,end));
00161         }
00162       }
00163 
00164       // try to assign vars in xs from glb(x0)
00165       if (x0.glbSize()>0) {
00166 
00167         LubRanges<View> ub(x0);
00168         Iter::Ranges::ToValues<LubRanges<View> > ubv(ub);
00169         GlbRanges<View> lb(x0);
00170         Iter::Ranges::ToValues<GlbRanges<View> > lbv(lb);
00171 
00172         int i=0;
00173         for (; ubv() && lbv() && ubv.val()==lbv.val();
00174             ++ubv, ++lbv, i++) {
00175           GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,lbv.val()));
00176         }
00177 
00178         if (i<xs_size-1 && x0.lubMax()==x0.glbMax()) {
00179           LubRanges<View> lbx0(x0);
00180           GlbRanges<View> ubx0(x0);
00181           Iter::Ranges::Inter<LubRanges<View>,GlbRanges<View> >
00182             inter(lbx0, ubx0);
00183 
00184           int to = x0.glbMax();
00185           int from = to;
00186           while (inter()) {
00187             from = inter.min();
00188             ++inter;
00189           }
00190 
00191           int k=xs_size-1;
00192           for (int j=to; j>=from;j--,k--) {
00193             GECODE_ME_CHECK_MODIFIED(loopFlag, xs[k].eq(home,j));
00194           }
00195         }
00196       }
00197 
00198     } while (loopFlag);
00199 
00200     for (int i=xs_size; i--; )
00201       if (!xs[i].assigned())
00202         return ES_FIX;
00203     return home.ES_SUBSUMED(*this);
00204   }
00205 
00206 }}}
00207 
00208 // STATISTICS: set-prop