Generated on Sat Feb 7 2015 02:01:14 for Gecode by doxygen 1.8.9.1
sqr-sqrt-abs.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  * Guido Tack <tack@gecode.org>
6  * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
7  *
8  * Copyright:
9  * Christian Schulte, 2004
10  * Guido Tack, 2006
11  * Vincent Barichard, 2012
12  *
13  * Last modified:
14  * $Date: 2013-02-04 21:28:39 +0100 (Mon, 04 Feb 2013) $ by $Author: schulte $
15  * $Revision: 13262 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 namespace Gecode { namespace Float { namespace Arithmetic {
43 
44  /*
45  * Positive bounds consistent squaring
46  *
47  */
48  template<class VA, class VB>
50  SqrPlus<VA,VB>::SqrPlus(Home home, VA x0, VB x1)
51  : MixBinaryPropagator<VA,PC_FLOAT_BND,VB,PC_FLOAT_BND>(home,x0,x1) {}
52 
53  template<class VA, class VB>
55  SqrPlus<VA,VB>::post(Home home, VA x0, VB x1) {
56  if (same(x0,x1)) {
57  if (x0.assigned())
58  return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED;
59  } else {
60  GECODE_ME_CHECK(x0.eq(home,sqrt(x1.val())));
61  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
62  }
63 
64  (void) new (home) SqrPlus<VA,VB>(home,x0,x1);
65  return ES_OK;
66  }
67 
68  template<class VA, class VB>
71  : MixBinaryPropagator<VA,PC_FLOAT_BND,VB,PC_FLOAT_BND>(home,share,p) {}
72 
73  template<class VA, class VB>
74  Actor*
75  SqrPlus<VA,VB>::copy(Space& home, bool share) {
76  return new (home) SqrPlus<VA,VB>(home,share,*this);
77  }
78 
79  template<class VA, class VB>
82  if (same(x0,x1)) {
83  if (x0.max() < 1) GECODE_ME_CHECK(x0.eq(home,0));
84  else if (x0.min() > 0) GECODE_ME_CHECK(x0.eq(home,1));
85  if (x0.assigned())
86  return ((x0.val() == 0) || (x0.val() == 1))? home.ES_SUBSUMED(*this) : ES_FAILED;
87  } else {
88  GECODE_ME_CHECK(x0.eq(home,sqrt(x1.val())));
89  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
90  if (x0.assigned() || x1.assigned()) return home.ES_SUBSUMED(*this);
91  }
92 
93  return ES_FIX;
94  }
95 
96 
97  /*
98  * Bounds consistent squaring
99  *
100  */
101 
102  template<class View>
104  Sqr<View>::Sqr(Home home, View x0, View x1)
105  : BinaryPropagator<View,PC_FLOAT_BND>(home,x0,x1) {}
106 
107  template<class View>
109  Sqr<View>::post(Home home, View x0, View x1) {
110  GECODE_ME_CHECK(x1.gq(home,0));
111  if (same(x0,x1)) {
112  if (x0.assigned())
113  return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED;
114  GECODE_ME_CHECK(x1.lq(home,1));
115  return SqrPlus<FloatView,FloatView>::post(home,x0,x1);
116  } else {
117  if (x0.min() >= 0)
118  return SqrPlus<FloatView,FloatView>::post(home,x0,x1);
119  if (x0.max() <= 0)
120  return SqrPlus<MinusView,FloatView>::post(home,MinusView(x0),x1);
121  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
122  (void) new (home) Sqr<View>(home,x0,x1);
123  }
124  return ES_OK;
125  }
126 
127  template<class View>
129  Sqr<View>::Sqr(Space& home, bool share, Sqr<View>& p)
130  : BinaryPropagator<View,PC_FLOAT_BND>(home,share,p) {}
131 
132  template<class View>
133  Actor*
134  Sqr<View>::copy(Space& home, bool share) {
135  return new (home) Sqr<View>(home,share,*this);
136  }
137 
138  template<class View>
139  ExecStatus
141  assert(x1.min() >= 0);
142  if (x0.min() >= 0)
143  GECODE_REWRITE(*this,(SqrPlus<FloatView,FloatView>::post(home(*this),x0,x1)));
144  if (x0.max() <= 0)
146  MinusView(x0),x1)));
147 
148  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
149  Rounding r;
150  FloatVal z = sqrt(x1.val());
151  if (x0.min() > -r.sqrt_up(x1.min()))
152  GECODE_ME_CHECK(x0.eq(home,z));
153  else if (x0.max() < r.sqrt_down(x1.min()))
154  GECODE_ME_CHECK(x0.eq(home,-z));
155  else
156  GECODE_ME_CHECK(x0.eq(home,hull(z,-z)));
157 
158  return ES_NOFIX;
159  }
160 
161 
162  /*
163  * Bounds consistent square root operator
164  *
165  */
166 
167  template<class A, class B>
169  Sqrt<A,B>::Sqrt(Home home, A x0, B x1)
170  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,x0,x1) {}
171 
172  template<class A, class B>
173  ExecStatus
174  Sqrt<A,B>::post(Home home, A x0, B x1) {
175  GECODE_ME_CHECK(x0.gq(home,0));
176  if (same(x0,x1)) {
177  if (x0.assigned())
178  return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED;
179  GECODE_ME_CHECK(x0.lq(home,1));
180  (void) new (home) Sqrt<A,B>(home,x0,x1);
181  } else {
182  GECODE_ME_CHECK(x1.eq(home,sqrt(x0.val())));
183  (void) new (home) Sqrt<A,B>(home,x0,x1);
184  }
185  return ES_OK;
186  }
187 
188  template<class A, class B>
190  Sqrt<A,B>::Sqrt(Space& home, bool share, Sqrt<A,B>& p)
191  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,share,p) {}
192 
193  template<class A, class B>
194  Actor*
195  Sqrt<A,B>::copy(Space& home, bool share) {
196  return new (home) Sqrt<A,B>(home,share,*this);
197  }
198 
199  template<class A, class B>
200  ExecStatus
202  if (same(x0,x1)) {
203  if (x0.max() < 1) GECODE_ME_CHECK(x0.eq(home,0));
204  else if (x0.min() > 0) GECODE_ME_CHECK(x0.eq(home,1));
205  if (x0.assigned())
206  return ((x0.val() == 0) || (x0.val() == 1))? home.ES_SUBSUMED(*this) : ES_FAILED;
207  } else {
208  GECODE_ME_CHECK(x0.eq(home,sqr(x1.val())));
209  GECODE_ME_CHECK(x1.eq(home,sqrt(x0.val())));
210  if (x0.assigned() || x1.assigned()) return home.ES_SUBSUMED(*this);
211  }
212 
213  return ES_FIX;
214  }
215 
216  /*
217  * Absolute consistent square operator
218  *
219  */
220 
221  template<class A, class B>
223  Abs<A,B>::Abs(Home home, A x0, B x1)
224  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,x0,x1) {}
225 
226  template<class A, class B>
227  ExecStatus
228  Abs<A,B>::post(Home home, A x0, B x1) {
229  (void) new (home) Abs<A,B>(home,x0,x1);
230  return ES_OK;
231  }
232 
233  template<class A, class B>
235  Abs<A,B>::Abs(Space& home, bool share, Abs<A,B>& p)
236  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,share,p) {}
237 
238  template<class A, class B>
239  Actor*
240  Abs<A,B>::copy(Space& home, bool share) {
241  return new (home) Abs<A,B>(home,share,*this);
242  }
243 
244  template<class A, class B>
245  ExecStatus
247  GECODE_ME_CHECK(x1.eq(home,abs(x0.val())));
248  if (x0.min() >= 0)
249  GECODE_ME_CHECK(x0.eq(home,FloatVal(x1.min(), x1.max())));
250  else if (x0.max() <= 0)
251  GECODE_ME_CHECK(x0.eq(home,FloatVal(-x1.max(), -x1.min())));
252  else
253  GECODE_ME_CHECK(x0.eq(home,FloatVal(-x1.max(), x1.max())));
254  return (x0.assigned() && x1.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX;
255  }
256 
257 }}}
258 
259 // STATISTICS: float-prop
260 
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:109
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:49
Sqrt(Space &home, bool share, Sqrt &p)
Constructor for cloning p.
FloatVal hull(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:511
Propagation has computed fixpoint.
Definition: core.hpp:528
Sqr(Space &home, bool share, Sqr &p)
Constructor for cloning p.
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
static ExecStatus post(Home home, View x0, View x1)
Post propagator for .
static ExecStatus post(Home home, VA x0, VB x1)
Post propagator .
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
bool same(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether two views are the same.
Definition: view.hpp:603
Bounds consistent positive square propagator.
Definition: arithmetic.hh:62
Propagator for bounds consistent square operator
Definition: arithmetic.hh:86
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Execution has resulted in failure.
Definition: core.hpp:525
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Binary propagator.
Definition: propagator.hpp:87
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:103
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
Propagator for bounds consistent square root operator
Definition: arithmetic.hh:113
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:110
Propagator for bounds consistent absolute operator
Definition: arithmetic.hh:140
Abs(Space &home, bool share, Abs &p)
Constructor for cloning p.
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
Floating point rounding policy.
Definition: float.hh:137
Float value type.
Definition: float.hh:321
Mixed binary propagator.
Definition: propagator.hpp:203
Minus float view.
Definition: view.hpp:158
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
ExecStatus
Definition: core.hpp:523
#define forceinline
Definition: config.hpp:132
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
const Gecode::PropCond PC_FLOAT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:292
Gecode toplevel namespace
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Home class for posting propagators
Definition: core.hpp:717
SqrPlus(Home home, VA x0, VB x1)
Constructor for posting.
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.