Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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$ by $Author$
13  * $Revision$
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>
101  : BinaryPropagator<View,PC_FLOAT_BND>(home,p) {}
102 
103  template<class View>
104  Actor*
106  return new (home) MultZeroOne<View>(home,*this);
107  }
108 
109  template<class View>
110  ExecStatus
112  switch (rtest_eq(x0,0.0)) {
113  case RT_FALSE:
114  GECODE_ME_CHECK(x1.eq(home,1.0));
115  break;
116  case RT_TRUE:
117  break;
118  case RT_MAYBE:
119  switch (rtest_eq(x1,1.0)) {
120  case RT_FALSE:
121  GECODE_ME_CHECK(x0.eq(home,0.0));
122  break;
123  case RT_TRUE:
124  break;
125  case RT_MAYBE:
126  return ES_FIX;
127  default: GECODE_NEVER;
128  }
129  break;
130  default: GECODE_NEVER;
131  }
132  return home.ES_SUBSUMED(*this);
133  }
134 
135 
136  /*
137  * Positive bounds consistent multiplication
138  *
139  */
140  template<class VA, class VB, class VC>
142  MultPlus<VA,VB,VC>::MultPlus(Home home, VA x0, VB x1, VC x2)
144  (home,x0,x1,x2) {}
145 
146  template<class VA, class VB, class VC>
150  (home,p) {}
151 
152  template<class VA, class VB, class VC>
153  Actor*
155  return new (home) MultPlus<VA,VB,VC>(home,*this);
156  }
157 
158  template<class VA, class VB, class VC>
159  ExecStatus
161  if (x1.min() != 0.0)
162  GECODE_ME_CHECK(x0.eq(home,x2.val() / x1.val()));
163  if (x0.min() != 0.0)
164  GECODE_ME_CHECK(x1.eq(home,x2.val() / x0.val()));
165  GECODE_ME_CHECK(x2.eq(home,x0.val() * x1.val()));
166  if (x0.assigned() && x1.assigned() && x2.assigned())
167  return home.ES_SUBSUMED(*this);
168  return ES_NOFIX;
169  }
170 
171  template<class VA, class VB, class VC>
173  MultPlus<VA,VB,VC>::post(Home home, VA x0, VB x1, VC x2) {
174  GECODE_ME_CHECK(x0.gq(home,0.0));
175  GECODE_ME_CHECK(x1.gq(home,0.0));
176  Rounding r;
177  GECODE_ME_CHECK(x2.gq(home,r.mul_down(x0.min(),x1.min())));
178  (void) new (home) MultPlus<VA,VB,VC>(home,x0,x1,x2);
179  return ES_OK;
180  }
181 
182 
183  /*
184  * Bounds consistent multiplication
185  *
186  */
187  template<class View>
189  Mult<View>::Mult(Home home, View x0, View x1, View x2)
190  : TernaryPropagator<View,PC_FLOAT_BND>(home,x0,x1,x2) {}
191 
192  template<class View>
195  : TernaryPropagator<View,PC_FLOAT_BND>(home,p) {}
196 
197  template<class View>
198  Actor*
200  return new (home) Mult<View>(home,*this);
201  }
202 
203  template<class View>
204  ExecStatus
206  Rounding r;
207  if (pos(x0)) {
208  if (pos(x1) || pos(x2)) goto rewrite_ppp;
209  if (neg(x1) || neg(x2)) goto rewrite_pnn;
210  goto prop_pxx;
211  }
212  if (neg(x0)) {
213  if (neg(x1) || pos(x2)) goto rewrite_nnp;
214  if (pos(x1) || neg(x2)) goto rewrite_npn;
215  goto prop_nxx;
216  }
217  if (pos(x1)) {
218  if (pos(x2)) goto rewrite_ppp;
219  if (neg(x2)) goto rewrite_npn;
220  goto prop_xpx;
221  }
222  if (neg(x1)) {
223  if (pos(x2)) goto rewrite_nnp;
224  if (neg(x2)) goto rewrite_pnn;
225  goto prop_xnx;
226  }
227 
228  assert(any(x0) && any(x1));
229  GECODE_ME_CHECK(x2.lq(home,std::max(r.mul_up(x0.max(),x1.max()),
230  r.mul_up(x0.min(),x1.min()))));
231  GECODE_ME_CHECK(x2.gq(home,std::min(r.mul_down(x0.min(),x1.max()),
232  r.mul_down(x0.max(),x1.min()))));
233 
234  if (pos(x2)) {
235  if (r.div_up(x2.min(),x1.min()) < x0.min())
236  GECODE_ME_CHECK(x0.gq(home,0));
237  if (r.div_up(x2.min(),x0.min()) < x1.min())
238  GECODE_ME_CHECK(x1.gq(home,0));
239  }
240  if (neg(x2)) {
241  if (r.div_up(x2.max(),x1.max()) < x0.min())
242  GECODE_ME_CHECK(x0.gq(home,0));
243  if (r.div_up(x2.max(),x0.max()) < x1.min())
244  GECODE_ME_CHECK(x1.gq(home,0));
245  }
246 
247  if (x0.assigned()) {
248  assert((x0.val() == 0.0) && (x2.val() == 0.0));
249  return home.ES_SUBSUMED(*this);
250  }
251 
252  if (x1.assigned()) {
253  assert((x1.val() == 0.0) && (x2.val() == 0.0));
254  return home.ES_SUBSUMED(*this);
255  }
256 
257  return ES_NOFIX;
258 
259  prop_xpx:
260  std::swap(x0,x1);
261  prop_pxx:
262  assert(pos(x0) && any(x1) && any(x2));
263 
264  GECODE_ME_CHECK(x2.lq(home,r.mul_up(x0.max(),x1.max())));
265  GECODE_ME_CHECK(x2.gq(home,r.mul_down(x0.max(),x1.min())));
266 
267  if (pos(x2)) goto rewrite_ppp;
268  if (neg(x2)) goto rewrite_pnn;
269 
270  GECODE_ME_CHECK(x1.lq(home,r.div_up(x2.max(),x0.min())));
271  GECODE_ME_CHECK(x1.gq(home,r.div_down(x2.min(),x0.min())));
272 
273  if (x0.assigned() && x1.assigned()) {
274  GECODE_ME_CHECK(x2.eq(home,x0.val()*x1.val()));
275  return home.ES_SUBSUMED(*this);
276  }
277 
278  return ES_NOFIX;
279 
280  prop_xnx:
281  std::swap(x0,x1);
282  prop_nxx:
283  assert(neg(x0) && any(x1) && any(x2));
284 
285  GECODE_ME_CHECK(x2.lq(home,r.mul_up(x0.min(),x1.min())));
286  GECODE_ME_CHECK(x2.gq(home,r.mul_down(x0.min(),x1.max())));
287 
288  if (pos(x2)) goto rewrite_nnp;
289  if (neg(x2)) goto rewrite_npn;
290 
291  if (x0.max() != 0.0) {
292  GECODE_ME_CHECK(x1.lq(home,r.div_up(x2.min(),x0.max())));
293  GECODE_ME_CHECK(x1.gq(home,r.div_down(x2.max(),x0.max())));
294  }
295 
296  if (x0.assigned() && x1.assigned()) {
297  GECODE_ME_CHECK(x2.eq(home,x0.val()*x1.val()));
298  return home.ES_SUBSUMED(*this);
299  }
300 
301  return ES_NOFIX;
302 
303  rewrite_ppp:
305  ::post(home(*this),x0,x1,x2)));
306  rewrite_nnp:
308  ::post(home(*this),MinusView(x0),MinusView(x1),x2)));
309  rewrite_pnn:
310  std::swap(x0,x1);
311  rewrite_npn:
313  ::post(home(*this),MinusView(x0),x1,MinusView(x2))));
314  }
315 
316  template<class View>
317  ExecStatus
318  Mult<View>::post(Home home, View x0, View x1, View x2) {
319  if (same(x0,x1))
320  return Sqr<View>::post(home,x0,x2);
321  if (same(x0,x2))
322  return MultZeroOne<View>::post(home,x0,x1);
323  if (same(x1,x2))
324  return MultZeroOne<View>::post(home,x1,x0);
325  if (pos(x0)) {
326  if (pos(x1) || pos(x2)) goto post_ppp;
327  if (neg(x1) || neg(x2)) goto post_pnn;
328  } else if (neg(x0)) {
329  if (neg(x1) || pos(x2)) goto post_nnp;
330  if (pos(x1) || neg(x2)) goto post_npn;
331  } else if (pos(x1)) {
332  if (pos(x2)) goto post_ppp;
333  if (neg(x2)) goto post_npn;
334  } else if (neg(x1)) {
335  if (pos(x2)) goto post_nnp;
336  if (neg(x2)) goto post_pnn;
337  }
338  {
339  GECODE_ME_CHECK(x2.eq(home,x0.val()*x1.val()));
340  (void) new (home) Mult<View>(home,x0,x1,x2);
341  }
342  return ES_OK;
343 
344  post_ppp:
345  return MultPlus<FloatView,FloatView,FloatView>::post(home,x0,x1,x2);
346  post_nnp:
348  MinusView(x0),MinusView(x1),x2);
349  post_pnn:
350  std::swap(x0,x1);
351  post_npn:
353  MinusView(x0),x1,MinusView(x2));
354  }
355 
356 
357 }}}
358 
359 // STATISTICS: float-prop
360 
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition: macros.hpp:120
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: )
MultZeroOne(Space &home, MultZeroOne< View > &p)
Constructor for cloning p.
Definition: mult.hpp:100
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
const FloatNum max
Largest allowed float value.
Definition: float.hh:848
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
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
Relation does hold.
Definition: view.hpp:498
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: mult.hpp:154
MultPlus(Home home, VA x0, VB x1, VC x2)
Constructor for posting.
Definition: mult.hpp:142
Bounds consistent positive multiplication propagator.
Definition: arithmetic.hh:248
Relation does not hold.
Definition: view.hpp:496
#define forceinline
Definition: config.hpp:182
Propagation has computed fixpoint.
Definition: core.hpp:469
Computation spaces.
Definition: core.hpp:1668
Base-class for both propagators and branchers.
Definition: core.hpp:620
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 .
Definition: sqr-sqrt.hpp:109
bool same(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether two views are the same.
Definition: view.hpp:643
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
Binary propagator.
Definition: pattern.hpp:88
Mixed ternary propagator.
Definition: pattern.hpp:241
Bounds or domain consistent propagator for .
Definition: arithmetic.hh:223
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: mult.hpp:160
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: mult.hpp:111
Ternary propagator.
Definition: pattern.hpp:117
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:56
Floating point rounding policy.
Definition: float.hh:158
Mult(Space &home, Mult< View > &p)
Constructor for cloning p.
Definition: mult.hpp:194
static ExecStatus post(Home home, VA x0, VB x1, VC x2)
Post propagator .
Definition: mult.hpp:173
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
RelTest rtest_eq(View x, View y)
Test whether views x and y are equal.
Definition: rel-test.hpp:44
Minus float view.
Definition: view.hpp:158
ExecStatus
Definition: core.hpp:464
FloatNum div_down(FloatNum x, FloatNum y)
Return lower bound of x divided by y (domain: )
Post propagator for SetVar x
Definition: set.hh:769
Execution is okay.
Definition: core.hpp:468
Propagation has not computed fixpoint.
Definition: core.hpp:467
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: mult.hpp:205
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:318
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: mult.hpp:199
Gecode toplevel namespace
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition: mult.hpp:105
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:91
Home class for posting propagators
Definition: core.hpp:846
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
IntRelType swap(IntRelType irt)
Return swapped relation type of irt.
Definition: irt.hpp:41