Generated on Thu Mar 22 10:39:35 2012 for Gecode by doxygen 1.6.3

int-eq.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2011
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-18 09:41:50 +0200 (Thu, 18 Aug 2011) $ by $Author: tack $
00011  *     $Revision: 12311 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include <gecode/int/rel.hh>
00039 #include <gecode/int/distinct.hh>
00040 
00041 namespace Gecode { namespace Int { namespace NValues {
00042 
00043   template<class VY>
00044   forceinline
00045   EqInt<VY>::EqInt(Home home, ValSet& vs, ViewArray<IntView>& x, VY y)
00046     : IntBase<VY>(home,vs,x,y) {
00047     home.notice(*this, AP_WEAKLY);
00048   }
00049 
00050   template<class VY>
00051   inline ExecStatus
00052   EqInt<VY>::post(Home home, ViewArray<IntView>& x, VY y) {
00053     if (x.size() == 0) {
00054       GECODE_ME_CHECK(y.eq(home,0));
00055       return ES_OK;
00056     }
00057 
00058     x.unique(home);
00059 
00060     if (x.size() == 1) {
00061       GECODE_ME_CHECK(y.eq(home,1));
00062       return ES_OK;
00063     }
00064 
00065     GECODE_ME_CHECK(y.gq(home,1));
00066     GECODE_ME_CHECK(y.lq(home,x.size()));
00067 
00068     if (y.max() == 1) {
00069       assert(y.assigned());
00070       return Rel::NaryEqDom<IntView>::post(home,x);
00071     }
00072 
00073     if (y.min() == x.size()) {
00074       assert(y.assigned());
00075       return Distinct::Dom<IntView>::post(home,x);
00076     }
00077     
00078     // Eliminate assigned views and store them into the value set
00079     ValSet vs;
00080     int n = x.size();
00081     for (int i=n; i--; )
00082       if (x[i].assigned()) {
00083         vs.add(home, x[i].val());
00084         x[i] = x[--n];
00085       }
00086 
00087     GECODE_ME_CHECK(y.gq(home,vs.size()));
00088     GECODE_ME_CHECK(y.lq(home,n + vs.size()));
00089 
00090     if (n == 0) {
00091       assert(y.val() == vs.size());
00092       return ES_OK;
00093     }
00094     x.size(n);
00095     (void) new (home) EqInt<VY>(home, vs, x, y);
00096     return ES_OK;
00097   }
00098     
00099   template<class VY>
00100   forceinline
00101   EqInt<VY>::EqInt(Space& home, bool share, EqInt<VY>& p)
00102     : IntBase<VY>(home, share, p) {}
00103 
00104   template<class VY>
00105   Propagator*
00106   EqInt<VY>::copy(Space& home, bool share) {
00107     return new (home) EqInt<VY>(home, share, *this);
00108   }
00109 
00110   template<class VY>
00111   forceinline size_t
00112   EqInt<VY>::dispose(Space& home) {
00113     home.ignore(*this, AP_WEAKLY);
00114     (void) IntBase<VY>::dispose(home);
00115     return sizeof(*this);
00116   }
00117 
00118   template<class VY>
00119   ExecStatus
00120   EqInt<VY>::propagate(Space& home, const ModEventDelta& med) {
00121     // Add assigned views to value set
00122     if (IntView::me(med) == ME_INT_VAL)
00123       add(home);
00124 
00125     GECODE_ME_CHECK(y.gq(home, vs.size()));
00126     GECODE_ME_CHECK(y.lq(home, x.size() + vs.size()));
00127 
00128     if (x.size() == 0) 
00129       return home.ES_SUBSUMED(*this);
00130 
00131     // All values must be in the value set
00132     if (y.max() == vs.size())
00133       return all_in_valset(home);
00134 
00135     // Compute positions of disjoint views and eliminate subsumed views
00136     Region r(home);
00137     int* dis; int n_dis;
00138     disjoint(home,r,dis,n_dis);
00139 
00140     // The number might have changed due to elimination of subsumed views
00141     GECODE_ME_CHECK(y.lq(home, x.size() + vs.size()));
00142 
00143     if (x.size() == 0) {
00144       assert(y.val() == vs.size());
00145       return home.ES_SUBSUMED(*this);
00146     }
00147 
00148     GECODE_ES_CHECK(prune_upper(home,g));
00149 
00150     // No lower bound pruning possible
00151     if (n_dis == 0)
00152       return ES_NOFIX;
00153 
00154     // Only if the propagator is at fixpoint here, continue with
00155     // propagating the lower bound
00156     if (IntView::me(Propagator::modeventdelta()) != ME_INT_NONE)
00157       return ES_NOFIX;
00158 
00159     // Do lower bound-based pruning
00160     GECODE_ES_CHECK(prune_lower(home,dis,n_dis));
00161 
00162     return ES_NOFIX;
00163   }
00164   
00165 }}}
00166 
00167 // STATISTICS: int-prop