Generated on Tue Mar 24 2020 14:04:04 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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include "test/int.hh"
35 
36 #include <gecode/minimodel.hh>
37 
38 namespace Test { namespace Int {
39 
41  namespace MiniModelArithmetic {
42 
48  class Mult : public Test {
50  public:
52  Mult(const std::string& s, const Gecode::IntSet& d)
53  : Test("MiniModel::Mult::"+s,3,d) {
54  testfix = false;
55  }
57  virtual bool solution(const Assignment& x) const {
58  double d0 = static_cast<double>(x[0]);
59  double d1 = static_cast<double>(x[1]);
60  double d2 = static_cast<double>(x[2]);
61  return d0*d1 == d2;
62  }
64  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
65  using namespace Gecode;
66  rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], IPL_DOM);
67  }
68  };
69 
71  class Div : public Test {
72  public:
74  Div(const std::string& s, const Gecode::IntSet& d)
75  : Test("MiniModel::Div::"+s,3,d) {
76  testfix = false;
77  }
79  virtual bool solution(const Assignment& x) const {
80  return (x[1] != 0) && (x[0] / x[1] == x[2]);
81  }
83  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
84  using namespace Gecode;
85  rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], IPL_DOM);
86  }
87  };
88 
90  class Mod : public Test {
91  public:
93  Mod(const std::string& s, const Gecode::IntSet& d)
94  : Test("MiniModel::Mod::"+s,3,d) {
95  testfix = false;
96  }
98  virtual bool solution(const Assignment& x) const {
99  return (x[1] != 0) && (x[0] % x[1] == x[2]);
100  }
102  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
103  using namespace Gecode;
104  rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], IPL_DOM);
105  }
106  };
107 
109  class Plus : public Test {
110  public:
112  Plus(const std::string& s, const Gecode::IntSet& d)
113  : Test("MiniModel::Plus::"+s,3,d) {
114  testfix = false;
115  }
117  virtual bool solution(const Assignment& x) const {
118  double d0 = static_cast<double>(x[0]);
119  double d1 = static_cast<double>(x[1]);
120  double d2 = static_cast<double>(x[2]);
121  return ((d0+d1 >= Gecode::Int::Limits::min) &&
122  (d0+d1 <= Gecode::Int::Limits::max) &&
123  (d0+d1 == d2));
124  }
126  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
127  using namespace Gecode;
128  rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], IPL_DOM);
129  }
130  };
131 
133  class Minus : public Test {
134  public:
136  Minus(const std::string& s, const Gecode::IntSet& d)
137  : Test("MiniModel::Minus::"+s,3,d) {
138  testfix = false;
139  }
141  virtual bool solution(const Assignment& x) const {
142  double d0 = static_cast<double>(x[0]);
143  double d1 = static_cast<double>(x[1]);
144  double d2 = static_cast<double>(x[2]);
145  return ((d0-d1 >= Gecode::Int::Limits::min) &&
146  (d0-d1 <= Gecode::Int::Limits::max) &&
147  (d0-d1 == d2));
148  }
150  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
151  using namespace Gecode;
152  rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], IPL_DOM);
153  }
154  };
155 
157  class Sqr : public Test {
158  public:
160  Sqr(const std::string& s, const Gecode::IntSet& d)
161  : Test("MiniModel::Sqr::"+s,2,d) {
162  testfix = false;
163  }
165  virtual bool solution(const Assignment& x) const {
166  double d0 = static_cast<double>(x[0]);
167  double d1 = static_cast<double>(x[1]);
168  return d0*d0 == d1;
169  }
171  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
172  using namespace Gecode;
173  rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], IPL_DOM);
174  }
175  };
176 
178  class Sqrt : public Test {
179  public:
181  Sqrt(const std::string& s, const Gecode::IntSet& d)
182  : Test("MiniModel::Sqrt::"+s,2,d) {
183  testfix = false;
184  }
186  virtual bool solution(const Assignment& x) const {
187  double d0 = static_cast<double>(x[0]);
188  double d1 = static_cast<double>(x[1]);
189  return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
190  }
192  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
193  using namespace Gecode;
194  rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], IPL_DOM);
195  }
196  };
197 
199  class Abs : public Test {
200  public:
202  Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntPropLevel ipl)
203  : Test("MiniModel::Abs::"+str(ipl)+"::"+s,
204  2,d,false,ipl) {
205  testfix = false;
206  }
208  virtual bool solution(const Assignment& x) const {
209  double d0 = static_cast<double>(x[0]);
210  double d1 = static_cast<double>(x[1]);
211  return (d0<0.0 ? -d0 : d0) == d1;
212  }
214  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
215  using namespace Gecode;
216  rel(home, expr(home, abs(x[0]), ipl), IRT_EQ, x[1], IPL_DOM);
217  }
218  };
219 
221  class Min : public Test {
222  public:
224  Min(const std::string& s, const Gecode::IntSet& d)
225  : Test("MiniModel::Min::Bin::"+s,3,d) {
226  testfix = false;
227  }
229  virtual bool solution(const Assignment& x) const {
230  return std::min(x[0],x[1]) == x[2];
231  }
233  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
234  using namespace Gecode;
235  rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
236  }
237  };
238 
240  class Max : public Test {
241  public:
243  Max(const std::string& s, const Gecode::IntSet& d)
244  : Test("MiniModel::Max::Bin::"+s,3,d) {
245  testfix = false;
246  }
248  virtual bool solution(const Assignment& x) const {
249  return std::max(x[0],x[1]) == x[2];
250  }
252  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
253  using namespace Gecode;
254  rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
255  }
256  };
257 
259  class MinNary : public Test {
260  public:
262  MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
263  testfix = false;
264  }
266  virtual bool solution(const Assignment& x) const {
267  return std::min(std::min(x[0],x[1]), x[2]) == x[3];
268  }
270  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
271  using namespace Gecode;
272  IntVarArgs m(3);
273  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
274  rel(home, expr(home, min(m)), IRT_EQ, x[3], IPL_DOM);
275  }
276  };
277 
279  class MaxNary : public Test {
280  public:
282  MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
283  testfix = false;
284  }
286  virtual bool solution(const Assignment& x) const {
287  return std::max(std::max(x[0],x[1]), x[2]) == x[3];
288  }
290  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
291  using namespace Gecode;
292  IntVarArgs m(3);
293  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
294  rel(home, expr(home, max(m)), IRT_EQ, x[3], IPL_DOM);
295  }
296  };
297 
298  const int v1[7] = {
300  -1,0,1,
302  };
303  const int v2[9] = {
304  static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
305  -4,-2,-1,0,1,2,4,
306  static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
307  };
308 
309  Gecode::IntSet d1(v1,7);
310  Gecode::IntSet d2(v2,9);
311  Gecode::IntSet d3(-8,8);
312 
313  Mult mult_max("A",d1);
314  Mult mult_med("B",d2);
315  Mult mult_min("C",d3);
316 
317  Div div_max("A",d1);
318  Div div_med("B",d2);
319  Div div_min("C",d3);
320 
321  Mod mod_max("A",d1);
322  Mod mod_med("B",d2);
323  Mod mod_min("C",d3);
324 
325  Plus plus_max("A",d1);
326  Plus plus_med("B",d2);
327  Plus plus_min("C",d3);
328 
329  Minus minus_max("A",d1);
330  Minus minus_med("B",d2);
331  Minus minus_min("C",d3);
332 
333  Sqr sqr_max("A",d1);
334  Sqr sqr_med("B",d2);
335  Sqr sqr_min("C",d3);
336 
337  Sqrt sqrt_max("A",d1);
338  Sqrt sqrt_med("B",d2);
339  Sqrt sqrt_min("C",d3);
340 
341  Abs abs_bnd_max("A",d1,Gecode::IPL_BND);
342  Abs abs_bnd_med("B",d2,Gecode::IPL_BND);
343  Abs abs_bnd_min("C",d3,Gecode::IPL_BND);
344  Abs abs_dom_max("A",d1,Gecode::IPL_DOM);
345  Abs abs_dom_med("B",d2,Gecode::IPL_DOM);
346  Abs abs_dom_min("C",d3,Gecode::IPL_DOM);
347 
348  Min min_max("A",d1);
349  Min min_med("B",d2);
350  Min min_min("C",d3);
351 
352  Max max_max("A",d1);
353  Max max_med("B",d2);
354  Max max_min("C",d3);
355 
358 
360  }
361 
362 }}
363 
364 // 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:234
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:656
Sqr sqr_med("B", d2)
const int v2[9]
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:95
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:846
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:974
Computation spaces.
Definition: core.hpp:1742
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:763
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:240
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:249
Test for subtraction constraint
Test for addition constraint
BoolVar expr(Home home, const BoolExpr &e, const IntPropLevels &ipls)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:629
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:102
Sqr sqr_max("A", d1)
Abs abs_bnd_med("B", d2, Gecode::IPL_BND)
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
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:978
Gecode::IntSet d1(v1, 7)
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:41
virtual bool solution(const Assignment &x) const
Test whether x is solution
Div div_med("B", d2)
Base class for assignments
Definition: int.hh:59
Div div_min("C", d3)
Test for absolute value constraint
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
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:39
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:926
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:844
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:209