Generated on Tue Jan 28 2020 00:00:00 for Gecode by doxygen 1.8.17
mm-arithmetic.cpp
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  *
6  * Copyright:
7  * Christian Schulte, 2008
8  *
9  * Last modified:
10  * $Date: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
11  * $Revision: 14672 $
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 #include "test/int.hh"
39 
40 #include <gecode/minimodel.hh>
41 
42 namespace Test { namespace Int {
43 
45  namespace MiniModelArithmetic {
46 
52  class Mult : public Test {
54  public:
56  Mult(const std::string& s, const Gecode::IntSet& d)
57  : Test("MiniModel::Mult::"+s,3,d) {
58  testfix = false;
59  }
61  virtual bool solution(const Assignment& x) const {
62  double d0 = static_cast<double>(x[0]);
63  double d1 = static_cast<double>(x[1]);
64  double d2 = static_cast<double>(x[2]);
65  return d0*d1 == d2;
66  }
68  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
69  using namespace Gecode;
70  rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], IPL_DOM);
71  }
72  };
73 
75  class Div : public Test {
76  public:
78  Div(const std::string& s, const Gecode::IntSet& d)
79  : Test("MiniModel::Div::"+s,3,d) {
80  testfix = false;
81  }
83  virtual bool solution(const Assignment& x) const {
84  return (x[1] != 0) && (x[0] / x[1] == x[2]);
85  }
87  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
88  using namespace Gecode;
89  rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], IPL_DOM);
90  }
91  };
92 
94  class Mod : public Test {
95  public:
97  Mod(const std::string& s, const Gecode::IntSet& d)
98  : Test("MiniModel::Mod::"+s,3,d) {
99  testfix = false;
100  }
102  virtual bool solution(const Assignment& x) const {
103  return (x[1] != 0) && (x[0] % x[1] == x[2]);
104  }
106  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
107  using namespace Gecode;
108  rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], IPL_DOM);
109  }
110  };
111 
113  class Plus : public Test {
114  public:
116  Plus(const std::string& s, const Gecode::IntSet& d)
117  : Test("MiniModel::Plus::"+s,3,d) {
118  testfix = false;
119  }
121  virtual bool solution(const Assignment& x) const {
122  double d0 = static_cast<double>(x[0]);
123  double d1 = static_cast<double>(x[1]);
124  double d2 = static_cast<double>(x[2]);
125  return ((d0+d1 >= Gecode::Int::Limits::min) &&
126  (d0+d1 <= Gecode::Int::Limits::max) &&
127  (d0+d1 == d2));
128  }
130  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
131  using namespace Gecode;
132  rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], IPL_DOM);
133  }
134  };
135 
137  class Minus : public Test {
138  public:
140  Minus(const std::string& s, const Gecode::IntSet& d)
141  : Test("MiniModel::Minus::"+s,3,d) {
142  testfix = false;
143  }
145  virtual bool solution(const Assignment& x) const {
146  double d0 = static_cast<double>(x[0]);
147  double d1 = static_cast<double>(x[1]);
148  double d2 = static_cast<double>(x[2]);
149  return ((d0-d1 >= Gecode::Int::Limits::min) &&
150  (d0-d1 <= Gecode::Int::Limits::max) &&
151  (d0-d1 == d2));
152  }
154  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
155  using namespace Gecode;
156  rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], IPL_DOM);
157  }
158  };
159 
161  class Sqr : public Test {
162  public:
164  Sqr(const std::string& s, const Gecode::IntSet& d)
165  : Test("MiniModel::Sqr::"+s,2,d) {
166  testfix = false;
167  }
169  virtual bool solution(const Assignment& x) const {
170  double d0 = static_cast<double>(x[0]);
171  double d1 = static_cast<double>(x[1]);
172  return d0*d0 == d1;
173  }
175  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
176  using namespace Gecode;
177  rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], IPL_DOM);
178  }
179  };
180 
182  class Sqrt : public Test {
183  public:
185  Sqrt(const std::string& s, const Gecode::IntSet& d)
186  : Test("MiniModel::Sqrt::"+s,2,d) {
187  testfix = false;
188  }
190  virtual bool solution(const Assignment& x) const {
191  double d0 = static_cast<double>(x[0]);
192  double d1 = static_cast<double>(x[1]);
193  return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
194  }
196  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
197  using namespace Gecode;
198  rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], IPL_DOM);
199  }
200  };
201 
203  class Abs : public Test {
204  public:
206  Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntPropLevel ipl)
207  : Test("MiniModel::Abs::"+str(ipl)+"::"+s,
208  2,d,false,ipl) {
209  testfix = false;
210  }
212  virtual bool solution(const Assignment& x) const {
213  double d0 = static_cast<double>(x[0]);
214  double d1 = static_cast<double>(x[1]);
215  return (d0<0.0 ? -d0 : d0) == d1;
216  }
218  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
219  using namespace Gecode;
220  rel(home, expr(home, abs(x[0]), ipl), IRT_EQ, x[1], IPL_DOM);
221  }
222  };
223 
225  class Min : public Test {
226  public:
228  Min(const std::string& s, const Gecode::IntSet& d)
229  : Test("MiniModel::Min::Bin::"+s,3,d) {
230  testfix = false;
231  }
233  virtual bool solution(const Assignment& x) const {
234  return std::min(x[0],x[1]) == x[2];
235  }
237  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
238  using namespace Gecode;
239  rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
240  }
241  };
242 
244  class Max : public Test {
245  public:
247  Max(const std::string& s, const Gecode::IntSet& d)
248  : Test("MiniModel::Max::Bin::"+s,3,d) {
249  testfix = false;
250  }
252  virtual bool solution(const Assignment& x) const {
253  return std::max(x[0],x[1]) == x[2];
254  }
256  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
257  using namespace Gecode;
258  rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
259  }
260  };
261 
263  class MinNary : public Test {
264  public:
266  MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
267  testfix = false;
268  }
270  virtual bool solution(const Assignment& x) const {
271  return std::min(std::min(x[0],x[1]), x[2]) == x[3];
272  }
274  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
275  using namespace Gecode;
276  IntVarArgs m(3);
277  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
278  rel(home, expr(home, min(m)), IRT_EQ, x[3], IPL_DOM);
279  }
280  };
281 
283  class MaxNary : public Test {
284  public:
286  MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
287  testfix = false;
288  }
290  virtual bool solution(const Assignment& x) const {
291  return std::max(std::max(x[0],x[1]), x[2]) == x[3];
292  }
294  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
295  using namespace Gecode;
296  IntVarArgs m(3);
297  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
298  rel(home, expr(home, max(m)), IRT_EQ, x[3], IPL_DOM);
299  }
300  };
301 
302  const int v1[7] = {
304  -1,0,1,
306  };
307  const int v2[9] = {
308  static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
309  -4,-2,-1,0,1,2,4,
310  static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
311  };
312 
313  Gecode::IntSet d1(v1,7);
314  Gecode::IntSet d2(v2,9);
315  Gecode::IntSet d3(-8,8);
316 
317  Mult mult_max("A",d1);
318  Mult mult_med("B",d2);
319  Mult mult_min("C",d3);
320 
321  Div div_max("A",d1);
322  Div div_med("B",d2);
323  Div div_min("C",d3);
324 
325  Mod mod_max("A",d1);
326  Mod mod_med("B",d2);
327  Mod mod_min("C",d3);
328 
329  Plus plus_max("A",d1);
330  Plus plus_med("B",d2);
331  Plus plus_min("C",d3);
332 
333  Minus minus_max("A",d1);
334  Minus minus_med("B",d2);
335  Minus minus_min("C",d3);
336 
337  Sqr sqr_max("A",d1);
338  Sqr sqr_med("B",d2);
339  Sqr sqr_min("C",d3);
340 
341  Sqrt sqrt_max("A",d1);
342  Sqrt sqrt_med("B",d2);
343  Sqrt sqrt_min("C",d3);
344 
345  Abs abs_bnd_max("A",d1,Gecode::IPL_BND);
346  Abs abs_bnd_med("B",d2,Gecode::IPL_BND);
347  Abs abs_bnd_min("C",d3,Gecode::IPL_BND);
348  Abs abs_dom_max("A",d1,Gecode::IPL_DOM);
349  Abs abs_dom_med("B",d2,Gecode::IPL_DOM);
350  Abs abs_dom_min("C",d3,Gecode::IPL_DOM);
351 
352  Min min_max("A",d1);
353  Min min_med("B",d2);
354  Min min_min("C",d3);
355 
356  Max max_max("A",d1);
357  Max max_med("B",d2);
358  Max max_min("C",d3);
359 
362 
364  }
365 
366 }}
367 
368 // STATISTICS: test-minimodel
Mod mod_min("C", d3)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Mult mult_max("A", d1)
Plus plus_med("B", d2)
Abs(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Abs abs_dom_min("C", d3, Gecode::IPL_DOM)
virtual bool solution(const Assignment &x) const
Test whether x is solution
Minus minus_med("B", d2)
Gecode::IntPropLevel ipl
Propagation level.
Definition: int.hh:238
Sqr(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Plus(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Plus plus_max("A", d1)
Passing integer variables.
Definition: int.hh:639
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Sqr sqr_med("B", d2)
const int v2[9]
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:99
Max max_med("B", d2)
Test for binary minimum constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
MinNary(void)
Create and register test.
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
Test for division constraint
Abs abs_dom_max("A", d1, Gecode::IPL_DOM)
Mod(const std::string &s, const Gecode::IntSet &d)
Create and register test.
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:955
Computation spaces.
Definition: core.hpp:1748
virtual bool solution(const Assignment &x) const
Test whether x is solution
Minus minus_max("A", d1)
Test for division constraint
virtual bool solution(const Assignment &x) const
Test whether x is solution
Integer variable array.
Definition: int.hh:744
Abs abs_bnd_min("C", d3, Gecode::IPL_BND)
Abs abs_dom_med("B", d2, Gecode::IPL_DOM)
Max max_max("A", d1)
Test for sqr constraint
Minus minus_min("C", d3)
Min min_max("A", d1)
Gecode toplevel namespace
bool testfix
Whether to perform fixpoint test.
Definition: int.hh:244
virtual bool solution(const Assignment &x) const
Test whether x is solution
Integer sets.
Definition: int.hh:174
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Test for subtraction constraint
Test for addition constraint
const int v1[7]
Test for n-ary minimmum constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:106
Sqr sqr_max("A", d1)
Abs abs_bnd_med("B", d2, Gecode::IPL_BND)
@ IPL_DOM
Domain propagation Preferences: prefer speed or memory.
Definition: int.hh:960
Mod mod_med("B", d2)
Minus(const std::string &s, const Gecode::IntSet &d)
Create and register test.
const int max
Largest allowed integer value.
Definition: int.hh:116
Test for multiplication constraint
Mult mult_min("C", d3)
Sqrt sqrt_med("B", d2)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
@ IPL_BND
Bounds propagation.
Definition: int.hh:959
Gecode::IntSet d1(v1, 7)
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:45
virtual bool solution(const Assignment &x) const
Test whether x is solution
Div div_med("B", d2)
Base class for assignments
Definition: int.hh:63
Div div_min("C", d3)
Test for absolute value constraint
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Div div_max("A", d1)
MaxNary(void)
Create and register test.
MaxNary max_nary
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Test for binary maximum constraint
Gecode::IntSet d2(v2, 9)
Max(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Mult mult_med("B", d2)
Abs abs_bnd_max("A", d1, Gecode::IPL_BND)
Gecode::IntSet d(v, 7)
Test for sqrt constraint
Max max_min("C", d3)
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
virtual bool solution(const Assignment &x) const
Test whether x is solution
General test support.
Definition: afc.cpp:43
const int min
Smallest allowed integer value.
Definition: int.hh:118
virtual bool solution(const Assignment &x) const
Test whether x is solution
Sqrt(const std::string &s, const Gecode::IntSet &d)
Create and register test.
@ IRT_EQ
Equality ( )
Definition: int.hh:907
Sqrt sqrt_min("C", d3)
Gecode::IntSet d3(-8, 8)
Sqrt sqrt_max("A", d1)
Sqr sqr_min("C", d3)
MinNary min_nary
virtual bool solution(const Assignment &x) const
Test whether x is solution
Mult(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Min(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Min min_med("B", d2)
Mod mod_max("A", d1)
Plus plus_min("C", d3)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
virtual bool solution(const Assignment &x) const
Test whether x is solution
const FloatNum max
Largest allowed float value.
Definition: float.hh:848
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Div(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Min min_min("C", d3)
Test for n-ary maximum constraint
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:212