this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * Vincent Barichard <Vincent.Barichard@univ-angers.fr> 6 * 7 * Copyright: 8 * Christian Schulte, 2004 9 * Vincent Barichard, 2012 10 * 11 * This file is part of Gecode, the generic constraint 12 * development environment: 13 * http://www.gecode.org 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining 16 * a copy of this software and associated documentation files (the 17 * "Software"), to deal in the Software without restriction, including 18 * without limitation the rights to use, copy, modify, merge, publish, 19 * distribute, sublicense, and/or sell copies of the Software, and to 20 * permit persons to whom the Software is furnished to do so, subject to 21 * the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be 24 * included in all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 * 34 */ 35 36#include <cmath> 37 38namespace Gecode { namespace Float { namespace Arithmetic { 39 40 /* 41 * Bounds consistent min propagator 42 * 43 */ 44 template<class A, class B, class C> 45 forceinline 46 Min<A,B,C>::Min(Home home, A x0, B x1, C x2) 47 : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,x0,x1,x2) {} 48 49 template<class A, class B, class C> 50 forceinline 51 Min<A,B,C>::Min(Space& home, Min<A,B,C>& p) 52 : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,p) {} 53 54 template<class A, class B, class C> 55 forceinline 56 Min<A,B,C>::Min(Space& home, Propagator& p, 57 A x0, B x1, C x2) 58 : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,p,x0,x1,x2) {} 59 60 template<class A, class B, class C> 61 Actor* 62 Min<A,B,C>::copy(Space& home) { 63 return new (home) Min<A,B,C>(home,*this); 64 } 65 66 template<class A, class B, class C> 67 ExecStatus 68 Min<A,B,C>::post(Home home, A x0, B x1, C x2) { 69 GECODE_ME_CHECK(x2.eq(home,min(x0.domain(),x1.domain()))); 70 GECODE_ME_CHECK(x0.gq(home,x2.min())); 71 GECODE_ME_CHECK(x1.gq(home,x2.min())); 72 (void) new (home) Min<A,B,C>(home,x0,x1,x2); 73 return ES_OK; 74 } 75 76 template<class A, class B, class C> 77 ExecStatus 78 Min<A,B,C>::propagate(Space& home, const ModEventDelta&) { 79 GECODE_ME_CHECK(x2.eq(home,min(x0.domain(),x1.domain()))); 80 GECODE_ME_CHECK(x0.gq(home,x2.min())); 81 GECODE_ME_CHECK(x1.gq(home,x2.min())); 82 if (x0 == x1) { 83 GECODE_ME_CHECK(x0.lq(home,x2.max())); 84 } else { 85 if (!overlap(x1.val(),x2.val())) GECODE_ME_CHECK(x0.lq(home,x2.max())); 86 if (!overlap(x0.val(),x2.val())) GECODE_ME_CHECK(x1.lq(home,x2.max())); 87 } 88 return (x0.assigned() && x1.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX; 89 } 90 91 /* 92 * Bounds consistent max propagator 93 * 94 */ 95 96 template<class A, class B, class C> 97 forceinline 98 Max<A,B,C>::Max(Home home, A x0, B x1, C x2) 99 : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,x0,x1,x2) {} 100 101 template<class A, class B, class C> 102 forceinline 103 Max<A,B,C>::Max(Space& home, Max<A,B,C>& p) 104 : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,p) {} 105 106 template<class A, class B, class C> 107 forceinline 108 Max<A,B,C>::Max(Space& home, Propagator& p, 109 A x0, B x1, C x2) 110 : MixTernaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND,C,PC_FLOAT_BND>(home,p,x0,x1,x2) {} 111 112 template<class A, class B, class C> 113 Actor* 114 Max<A,B,C>::copy(Space& home) { 115 return new (home) Max<A,B,C>(home,*this); 116 } 117 118 template<class A, class B, class C> 119 ExecStatus 120 Max<A,B,C>::post(Home home, A x0, B x1, C x2) { 121 GECODE_ME_CHECK(x2.eq(home,max(x0.domain(),x1.domain()))); 122 GECODE_ME_CHECK(x0.lq(home,x2.max())); 123 GECODE_ME_CHECK(x1.lq(home,x2.max())); 124 (void) new (home) Max<A,B,C>(home,x0,x1,x2); 125 return ES_OK; 126 } 127 128 template<class A, class B, class C> 129 ExecStatus 130 Max<A,B,C>::propagate(Space& home, const ModEventDelta&) { 131 GECODE_ME_CHECK(x2.eq(home,max(x0.domain(),x1.domain()))); 132 GECODE_ME_CHECK(x0.lq(home,x2.max())); 133 GECODE_ME_CHECK(x1.lq(home,x2.max())); 134 if (x0 == x1) { 135 GECODE_ME_CHECK(x0.gq(home,x2.min())); 136 } else { 137 if (!overlap(x1.val(),x2.val())) GECODE_ME_CHECK(x0.gq(home,x2.min())); 138 if (!overlap(x0.val(),x2.val())) GECODE_ME_CHECK(x1.gq(home,x2.min())); 139 } 140 return (x0.assigned() && x1.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX; 141 } 142 143 /* 144 * Nary bounds consistent maximum 145 * 146 */ 147 148 template<class View> 149 forceinline 150 NaryMax<View>::NaryMax(Home home, ViewArray<View>& x, View y) 151 : NaryOnePropagator<View,PC_FLOAT_BND>(home,x,y) {} 152 153 template<class View> 154 ExecStatus 155 NaryMax<View>::post(Home home, ViewArray<View>& x, View y) { 156 assert(x.size() > 0); 157 x.unique(); 158 if (x.size() == 1) 159 return Rel::Eq<View,View>::post(home,x[0],y); 160 if (x.size() == 2) 161 return Max<View,View,View>::post(home,x[0],x[1],y); 162 FloatNum l = Float::Limits::min; 163 FloatNum u = Float::Limits::min; 164 for (int i=x.size(); i--; ) { 165 l = std::max(l,x[i].min()); 166 u = std::max(u,x[i].max()); 167 } 168 GECODE_ME_CHECK(y.gq(home,l)); 169 GECODE_ME_CHECK(y.lq(home,u)); 170 if (x.same(y)) { 171 // Check whether y occurs in x 172 for (int i=x.size(); i--; ) 173 GECODE_ES_CHECK((Rel::Lq<View>::post(home,x[i],y))); 174 } else { 175 (void) new (home) NaryMax<View>(home,x,y); 176 } 177 return ES_OK; 178 } 179 180 template<class View> 181 forceinline 182 NaryMax<View>::NaryMax(Space& home, NaryMax<View>& p) 183 : NaryOnePropagator<View,PC_FLOAT_BND>(home,p) {} 184 185 template<class View> 186 Actor* 187 NaryMax<View>::copy(Space& home) { 188 if (x.size() == 1) 189 return new (home) Rel::Eq<View,View>(home,*this,x[0],y); 190 if (x.size() == 2) 191 return new (home) Max<View,View,View>(home,*this,x[0],x[1],y); 192 return new (home) NaryMax<View>(home,*this); 193 } 194 195 /// Status of propagation for nary max 196 enum MaxPropStatus { 197 MPS_ASSIGNED = 1<<0, ///< All views are assigned 198 MPS_REMOVED = 1<<1, ///< A view is removed 199 MPS_NEW_BOUND = 1<<2 ///< Telling has found a new upper bound 200 }; 201 202 template<class View> 203 forceinline ExecStatus 204 prop_nary_max(Space& home, Propagator& p, 205 ViewArray<View>& x, View y, PropCond pc) { 206 rerun: 207 assert(x.size() > 0); 208 FloatNum maxmax = x[x.size()-1].max(); 209 FloatNum maxmin = x[x.size()-1].min(); 210 for (int i = x.size()-1; i--; ) { 211 maxmax = std::max(x[i].max(),maxmax); 212 maxmin = std::max(x[i].min(),maxmin); 213 } 214 GECODE_ME_CHECK(y.lq(home,maxmax)); 215 GECODE_ME_CHECK(y.gq(home,maxmin)); 216 maxmin = y.min(); 217 maxmax = y.max(); 218 int status = MPS_ASSIGNED; 219 for (int i = x.size(); i--; ) { 220 ModEvent me = x[i].lq(home,maxmax); 221 if (me == ME_FLOAT_FAILED) 222 return ES_FAILED; 223 if (me_modified(me) && (x[i].max() != maxmax)) 224 status |= MPS_NEW_BOUND; 225 if (x[i].max() < maxmin) { 226 x.move_lst(i,home,p,pc); 227 status |= MPS_REMOVED; 228 } else if (!x[i].assigned()) 229 status &= ~MPS_ASSIGNED; 230 } 231 if (x.size() == 0) 232 return ES_FAILED; 233 if ((status & MPS_REMOVED) != 0) 234 goto rerun; 235 if (((status & MPS_ASSIGNED) != 0) && y.assigned()) 236 return home.ES_SUBSUMED(p); 237 return ((status & MPS_NEW_BOUND) != 0) ? ES_NOFIX : ES_FIX; 238 } 239 240 template<class View> 241 ExecStatus 242 NaryMax<View>::propagate(Space& home, const ModEventDelta&) { 243 return prop_nary_max(home,*this,x,y,PC_FLOAT_BND); 244 } 245 246}}} 247 248// STATISTICS: float-prop 249