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