Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
38namespace Test { namespace Int {
39
41 namespace MiniModelArithmetic {
42
49 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) &&
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) &&
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
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
312
316
320
324
328
332
336
340
347
351
355
358
360 }
361
362}}
363
364// STATISTICS: test-minimodel
Node * x
Pointer to corresponding Boolean expression node.
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Computation spaces.
Definition core.hpp:1742
Base class for assignments
Definition int.hh:59
Test for absolute value constraint
Abs(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
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
Test for division 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
Div(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Test for n-ary maximum 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
MaxNary(void)
Create and register test.
Test for binary maximum constraint
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.
Max(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Test for n-ary minimmum constraint
virtual bool solution(const Assignment &x) const
Test whether x is solution
MinNary(void)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Test for binary minimum constraint
Min(const std::string &s, const Gecode::IntSet &d)
Create and register test.
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.
Test for subtraction constraint
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.
Minus(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Test for division 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
Mod(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Test for multiplication constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Mult(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for addition constraint
Plus(const std::string &s, const Gecode::IntSet &d)
Create and register test.
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 void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Sqr(const std::string &s, const Gecode::IntSet &d)
Create and register test.
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.
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.
bool testfix
Whether to perform fixpoint test.
Definition int.hh:240
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:234
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
@ IPL_BND
Bounds propagation.
Definition int.hh:978
const int min
Smallest allowed integer value.
Definition int.hh:118
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode toplevel namespace
Abs abs_dom_min("C", d3, Gecode::IPL_DOM)
Abs abs_bnd_max("A", d1, Gecode::IPL_BND)
Abs abs_dom_med("B", d2, Gecode::IPL_DOM)
Abs abs_bnd_med("B", d2, Gecode::IPL_BND)
Abs abs_bnd_min("C", d3, Gecode::IPL_BND)
Gecode::IntSet d2(v2, 9)
Gecode::IntSet d1(v1, 7)
Abs abs_dom_max("A", d1, Gecode::IPL_DOM)
Gecode::IntSet d3(-8, 8)
General test support.
Definition afc.cpp:39