Generated on Sat Feb 7 2015 02:01:14 for Gecode by doxygen 1.8.9.1
mult.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  * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
6  *
7  * Copyright:
8  * Christian Schulte, 2004
9  * Vincent Barichard, 2012
10  *
11  * Last modified:
12  * $Date: 2013-02-04 21:28:39 +0100 (Mon, 04 Feb 2013) $ by $Author: schulte $
13  * $Revision: 13262 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 namespace Gecode { namespace Float { namespace Arithmetic {
41 
43  template<class View>
44  forceinline bool
45  pos(const View& x) {
46  return x.min() >= 0.0;
47  }
49  template<class View>
50  forceinline bool
51  neg(const View& x) {
52  return x.max() <= 0.0;
53  }
55  template<class View>
56  forceinline bool
57  any(const View& x) {
58  return (x.min() <= 0.0) && (x.max() >= 0.0);
59  }
60 
61  /*
62  * Propagator for x * y = x
63  *
64  */
65 
66  template<class View>
68  MultZeroOne<View>::MultZeroOne(Home home, View x0, View x1)
69  : BinaryPropagator<View,PC_FLOAT_BND>(home,x0,x1) {}
70 
71  template<class View>
73  MultZeroOne<View>::post(Home home, View x0, View x1) {
74  switch (rtest_eq(x0,0.0)) {
75  case RT_FALSE:
76  GECODE_ME_CHECK(x1.eq(home,1.0));
77  break;
78  case RT_TRUE:
79  break;
80  case RT_MAYBE:
81  switch (rtest_eq(x1,1.0)) {
82  case RT_FALSE:
83  GECODE_ME_CHECK(x0.eq(home,0.0));
84  break;
85  case RT_TRUE:
86  break;
87  case RT_MAYBE:
88  (void) new (home) MultZeroOne<View>(home,x0,x1);
89  break;
90  default: GECODE_NEVER;
91  }
92  break;
93  default: GECODE_NEVER;
94  }
95  return ES_OK;
96  }
97 
98  template<class View>
102  : BinaryPropagator<View,PC_FLOAT_BND>(home,share,p) {}
103 
104  template<class View>
105  Actor*
106  MultZeroOne<View>::copy(Space& home, bool share) {
107  return new (home) MultZeroOne<View>(home,share,*this);
108  }
109 
110  template<class View>
111  ExecStatus
113  switch (rtest_eq(x0,0.0)) {
114  case RT_FALSE:
115  GECODE_ME_CHECK(x1.eq(home,1.0));
116  break;
117  case RT_TRUE:
118  break;
119  case RT_MAYBE:
120  switch (rtest_eq(x1,1.0)) {
121  case RT_FALSE:
122  GECODE_ME_CHECK(x0.eq(home,0.0));
123  break;
124  case RT_TRUE:
125  break;
126  case RT_MAYBE:
127  return ES_FIX;
128  default: GECODE_NEVER;
129  }
130  break;
131  default: GECODE_NEVER;
132  }
133  return home.ES_SUBSUMED(*this);
134  }
135 
136 
137  /*
138  * Positive bounds consistent multiplication
139  *
140  */
141  template<class VA, class VB, class VC>
143  MultPlus<VA,VB,VC>::MultPlus(Home home, VA x0, VB x1, VC x2)
145  (home,x0,x1,x2) {}
146 
147  template<class VA, class VB, class VC>
152  (home,share,p) {}
153 
154  template<class VA, class VB, class VC>
155  Actor*
156  MultPlus<VA,VB,VC>::copy(Space& home, bool share) {
157  return new (home) MultPlus<VA,VB,VC>(home,share,*this);
158  }
159 
160  template<class VA, class VB, class VC>
161  ExecStatus
163  if (x1.min() != 0.0)
164  GECODE_ME_CHECK(x0.eq(home,x2.val() / x1.val()));
165  if (x0.min() != 0.0)
166  GECODE_ME_CHECK(x1.eq(home,x2.val() / x0.val()));
167  GECODE_ME_CHECK(x2.eq(home,x0.val() * x1.val()));
168  if (x0.assigned() && x1.assigned() && x2.assigned())
169  return home.ES_SUBSUMED(*this);
170  return ES_NOFIX;
171  }
172 
173  template<class VA, class VB, class VC>
175  MultPlus<VA,VB,VC>::post(Home home, VA x0, VB x1, VC x2) {
176  GECODE_ME_CHECK(x0.gq(home,0.0));
177  GECODE_ME_CHECK(x1.gq(home,0.0));
178  Rounding r;
179  GECODE_ME_CHECK(x2.gq(home,r.mul_down(x0.min(),x1.min())));
180  (void) new (home) MultPlus<VA,VB,VC>(home,x0,x1,x2);
181  return ES_OK;
182  }
183 
184 
185  /*
186  * Bounds consistent multiplication
187  *
188  */
189  template<class View>
191  Mult<View>::Mult(Home home, View x0, View x1, View x2)
192  : TernaryPropagator<View,PC_FLOAT_BND>(home,x0,x1,x2) {}
193 
194  template<class View>
196  Mult<View>::Mult(Space& home, bool share, Mult<View>& p)
197  : TernaryPropagator<View,PC_FLOAT_BND>(home,share,p) {}
198 
199  template<class View>
200  Actor*
201  Mult<View>::copy(Space& home, bool share) {
202  return new (home) Mult<View>(home,share,*this);
203  }
204 
205  template<class View>
206  ExecStatus
208  Rounding r;
209  if (pos(x0)) {
210  if (pos(x1) || pos(x2)) goto rewrite_ppp;
211  if (neg(x1) || neg(x2)) goto rewrite_pnn;
212  goto prop_pxx;
213  }
214  if (neg(x0)) {
215  if (neg(x1) || pos(x2)) goto rewrite_nnp;
216  if (pos(x1) || neg(x2)) goto rewrite_npn;
217  goto prop_nxx;
218  }
219  if (pos(x1)) {
220  if (pos(x2)) goto rewrite_ppp;
221  if (neg(x2)) goto rewrite_npn;
222  goto prop_xpx;
223  }
224  if (neg(x1)) {
225  if (pos(x2)) goto rewrite_nnp;
226  if (neg(x2)) goto rewrite_pnn;
227  goto prop_xnx;
228  }
229 
230  assert(any(x0) && any(x1));
231  GECODE_ME_CHECK(x2.lq(home,std::max(r.mul_up(x0.max(),x1.max()),
232  r.mul_up(x0.min(),x1.min()))));
233  GECODE_ME_CHECK(x2.gq(home,std::min(r.mul_down(x0.min(),x1.max()),
234  r.mul_down(x0.max(),x1.min()))));
235 
236  if (pos(x2)) {
237  if (r.div_up(x2.min(),x1.min()) < x0.min())
238  GECODE_ME_CHECK(x0.gq(home,0));
239  if (r.div_up(x2.min(),x0.min()) < x1.min())
240  GECODE_ME_CHECK(x1.gq(home,0));
241  }
242  if (neg(x2)) {
243  if (r.div_up(x2.max(),x1.max()) < x0.min())
244  GECODE_ME_CHECK(x0.gq(home,0));
245  if (r.div_up(x2.max(),x0.max()) < x1.min())
246  GECODE_ME_CHECK(x1.gq(home,0));
247  }
248 
249  if (x0.assigned()) {
250  assert((x0.val() == 0.0) && (x2.val() == 0.0));
251  return home.ES_SUBSUMED(*this);
252  }
253 
254  if (x1.assigned()) {
255  assert((x1.val() == 0.0) && (x2.val() == 0.0));
256  return home.ES_SUBSUMED(*this);
257  }
258 
259  return ES_NOFIX;
260 
261  prop_xpx:
262  std::swap(x0,x1);
263  prop_pxx:
264  assert(pos(x0) && any(x1) && any(x2));
265 
266  GECODE_ME_CHECK(x2.lq(home,r.mul_up(x0.max(),x1.max())));
267  GECODE_ME_CHECK(x2.gq(home,r.mul_down(x0.max(),x1.min())));
268 
269  if (pos(x2)) goto rewrite_ppp;
270  if (neg(x2)) goto rewrite_pnn;
271 
272  GECODE_ME_CHECK(x1.lq(home,r.div_up(x2.max(),x0.min())));
273  GECODE_ME_CHECK(x1.gq(home,r.div_down(x2.min(),x0.min())));
274 
275  if (x0.assigned() && x1.assigned()) {
276  GECODE_ME_CHECK(x2.eq(home,x0.val()*x1.val()));
277  return home.ES_SUBSUMED(*this);
278  }
279 
280  return ES_NOFIX;
281 
282  prop_xnx:
283  std::swap(x0,x1);
284  prop_nxx:
285  assert(neg(x0) && any(x1) && any(x2));
286 
287  GECODE_ME_CHECK(x2.lq(home,r.mul_up(x0.min(),x1.min())));
288  GECODE_ME_CHECK(x2.gq(home,r.mul_down(x0.min(),x1.max())));
289 
290  if (pos(x2)) goto rewrite_nnp;
291  if (neg(x2)) goto rewrite_npn;
292 
293  if (x0.max() != 0.0) {
294  GECODE_ME_CHECK(x1.lq(home,r.div_up(x2.min(),x0.max())));
295  GECODE_ME_CHECK(x1.gq(home,r.div_down(x2.max(),x0.max())));
296  }
297 
298  if (x0.assigned() && x1.assigned()) {
299  GECODE_ME_CHECK(x2.eq(home,x0.val()*x1.val()));
300  return home.ES_SUBSUMED(*this);
301  }
302 
303  return ES_NOFIX;
304 
305  rewrite_ppp:
307  ::post(home(*this),x0,x1,x2)));
308  rewrite_nnp:
310  ::post(home(*this),MinusView(x0),MinusView(x1),x2)));
311  rewrite_pnn:
312  std::swap(x0,x1);
313  rewrite_npn:
315  ::post(home(*this),MinusView(x0),x1,MinusView(x2))));
316  }
317 
318  template<class View>
319  ExecStatus
320  Mult<View>::post(Home home, View x0, View x1, View x2) {
321  if (same(x0,x1))
322  return Sqr<View>::post(home,x0,x2);
323  if (same(x0,x2))
324  return MultZeroOne<View>::post(home,x0,x1);
325  if (same(x1,x2))
326  return MultZeroOne<View>::post(home,x1,x0);
327  if (pos(x0)) {
328  if (pos(x1) || pos(x2)) goto post_ppp;
329  if (neg(x1) || neg(x2)) goto post_pnn;
330  } else if (neg(x0)) {
331  if (neg(x1) || pos(x2)) goto post_nnp;
332  if (pos(x1) || neg(x2)) goto post_npn;
333  } else if (pos(x1)) {
334  if (pos(x2)) goto post_ppp;
335  if (neg(x2)) goto post_npn;
336  } else if (neg(x1)) {
337  if (pos(x2)) goto post_nnp;
338  if (neg(x2)) goto post_pnn;
339  }
340  {
341  GECODE_ME_CHECK(x2.eq(home,x0.val()*x1.val()));
342  (void) new (home) Mult<View>(home,x0,x1,x2);
343  }
344  return ES_OK;
345 
346  post_ppp:
347  return MultPlus<FloatView,FloatView,FloatView>::post(home,x0,x1,x2);
348  post_nnp:
350  MinusView(x0),MinusView(x1),x2);
351  post_pnn:
352  std::swap(x0,x1);
353  post_npn:
355  MinusView(x0),x1,MinusView(x2));
356  }
357 
358 
359 }}}
360 
361 // STATISTICS: float-prop
362 
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:109
FloatNum div_up(FloatNum x, FloatNum y)
Return upper bound of x divided y (domain: )
FloatNum mul_down(FloatNum x, FloatNum y)
Return lower bound of x times y (domain: )
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:228
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
bool neg(const View &x)
Test whether x is negative.
Definition: mult.hpp:51
bool any(const View &x)
Test whether x is neither positive nor negative.
Definition: mult.hpp:57
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: mult.hpp:156
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
Relation does hold.
Definition: view.hpp:498
MultPlus(Home home, VA x0, VB x1, VC x2)
Constructor for posting.
Definition: mult.hpp:143
Bounds consistent positive multiplication propagator.
Definition: arithmetic.hh:248
Relation does not hold.
Definition: view.hpp:496
Propagation has computed fixpoint.
Definition: core.hpp:528
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
Mult(Space &home, bool share, Mult< View > &p)
Constructor for cloning p.
Definition: mult.hpp:196
static ExecStatus post(Home home, View x0, View x1)
Post propagator .
Definition: mult.hpp:73
static ExecStatus post(Home home, View x0, View x1)
Post propagator for .
bool same(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether two views are the same.
Definition: view.hpp:603
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Binary propagator.
Definition: propagator.hpp:87
Mixed ternary propagator.
Definition: propagator.hpp:235
Bounds or domain consistent propagator for .
Definition: arithmetic.hh:223
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: mult.hpp:162
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: mult.hpp:112
Ternary propagator.
Definition: propagator.hpp:115
Bounds consistent multiplication propagator.
Definition: arithmetic.hh:275
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
MultZeroOne(Space &home, bool share, MultZeroOne< View > &p)
Constructor for cloning p.
Definition: mult.hpp:100
Floating point rounding policy.
Definition: float.hh:137
static ExecStatus post(Home home, VA x0, VB x1, VC x2)
Post propagator .
Definition: mult.hpp:175
RelTest rtest_eq(View x, View y)
Test whether views x and y are equal.
Definition: rel-test.hpp:44
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Minus float view.
Definition: view.hpp:158
ExecStatus
Definition: core.hpp:523
FloatNum div_down(FloatNum x, FloatNum y)
Return lower bound of x divided by y (domain: )
#define forceinline
Definition: config.hpp:132
Execution is okay.
Definition: core.hpp:527
Propagation has not computed fixpoint.
Definition: core.hpp:526
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: mult.hpp:207
const Gecode::PropCond PC_FLOAT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:292
static ExecStatus post(Home home, View x0, View x1, View x2)
Post propagator .
Definition: mult.hpp:320
Gecode toplevel namespace
Relation may hold or not.
Definition: view.hpp:497
FloatNum mul_up(FloatNum x, FloatNum y)
Return upper bound of x times y (domain: )
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: mult.hpp:106
virtual Actor * copy(Space &home, bool share)
Copy propagator during cloning.
Definition: mult.hpp:201