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

int-lq.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   LqInt<VY>::LqInt(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   LqInt<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     GECODE_ME_CHECK(y.gq(home,1));
00061 
00062     if (x.size() == 1)
00063       return ES_OK;
00064 
00065     if (y.max() == 1) {
00066       assert(y.assigned());
00067       return Rel::NaryEqDom<IntView>::post(home,x);
00068     }
00069 
00070     if (y.min() >= x.size())
00071       return ES_OK;
00072     
00073     // Eliminate assigned views and store them into the value set
00074     ValSet vs;
00075     int n = x.size();
00076     for (int i=n; i--; )
00077       if (x[i].assigned()) {
00078         vs.add(home, x[i].val());
00079         x[i] = x[--n];
00080       }
00081 
00082     GECODE_ME_CHECK(y.gq(home,vs.size()));
00083 
00084     if (n == 0) {
00085       assert(y.min() >= vs.size());
00086       return ES_OK;
00087     }
00088 
00089     x.size(n);
00090 
00091     (void) new (home) LqInt<VY>(home, vs, x, y);
00092     return ES_OK;
00093   }
00094     
00095   template<class VY>
00096   forceinline
00097   LqInt<VY>::LqInt(Space& home, bool share, LqInt<VY>& p)
00098     : IntBase<VY>(home, share, p) {}
00099 
00100   template<class VY>
00101   Propagator*
00102   LqInt<VY>::copy(Space& home, bool share) {
00103     return new (home) LqInt<VY>(home, share, *this);
00104   }
00105 
00106   template<class VY>
00107   forceinline size_t
00108   LqInt<VY>::dispose(Space& home) {
00109     home.ignore(*this, AP_WEAKLY);
00110     (void) IntBase<VY>::dispose(home);
00111     return sizeof(*this);
00112   }
00113 
00114   template<class VY>
00115   ExecStatus
00116   LqInt<VY>::propagate(Space& home, const ModEventDelta& med) {
00117     // Add assigned views to value set
00118     if (IntView::me(med) == ME_INT_VAL)
00119       add(home);
00120 
00121     GECODE_ME_CHECK(y.gq(home, vs.size()));
00122 
00123     if (x.size() == 0)
00124       return home.ES_SUBSUMED(*this);
00125 
00126     // All values must be in the value set
00127     if (y.max() == vs.size())
00128       return all_in_valset(home);
00129 
00130     if (x.size() + vs.size() <= y.min())
00131       return home.ES_SUBSUMED(*this);
00132       
00133     // Compute positions of disjoint views
00134     Region r(home);
00135     int* dis; int n_dis;
00136     disjoint(home,r,dis,n_dis);
00137 
00138     // Some views might have been eliminated as they are subsumed
00139     if (x.size() == 0)
00140       return home.ES_SUBSUMED(*this);
00141 
00142     // No lower bound pruning possible
00143     if (n_dis == 0)
00144       return ES_NOFIX;
00145 
00146     // Do lower bound-based pruning
00147     GECODE_ES_CHECK(prune_lower(home,dis,n_dis));
00148 
00149     return ES_NOFIX;
00150   }
00151   
00152 }}}
00153 
00154 // STATISTICS: int-prop