Generated on Sat Jul 29 2017 12:41:24 for Gecode by doxygen 1.8.13
sqr-sqrt.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: 2017-04-10 13:21:37 +0200 (Mon, 10 Apr 2017) $ by $Author: schulte $
15  * $Revision: 15631 $
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)
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)
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 
218 // STATISTICS: float-prop
219 
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:120
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
Sqrt(Space &home, bool share, Sqrt &p)
Constructor for cloning p.
Definition: sqr-sqrt.hpp:190
FloatVal hull(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:511
Propagation has computed fixpoint.
Definition: core.hpp:545
Sqr(Space &home, bool share, Sqr &p)
Constructor for cloning p.
Definition: sqr-sqrt.hpp:129
Computation spaces.
Definition: core.hpp:1748
Base-class for both propagators and branchers.
Definition: core.hpp:696
static ExecStatus post(Home home, View x0, View x1)
Post propagator for .
Definition: sqr-sqrt.hpp:109
static ExecStatus post(Home home, VA x0, VB x1)
Post propagator .
Definition: sqr-sqrt.hpp:55
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:643
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.
Definition: sqr-sqrt.hpp:81
Execution has resulted in failure.
Definition: core.hpp:542
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
Definition: sqr-sqrt.hpp:134
Binary propagator.
Definition: propagator.hpp:89
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:99
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
Definition: sqr-sqrt.hpp:195
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:106
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition: set.hh:784
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:56
Floating point rounding policy.
Definition: float.hh:158
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:784
Float value type.
Definition: float.hh:338
Mixed binary propagator.
Definition: propagator.hpp:213
Minus float view.
Definition: view.hpp:158
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sqr-sqrt.hpp:140
ExecStatus
Definition: core.hpp:540
#define forceinline
Definition: config.hpp:173
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
Definition: sqr-sqrt.hpp:174
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sqr-sqrt.hpp:201
Execution is okay.
Definition: core.hpp:544
Propagation has not computed fixpoint.
Definition: core.hpp:543
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:169
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: sqr-sqrt.hpp:75
Home class for posting propagators
Definition: core.hpp:922
SqrPlus(Home home, VA x0, VB x1)
Constructor for posting.
Definition: sqr-sqrt.hpp:50