Generated on Tue Apr 18 10:21:33 2017 for Gecode by doxygen 1.6.3

min-max.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  *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2004
00009  *     Vincent Barichard, 2012
00010  *
00011  *  Last modified:
00012  *     $Date: 2017-04-10 13:21:37 +0200 (Mon, 10 Apr 2017) $ by $Author: schulte $
00013  *     $Revision: 15631 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <cmath>
00041 
00042 namespace Gecode { namespace Float { namespace Arithmetic {
00043 
00044   /*
00045    * Bounds consistent min propagator
00046    *
00047    */
00048   template<class A, class B, class C>
00049   forceinline
00050   Min<A,B,C>::Min(Home home, A x0, B x1, C x2)
00051     : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,x0,x1,x2) {}
00052 
00053   template<class A, class B, class C>
00054   forceinline
00055   Min<A,B,C>::Min(Space& home, bool share, Min<A,B,C>& p)
00056     : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,share,p) {}
00057 
00058   template<class A, class B, class C>
00059   forceinline
00060   Min<A,B,C>::Min(Space& home, bool share, Propagator& p,
00061                   A x0, B x1, C x2)
00062     : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,share,p,x0,x1,x2) {}
00063 
00064   template<class A, class B, class C>
00065   Actor*
00066   Min<A,B,C>::copy(Space& home, bool share) {
00067     return new (home) Min<A,B,C>(home,share,*this);
00068   }
00069 
00070   template<class A, class B, class C>
00071   ExecStatus
00072   Min<A,B,C>::post(Home home, A x0, B x1, C x2) {
00073     (void) new (home) Min<A,B,C>(home,x0,x1,x2);
00074     return ES_OK;
00075   }
00076 
00077   template<class A, class B, class C>
00078   ExecStatus
00079   Min<A,B,C>::propagate(Space& home, const ModEventDelta&) {
00080     GECODE_ME_CHECK(x2.eq(home,min(x0.domain(),x1.domain())));
00081     GECODE_ME_CHECK(x0.gq(home,x2.min()));
00082     GECODE_ME_CHECK(x1.gq(home,x2.min()));
00083     if (same(x0,x1)) {
00084       GECODE_ME_CHECK(x0.lq(home,x2.max()));
00085     } else {
00086       if (!overlap(x1.val(),x2.val())) GECODE_ME_CHECK(x0.lq(home,x2.max()));
00087       if (!overlap(x0.val(),x2.val())) GECODE_ME_CHECK(x1.lq(home,x2.max()));
00088     }
00089     return (x0.assigned() && x1.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX;
00090   }
00091 
00092   /*
00093    * Bounds consistent max propagator
00094    *
00095    */
00096 
00097   template<class A, class B, class C>
00098   forceinline
00099   Max<A,B,C>::Max(Home home, A x0, B x1, C x2)
00100     : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,x0,x1,x2) {}
00101 
00102   template<class A, class B, class C>
00103   forceinline
00104   Max<A,B,C>::Max(Space& home, bool share, Max<A,B,C>& p)
00105     : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,share,p) {}
00106 
00107   template<class A, class B, class C>
00108   forceinline
00109   Max<A,B,C>::Max(Space& home, bool share, Propagator& p,
00110                   A x0, B x1, C x2)
00111     : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,share,p,x0,x1,x2) {}
00112 
00113   template<class A, class B, class C>
00114   Actor*
00115   Max<A,B,C>::copy(Space& home, bool share) {
00116     return new (home) Max<A,B,C>(home,share,*this);
00117   }
00118 
00119   template<class A, class B, class C>
00120   ExecStatus
00121   Max<A,B,C>::post(Home home, A x0, B x1, C x2) {
00122     (void) new (home) Max<A,B,C>(home,x0,x1,x2);
00123     return ES_OK;
00124   }
00125 
00126   template<class A, class B, class C>
00127   ExecStatus
00128   Max<A,B,C>::propagate(Space& home, const ModEventDelta&) {
00129     GECODE_ME_CHECK(x2.eq(home,max(x0.domain(),x1.domain())));
00130     GECODE_ME_CHECK(x0.lq(home,x2.max()));
00131     GECODE_ME_CHECK(x1.lq(home,x2.max()));
00132     if (same(x0,x1)) {
00133       GECODE_ME_CHECK(x0.gq(home,x2.min()));
00134     } else {
00135       if (!overlap(x1.val(),x2.val())) GECODE_ME_CHECK(x0.gq(home,x2.min()));
00136       if (!overlap(x0.val(),x2.val())) GECODE_ME_CHECK(x1.gq(home,x2.min()));
00137     }
00138     return (x0.assigned() && x1.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX;
00139   }
00140 
00141   /*
00142    * Nary bounds consistent maximum
00143    *
00144    */
00145 
00146   template<class View>
00147   forceinline
00148   NaryMax<View>::NaryMax(Home home, ViewArray<View>& x, View y)
00149     : NaryOnePropagator<View,PC_FLOAT_BND>(home,x,y) {}
00150 
00151   template<class View>
00152   ExecStatus
00153   NaryMax<View>::post(Home home, ViewArray<View>& x, View y) {
00154     assert(x.size() > 0);
00155     x.unique(home);
00156     if (x.size() == 1)
00157       return Rel::Eq<View,View>::post(home,x[0],y);
00158     if (x.size() == 2)
00159       return Max<View,View,View>::post(home,x[0],x[1],y);
00160     FloatNum l = Float::Limits::min;
00161     FloatNum u = Float::Limits::min;
00162     for (int i=x.size(); i--; ) {
00163       l = std::max(l,x[i].min());
00164       u = std::max(u,x[i].max());
00165     }
00166     GECODE_ME_CHECK(y.gq(home,l));
00167     GECODE_ME_CHECK(y.lq(home,u));
00168     if (x.same(home,y)) {
00169       // Check whether y occurs in x
00170       for (int i=x.size(); i--; )
00171         GECODE_ES_CHECK((Rel::Lq<View>::post(home,x[i],y)));
00172     } else {
00173       (void) new (home) NaryMax<View>(home,x,y);
00174     }
00175     return ES_OK;
00176   }
00177 
00178   template<class View>
00179   forceinline
00180   NaryMax<View>::NaryMax(Space& home, bool share, NaryMax<View>& p)
00181     : NaryOnePropagator<View,PC_FLOAT_BND>(home,share,p) {}
00182 
00183   template<class View>
00184   Actor*
00185   NaryMax<View>::copy(Space& home, bool share) {
00186     if (x.size() == 1)
00187       return new (home) Rel::Eq<View,View>(home,share,*this,x[0],y);
00188     if (x.size() == 2)
00189       return new (home) Max<View,View,View>(home,share,*this,x[0],x[1],y);
00190     return new (home) NaryMax<View>(home,share,*this);
00191   }
00192 
00194   enum MaxPropStatus {
00195     MPS_ASSIGNED  = 1<<0, 
00196     MPS_REMOVED   = 1<<1, 
00197     MPS_NEW_BOUND = 1<<2  
00198   };
00199 
00200   template<class View>
00201   forceinline ExecStatus
00202   prop_nary_max(Space& home, Propagator& p,
00203                 ViewArray<View>& x, View y, PropCond pc) {
00204   rerun:
00205     assert(x.size() > 0);
00206     FloatNum maxmax = x[x.size()-1].max();
00207     FloatNum maxmin = x[x.size()-1].min();
00208     for (int i = x.size()-1; i--; ) {
00209       maxmax = std::max(x[i].max(),maxmax);
00210       maxmin = std::max(x[i].min(),maxmin);
00211     }
00212     GECODE_ME_CHECK(y.lq(home,maxmax));
00213     GECODE_ME_CHECK(y.gq(home,maxmin));
00214     maxmin = y.min();
00215     maxmax = y.max();
00216     int status = MPS_ASSIGNED;
00217     for (int i = x.size(); i--; ) {
00218       ModEvent me = x[i].lq(home,maxmax);
00219       if (me == ME_FLOAT_FAILED)
00220         return ES_FAILED;
00221       if (me_modified(me) && (x[i].max() != maxmax))
00222         status |= MPS_NEW_BOUND;
00223       if (x[i].max() < maxmin) {
00224         x.move_lst(i,home,p,pc);
00225         status |= MPS_REMOVED;
00226       } else if (!x[i].assigned())
00227         status &= ~MPS_ASSIGNED;
00228     }
00229     if (x.size() == 0)
00230       return ES_FAILED;
00231     if ((status & MPS_REMOVED) != 0)
00232       goto rerun;
00233     if (((status & MPS_ASSIGNED) != 0) && y.assigned())
00234       return home.ES_SUBSUMED(p);
00235     return ((status & MPS_NEW_BOUND) != 0) ? ES_NOFIX : ES_FIX;
00236   }
00237 
00238   template<class View>
00239   ExecStatus
00240   NaryMax<View>::propagate(Space& home, const ModEventDelta&) {
00241     return prop_nary_max(home,*this,x,y,PC_FLOAT_BND);
00242   }
00243 
00244 }}}
00245 
00246 // STATISTICS: float-prop
00247