Generated on Sat Jan 12 2019 20:58:51 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  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Float { namespace Arithmetic {
39 
40  /*
41  * Positive bounds consistent squaring
42  *
43  */
44  template<class VA, class VB>
46  SqrPlus<VA,VB>::SqrPlus(Home home, VA x0, VB x1)
47  : MixBinaryPropagator<VA,PC_FLOAT_BND,VB,PC_FLOAT_BND>(home,x0,x1) {}
48 
49  template<class VA, class VB>
51  SqrPlus<VA,VB>::post(Home home, VA x0, VB x1) {
52  if (x0 == x1) {
53  if (x0.assigned())
54  return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED;
55  } else {
56  GECODE_ME_CHECK(x0.eq(home,sqrt(x1.val())));
57  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
58  }
59 
60  (void) new (home) SqrPlus<VA,VB>(home,x0,x1);
61  return ES_OK;
62  }
63 
64  template<class VA, class VB>
68 
69  template<class VA, class VB>
70  Actor*
72  return new (home) SqrPlus<VA,VB>(home,*this);
73  }
74 
75  template<class VA, class VB>
78  if (x0 == x1) {
79  if (x0.max() < 1) GECODE_ME_CHECK(x0.eq(home,0));
80  else if (x0.min() > 0) GECODE_ME_CHECK(x0.eq(home,1));
81  if (x0.assigned())
82  return ((x0.val() == 0) || (x0.val() == 1))? home.ES_SUBSUMED(*this) : ES_FAILED;
83  } else {
84  GECODE_ME_CHECK(x0.eq(home,sqrt(x1.val())));
85  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
86  if (x0.assigned() || x1.assigned()) return home.ES_SUBSUMED(*this);
87  }
88 
89  return ES_FIX;
90  }
91 
92 
93  /*
94  * Bounds consistent squaring
95  *
96  */
97 
98  template<class View>
100  Sqr<View>::Sqr(Home home, View x0, View x1)
101  : BinaryPropagator<View,PC_FLOAT_BND>(home,x0,x1) {}
102 
103  template<class View>
105  Sqr<View>::post(Home home, View x0, View x1) {
106  GECODE_ME_CHECK(x1.gq(home,0));
107  if (x0 == x1) {
108  if (x0.assigned())
109  return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED;
110  GECODE_ME_CHECK(x1.lq(home,1));
111  return SqrPlus<FloatView,FloatView>::post(home,x0,x1);
112  } else {
113  if (x0.min() >= 0)
114  return SqrPlus<FloatView,FloatView>::post(home,x0,x1);
115  if (x0.max() <= 0)
117  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
118  (void) new (home) Sqr<View>(home,x0,x1);
119  }
120  return ES_OK;
121  }
122 
123  template<class View>
126  : BinaryPropagator<View,PC_FLOAT_BND>(home,p) {}
127 
128  template<class View>
129  Actor*
131  return new (home) Sqr<View>(home,*this);
132  }
133 
134  template<class View>
135  ExecStatus
137  assert(x1.min() >= 0);
138  if (x0.min() >= 0)
140  if (x0.max() <= 0)
142  MinusView(x0),x1)));
143 
144  GECODE_ME_CHECK(x1.eq(home,sqr(x0.val())));
145  Rounding r;
146  FloatVal z = sqrt(x1.val());
147  if (x0.min() > -r.sqrt_up(x1.min()))
148  GECODE_ME_CHECK(x0.eq(home,z));
149  else if (x0.max() < r.sqrt_down(x1.min()))
150  GECODE_ME_CHECK(x0.eq(home,-z));
151  else
152  GECODE_ME_CHECK(x0.eq(home,hull(z,-z)));
153 
154  return ES_NOFIX;
155  }
156 
157 
158  /*
159  * Bounds consistent square root operator
160  *
161  */
162 
163  template<class A, class B>
165  Sqrt<A,B>::Sqrt(Home home, A x0, B x1)
166  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,x0,x1) {}
167 
168  template<class A, class B>
169  ExecStatus
170  Sqrt<A,B>::post(Home home, A x0, B x1) {
171  GECODE_ME_CHECK(x0.gq(home,0));
172  if (x0 == x1) {
173  if (x0.assigned())
174  return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED;
175  GECODE_ME_CHECK(x0.lq(home,1));
176  (void) new (home) Sqrt<A,B>(home,x0,x1);
177  } else {
178  GECODE_ME_CHECK(x1.eq(home,sqrt(x0.val())));
179  (void) new (home) Sqrt<A,B>(home,x0,x1);
180  }
181  return ES_OK;
182  }
183 
184  template<class A, class B>
188 
189  template<class A, class B>
190  Actor*
192  return new (home) Sqrt<A,B>(home,*this);
193  }
194 
195  template<class A, class B>
196  ExecStatus
198  if (x0 == x1) {
199  if (x0.max() < 1) GECODE_ME_CHECK(x0.eq(home,0));
200  else if (x0.min() > 0) GECODE_ME_CHECK(x0.eq(home,1));
201  if (x0.assigned())
202  return ((x0.val() == 0) || (x0.val() == 1))? home.ES_SUBSUMED(*this) : ES_FAILED;
203  } else {
204  GECODE_ME_CHECK(x0.eq(home,sqr(x1.val())));
205  GECODE_ME_CHECK(x1.eq(home,sqrt(x0.val())));
206  if (x0.assigned() || x1.assigned()) return home.ES_SUBSUMED(*this);
207  }
208 
209  return ES_FIX;
210  }
211 
212 }}}
213 
214 // STATISTICS: float-prop
215 
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:116
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3490
FloatVal hull(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:507
Sqrt(Space &home, Sqrt &p)
Constructor for cloning p.
Definition: sqr-sqrt.hpp:186
#define forceinline
Definition: config.hpp:185
Propagation has computed fixpoint.
Definition: core.hpp:476
virtual Actor * copy(Space &home)
Create copy during cloning.
Definition: sqr-sqrt.hpp:130
Computation spaces.
Definition: core.hpp:1701
Base-class for both propagators and branchers.
Definition: core.hpp:627
static ExecStatus post(Home home, View x0, View x1)
Post propagator for .
Definition: sqr-sqrt.hpp:105
static ExecStatus post(Home home, VA x0, VB x1)
Post propagator .
Definition: sqr-sqrt.hpp:51
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Bounds consistent positive square propagator.
Definition: arithmetic.hh:58
virtual Actor * copy(Space &home)
Create copy during cloning.
Definition: sqr-sqrt.hpp:191
Propagator for bounds consistent square operator
Definition: arithmetic.hh:82
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sqr-sqrt.hpp:77
Execution has resulted in failure.
Definition: core.hpp:473
Binary propagator.
Definition: pattern.hpp:84
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:95
Propagator for bounds consistent square root operator
Definition: arithmetic.hh:109
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:102
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition: set.hh:767
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:52
Floating point rounding policy.
Definition: float.hh:154
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:767
Float value type.
Definition: float.hh:334
Mixed binary propagator.
Definition: pattern.hpp:204
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: sqr-sqrt.hpp:71
Minus float view.
Definition: view.hpp:154
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sqr-sqrt.hpp:136
ExecStatus
Definition: core.hpp:471
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
Definition: sqr-sqrt.hpp:170
Sqr(Space &home, Sqr &p)
Constructor for cloning p.
Definition: sqr-sqrt.hpp:125
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sqr-sqrt.hpp:197
Execution is okay.
Definition: core.hpp:475
Propagation has not computed fixpoint.
Definition: core.hpp:474
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:89
Home class for posting propagators
Definition: core.hpp:853
SqrPlus(Home home, VA x0, VB x1)
Constructor for posting.
Definition: sqr-sqrt.hpp:46