Generated on Mon Aug 25 11:35:43 2008 for Gecode by doxygen 1.5.6

minmax.icc

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: 2008-02-20 10:27:10 +0100 (Wed, 20 Feb 2008) $ by $Author: tack $
00015  *     $Revision: 6241 $
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 
00047 namespace Gecode { namespace Set { namespace Int {
00048 
00049   template <class View>
00050   forceinline
00051   MinElement<View>::MinElement(Space* home, View y0, Gecode::Int::IntView y1)
00052     : IntSetPropagator<View,PC_SET_ANY,
00053                        Gecode::Int::PC_INT_BND> (home, y0, y1) {}
00054 
00055   template <class View>
00056   forceinline ExecStatus
00057   MinElement<View>::post(Space* home, View x0, Gecode::Int::IntView x1) {
00058     GECODE_ME_CHECK(x0.cardMin(home,1));
00059     (void) new (home) MinElement(home,x0,x1);
00060     return ES_OK;
00061   }
00062 
00063   template <class View>
00064   forceinline
00065   MinElement<View>::MinElement(Space* home, bool share, MinElement& p)
00066     : IntSetPropagator<View,PC_SET_ANY,
00067                        Gecode::Int::PC_INT_BND> (home, share, p) {}
00068 
00069   template <class View>
00070   Actor*
00071   MinElement<View>::copy(Space* home, bool share) {
00072    return new (home) MinElement(home,share,*this);
00073   }
00074 
00075   template <class View>
00076   Support::Symbol
00077   MinElement<View>::ati(void) {
00078    return Reflection::mangle<View>("Gecode::Set::Int::MinElement");
00079   }
00080 
00081   template <class View>
00082   Reflection::ActorSpec
00083   MinElement<View>::spec(const Space* home, Reflection::VarMap& m) const {
00084    return IntSetPropagator<View,PC_SET_ANY,Gecode::Int::PC_INT_BND>
00085      ::spec(home, m, ati());
00086   }
00087 
00088   template <class View>
00089   void
00090   MinElement<View>::post(Space* home, Reflection::VarMap& vars,
00091                          const Reflection::ActorSpec& spec) {
00092     spec.checkArity(2);
00093     View x0(home, vars, spec[0]);
00094     Gecode::Int::IntView x1(home, vars, spec[1]);
00095     (void) post(home,x0,x1);
00096   }
00097 
00098   template <class View>
00099   ExecStatus
00100   MinElement<View>::propagate(Space* home, ModEventDelta) {
00101     //x1 is an element of x0.ub
00102     //x1 =< smallest element of x0.lb
00103     //x1 =< x0.cardinialityMin-est largest element of x0.ub
00104     //(these 2 take care of determined x0)
00105     //No element in x0 is smaller than x1
00106     //if x1 is determined, it is part of the ub.
00107 
00108     //Consequently:
00109     //The domain of x1 is a subset of x0.ub up to the first element in x0.lb.
00110     //x0 lacks everything smaller than smallest possible x1.
00111 
00112     {
00113       LubRanges<View> ub(x0);
00114       GECODE_ME_CHECK(x1.inter_r(home,ub,false));
00115     }
00116     GECODE_ME_CHECK(x1.lq(home,x0.glbMin()));
00117     //if cardMin>lbSize?
00118     assert(x0.cardMin()>=1);
00119 
00120     {
00122       int size = 0;
00123       int maxN = BndSet::MAX_OF_EMPTY;
00124       for (LubRanges<View> ubr(x0); ubr(); ++ubr, ++size) {}
00125       GECODE_AUTOARRAY(int, ub, size*2);
00126       int i=0;
00127       for (LubRanges<View> ubr(x0); ubr(); ++ubr, ++i) {
00128         ub[2*i]   = ubr.min();
00129         ub[2*i+1] = ubr.max();
00130       }
00131       int x0cm = x0.cardMin()-1;
00132       for (int i=size; i--;) {
00133         int width = ub[2*i+1]-ub[2*i]+1;
00134         if (width > x0cm) {
00135           maxN = ub[2*i+1]-x0cm;
00136           break;
00137         }
00138         x0cm -= width;
00139       }
00140       GECODE_ME_CHECK(x1.lq(home,maxN));
00141     }
00142 
00143     GECODE_ME_CHECK( x0.exclude(home,
00144                                 Limits::min, x1.min()-1) );
00145 
00146     if (x1.assigned()) {
00147       GECODE_ME_CHECK(x0.include(home,x1.val()));
00148       GECODE_ME_CHECK(x0.exclude(home,
00149                                  Limits::min, x1.val()-1));
00150       return ES_SUBSUMED(this,home);
00151     }
00152 
00153     return ES_FIX;
00154   }
00155 
00156   template <class View>
00157   forceinline
00158   MaxElement<View>::MaxElement(Space* home, View y0, Gecode::Int::IntView y1)
00159     : IntSetPropagator<View,PC_SET_ANY,
00160                        Gecode::Int::PC_INT_BND> (home, y0, y1) {}
00161 
00162   template <class View>
00163   forceinline
00164   MaxElement<View>::MaxElement(Space* home, bool share, MaxElement& p)
00165     : IntSetPropagator<View,PC_SET_ANY,
00166                        Gecode::Int::PC_INT_BND> (home, share, p) {}
00167 
00168   template <class View>
00169   ExecStatus
00170   MaxElement<View>::post(Space* home, View x0,
00171                               Gecode::Int::IntView x1) {
00172     GECODE_ME_CHECK(x0.cardMin(home,1));
00173     (void) new (home) MaxElement(home,x0,x1);
00174     return ES_OK;
00175   }
00176 
00177   template <class View>
00178   Actor*
00179   MaxElement<View>::copy(Space* home, bool share) {
00180     return new (home) MaxElement(home,share,*this);
00181   }
00182 
00183   template <class View>
00184   Support::Symbol
00185   MaxElement<View>::ati(void) {
00186     return Reflection::mangle<View>("Gecode::Set::Int::MaxElement");
00187   }
00188 
00189   template <class View>
00190   Reflection::ActorSpec
00191   MaxElement<View>::spec(const Space* home, Reflection::VarMap& m) const {
00192     return IntSetPropagator<View,PC_SET_ANY,Gecode::Int::PC_INT_BND>
00193       ::spec(home, m, ati());
00194   }
00195 
00196   template <class View>
00197   void
00198   MaxElement<View>::post(Space* home, Reflection::VarMap& vars,
00199                          const Reflection::ActorSpec& spec) {
00200     spec.checkArity(2);
00201     View x0(home, vars, spec[0]);
00202     Gecode::Int::IntView x1(home, vars, spec[1]);
00203     (void) new (home) MaxElement<View>(home,x0,x1);
00204   }
00205 
00206   template <class View>
00207   ExecStatus
00208   MaxElement<View>::propagate(Space* home, ModEventDelta) {
00209     LubRanges<View> ub(x0);
00210     GECODE_ME_CHECK(x1.inter_r(home,ub,false));
00211     GECODE_ME_CHECK(x1.gq(home,x0.glbMax()));
00212     assert(x0.cardMin()>=1);
00213     GECODE_ME_CHECK(x1.gq(home,x0.lubMinN(x0.cardMin()-1)));
00214     GECODE_ME_CHECK(x0.exclude(home,
00215                                x1.max()+1,Limits::max) );
00216 
00217     if (x1.assigned()) {
00218       GECODE_ME_CHECK(x0.include(home,x1.val()));
00219       GECODE_ME_CHECK( x0.exclude(home,
00220                                   x1.val()+1,Limits::max) );
00221       return ES_SUBSUMED(this,home);
00222     }
00223 
00224     return ES_FIX;
00225   }  
00226 }}}
00227 
00228 // STATISTICS: set-prop