Generated on for Gecode by doxygen 1.17.0
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.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2005
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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 <cmath>
37#include <algorithm>
38#include <memory>
39
40#include <gecode/minimodel.hh>
41
42namespace Test { namespace Int {
43
45 namespace Arithmetic {
46
52
53 int gcd_value(int a, int b) {
54 a = (a < 0) ? -a : a;
55 b = (b < 0) ? -b : b;
56 while (b != 0) {
57 int t = a % b;
58 a = b; b = t;
59 }
60 return a;
61 }
62
64 // Reposting comparisons below are disabled where bounds subscriptions
65 // deliberately miss interior removals used by algebraic status tests.
66 // NumberTheoryLifecycle checks activation, cloning, and rescheduling
67 // separately; NumberTheorySparseBounds checks own-update fixpoints.
68 class GcdXYZ : public Test {
69 public:
71 GcdXYZ(const std::string& s, const Gecode::IntSet& d,
72 Gecode::IntPropLevel ipl, bool r=true)
73 : Test("Arithmetic::Gcd::XYZ::"+str(ipl)+"::"+s+(r ? "" : "::Plain"),3,d,r,ipl) {
75 }
76
77 virtual bool solution(const Assignment& x) const {
78 return gcd_value(x[0],x[1]) == x[2];
79 }
80
81 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
82 Gecode::gcd(home,x[0],x[1],x[2],ipl);
83 }
84
85 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
87 Gecode::gcd(home,x[0],x[1],x[2],r,ipl);
88 }
89 };
90
92 class GcdXXY : public Test {
93 public:
95 GcdXXY(const std::string& s, const Gecode::IntSet& d,
96 Gecode::IntPropLevel ipl, bool r=true)
97 : Test("Arithmetic::Gcd::XXY::"+str(ipl)+"::"+s+(r ? "" : "::Plain"),2,d,r,ipl) {
99 }
100
101 virtual bool solution(const Assignment& x) const {
102 return gcd_value(x[0],x[0]) == x[1];
103 }
104
105 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
106 Gecode::gcd(home,x[0],x[0],x[1],ipl);
107 }
108
109 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
111 Gecode::gcd(home,x[0],x[0],x[1],r,ipl);
112 }
113 };
114
116 class GcdXYX : public Test {
118 bool swap;
119 public:
121 GcdXYX(const std::string& s, const Gecode::IntSet& d,
122 Gecode::IntPropLevel ipl, bool r=true, bool sw=false)
123 : Test("Arithmetic::Gcd::XYX::"+str(ipl)+"::"+s+
124 (r ? "" : "::Plain")+(sw ? "::Swap" : ""),2,d,r,ipl),
125 swap(sw) {
127 }
128
129 virtual bool solution(const Assignment& x) const {
130 return gcd_value(x[0],x[1]) == x[0];
131 }
132
133 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
134 if (swap)
135 Gecode::gcd(home,x[1],x[0],x[0],ipl);
136 else
137 Gecode::gcd(home,x[0],x[1],x[0],ipl);
138 }
139
140 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
142 if (swap)
143 Gecode::gcd(home,x[1],x[0],x[0],r,ipl);
144 else
145 Gecode::gcd(home,x[0],x[1],x[0],r,ipl);
146 }
147 };
148
150 class GcdXXX : public Test {
151 public:
153 GcdXXX(const std::string& s, const Gecode::IntSet& d,
155 : Test("Arithmetic::Gcd::XXX::"+str(ipl)+"::"+s,1,d,true,ipl) {
157 }
158
159 virtual bool solution(const Assignment& x) const {
160 return gcd_value(x[0],x[0]) == x[0];
161 }
162
163 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
164 Gecode::gcd(home,x[0],x[0],x[0],ipl);
165 }
166
167 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
169 Gecode::gcd(home,x[0],x[0],x[0],r,ipl);
170 }
171 };
172
174 // With divisor zero, removing interior zero from the dividend can
175 // strengthen a reposted status test without a bounds wakeup.
176 class DividesXY : public Test {
177 public:
179 DividesXY(const std::string& s, const Gecode::IntSet& d,
181 : Test("Arithmetic::Divides::XY::"+str(ipl)+"::"+s,
182 2,d,true,ipl) { contest=CTL_NONE; testfix=false; }
183
184 virtual bool solution(const Assignment& x) const {
185 return (x[0] == 0) ? (x[1] == 0) : (x[1] % x[0] == 0);
186 }
187
188 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
189 Gecode::BoolVar b(home,1,1);
190 Gecode::divides(home,x[0],x[1],Gecode::Reify(b),ipl);
191 }
192
193 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
195 Gecode::divides(home,x[0],x[1],r,ipl);
196 }
197 };
198
200 class DividesXX : public Test {
201 public:
203 DividesXX(const std::string& s, const Gecode::IntSet& d,
205 : Test("Arithmetic::Divides::XX::"+str(ipl)+"::"+s,
206 1,d,true,ipl) { contest=CTL_NONE; }
207
208 virtual bool solution(const Assignment&) const {
209 return true;
210 }
211
212 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
213 Gecode::BoolVar b(home,1,1);
214 Gecode::divides(home,x[0],x[0],Gecode::Reify(b),ipl);
215 }
216
217 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
219 Gecode::divides(home,x[0],x[0],r,ipl);
220 }
221 };
222
224 bool arithmetic_failed(const std::string& what,
226 if (opt.log)
227 olog << what << ": " << x << std::endl;
228 return false;
229 }
230
233 class TestSpace : public Gecode::Space {
234 public:
235 virtual Gecode::Space* copy(void) { return nullptr; }
236 };
237 int kind;
238 public:
240 : ::Test::Base("Int::Arithmetic::NumberTheoryAlgebraic::"+str(k)),
241 kind(k) {}
242 virtual bool run(void) {
243 using namespace Gecode;
244 TestSpace home;
245 if (kind <= 2) {
246 IntVar a(home,6,6);
247 IntVar g(home,3,3);
248 IntVar x(home,kind == 1 ? -12 : 6,kind == 1 ? -6 : 12);
249 if (kind == 2) dom(home,x,IntSet({6,9,12}));
250 gcd(home,a,x,g);
251 if ((home.status() == SS_FAILED) || !x.assigned() ||
252 (x.val() != (kind == 1 ? -9 : 9)))
253 return arithmetic_failed("coprime quotient: a, x, g",
254 IntVarArgs() << a << x << g);
255 } else if (kind == 3) {
256 IntVar a(home,12,12);
257 IntVar x(home,1,24);
258 IntVar g(home,7,12);
259 gcd(home,a,x,g);
260 if ((home.status() == SS_FAILED) || !g.assigned() || (g.val()!=12))
261 return arithmetic_failed("divisor quotient band: a, x, g",
262 IntVarArgs() << a << x << g);
263 } else if (kind <= 5) {
264 IntVar d(home,kind == 4 ? 6 : -10,kind == 4 ? 10 : -6);
265 IntVar x(home,1,5); BoolVar b(home,1,1);
266 divides(home,d,x,Reify(b));
267 if (home.status() != SS_FAILED)
268 return arithmetic_failed("divisor magnitude: d, x",
269 IntVarArgs() << d << x);
270 } else if (kind == 6) {
271 IntVar d(home,0,0);
272 IntVar x(home,-5,5);
273 BoolVar b(home,0,0);
274 divides(home,d,x,Reify(b));
275 if ((home.status() == SS_FAILED) || x.in(0))
276 return arithmetic_failed("negated zero divisor: d, x",
277 IntVarArgs() << d << x);
278 } else if (kind == 7) {
279 IntVar c(home,6,6);
280 IntVar x(home,1,100);
281 IntVar z(home,1,100);
282 IntVar y(home,1,1);
283 product_mod(home,IntVarArgs({c,x,z}),12,y);
284 if (home.status() != SS_FAILED)
285 return arithmetic_failed("coefficient gcd failure: c, x, z, y",
286 IntVarArgs() << c << x << z << y);
287 } else if (kind == 8) {
288 IntVar c(home,-6,-6);
289 IntVar x(home,-100,100);
290 IntVar z(home,-100,100);
291 IntVar y(home,4,9);
292 product_mod(home,IntVarArgs({c,x,z}),12,y);
293 if ((home.status() == SS_FAILED) || !y.assigned() || (y.val()!=6))
294 return arithmetic_failed("coefficient result bounds: y",
295 IntVarArgs() << y);
296 } else if (kind == 9) {
297 IntVar a(home,2,2);
298 IntVar b(home,3,3);
299 IntVar x(home,-100,100);
300 IntVar y(home,0,5);
301 product_mod(home,IntVarArgs({a,x,b}),6,y);
302 if ((home.status() == SS_FAILED) || !y.assigned() || (y.val()!=0))
303 return arithmetic_failed("collective zero coefficient: y",
304 IntVarArgs() << y);
305 } else if (kind == 10) {
306 IntVar c(home,6,6);
307 IntVar x(home,1,100);
308 IntVar z(home,1,100);
309 IntVar y(home,IntSet({1,5,7,11}));
310 product_mod(home,IntVarArgs({c,x,z}),12,y);
311 if (home.status() != SS_FAILED)
312 return arithmetic_failed("sparse result multiples: y",
313 IntVarArgs() << y);
314 } else if (kind == 11) {
315 const int hi=Gecode::Int::Limits::max;
316 IntVar c(home,6,6);
317 IntVar x(home,1,hi);
318 IntVar z(home,1,hi);
319 IntVar y(home,1,1);
320 product_mod(home,IntVarArgs({c,x,z}),hi,y);
321 if (home.status() != SS_FAILED)
322 return arithmetic_failed("large coefficient gcd failure: y",
323 IntVarArgs() << y);
324 } else {
325 const int hi=Gecode::Int::Limits::max;
326 IntVar a(home,-hi,-hi);
327 IntVar x(home,hi-1,hi);
328 IntVar g(home,1,1);
329 gcd(home,a,x,g);
330 if ((home.status() == SS_FAILED) || !x.assigned() || (x.val()!=hi-1))
331 return arithmetic_failed("limit coprimality: a, x, g",
332 IntVarArgs() << a << x << g);
333 }
334 return true;
335 }
336 };
337
340 class TestSpace : public Gecode::Space {
341 public:
342 virtual Gecode::Space* copy(void) { return nullptr; }
343 };
344 public:
346 : ::Test::Base("Int::Arithmetic::NumberTheorySparseBounds") {}
347 virtual bool run(void) {
348 using namespace Gecode;
349 for (int division=0; division<2; division++) {
350 TestSpace home;
351 IntVar x(home,IntSet({1,3,5,6}));
352 IntVar y(home,10,10);
353 IntVar g(home,1,2);
354 BoolVar b(home,1,1);
355 if (division)
356 divides(home,g,x,Reify(b));
357 else
358 gcd(home,x,y,g);
359 if (home.status() == SS_FAILED)
360 return arithmetic_failed
361 ("initial propagation: x, y, g",
362 Gecode::IntVarArgs() << x << y << g);
363 rel(home,g,IRT_EQ,2);
364 if ((home.status() == SS_FAILED) || !x.assigned() || (x.val()!=6))
365 return arithmetic_failed
366 ("assigned divisor: x, y, g",
367 Gecode::IntVarArgs() << x << y << g);
368 }
369 return true;
370 }
371 };
372
375 class TestSpace : public Gecode::Space {
376 public:
382 TestSpace(int kind)
383 : x(*this,Gecode::IntSet({1,3,5,6})),
384 c(*this,kind == 0 ? 10 : 2,kind == 0 ? 10 : 2),
385 y(*this,kind == 0 ? 2 : (kind == 4 ? 1 : 12),
386 kind == 0 ? 2 : (kind == 4 ? 1 : 12)),
387 m(*this,kind == 4 ? 5 : 13,kind == 4 ? 7 : 17), b(*this,0,1) {
388 using namespace Gecode;
389 switch (kind) {
390 case 0: gcd(*this,x,c,y,Reify(b)); break;
391 case 1: divides(*this,c,x,Reify(b)); break;
392 case 2: product(*this,IntVarArgs({x,c}),y,Reify(b)); break;
393 case 3: product_mod(*this,IntVarArgs({x,c}),13,y,Reify(b)); break;
394 case 4: product_mod(*this,IntVarArgs({x,c}),m,y,Reify(b)); break;
395 default: GECODE_NEVER;
396 }
397 }
398 TestSpace(TestSpace& s) : Gecode::Space(s) {
399 x.update(*this,s.x); c.update(*this,s.c); y.update(*this,s.y);
400 m.update(*this,s.m); b.update(*this,s.b);
401 }
402 virtual Gecode::Space* copy(void) { return new TestSpace(*this); }
403 };
404 public:
406 : ::Test::Base("Int::Arithmetic::NumberTheoryLifecycle") {}
407 virtual bool run(void) {
408 using namespace Gecode;
409 for (int kind=0; kind<5; kind++) {
410 TestSpace home(kind);
411 if (home.status() == SS_FAILED)
412 return arithmetic_failed
413 ("initial propagation: home.x, home.c, home.y, home.m",
414 Gecode::IntVarArgs() << home.x << home.c << home.y << home.m);
415 std::unique_ptr<TestSpace> clone
416 (static_cast<TestSpace*>(home.clone()));
417 PropagatorGroup::all.disable(*clone);
418 rel(*clone,clone->b,IRT_EQ,1);
419 PropagatorGroup::all.enable(*clone);
420 if ((clone->status() == SS_FAILED) ||
421 ((kind != 4) && (!clone->x.assigned() || (clone->x.val() != 6))))
422 return arithmetic_failed
423 ("activation after cloning, kind "+str(kind)+": x, c, y, m",
424 Gecode::IntVarArgs() << clone->x << clone->c
425 << clone->y << clone->m);
426 // Clone the activated actor before fixing a variable modulus.
427 std::unique_ptr<TestSpace> child
428 (static_cast<TestSpace*>(clone->clone()));
429 rel(*child,child->m,IRT_EQ,kind == 4 ? 5 : 13);
430 if ((child->status() == SS_FAILED) || !child->x.assigned() ||
431 (child->x.val() != (kind == 4 ? 3 : 6)) ||
432 home.b.assigned() || (home.x.size() != 4) || clone->m.assigned())
433 return arithmetic_failed
434 ("fixed modulus in child, kind "+str(kind)+": child x, m; parent x, m",
435 Gecode::IntVarArgs() << child->x << child->m
436 << clone->x << clone->m);
437 }
438 return true;
439 }
440 };
441
444 class TestSpace : public Gecode::Space {
445 public:
446 virtual Gecode::Space* copy(void) { return nullptr; }
447 };
448 public:
450 : ::Test::Base("Int::Arithmetic::NumberTheoryIdentities") {}
451 virtual bool run(void) {
452 using namespace Gecode;
453 for (ReifyMode rm : {RM_EQV,RM_IMP,RM_PMI})
454 for (int kind=0; kind<4; kind++) {
455 TestSpace home;
456 IntVar x(home,-7,7);
457 IntVar zero(home,0,0);
458 IntVar m(home,0,7);
459 IntVar one(home,1,1);
460 BoolVar b(home,0,1);
461 if (kind == 0) product(home,IntVarArgs({zero,x}),zero,Reify(b,rm));
462 if (kind == 1) product(home,IntVarArgs({x}),x,Reify(b,rm));
463 if (kind == 2) product_mod(home,IntVarArgs({zero,x}),m,zero,Reify(b,rm));
464 if (kind == 3) product_mod(home,IntVarArgs({x}),one,zero,Reify(b,rm));
465 if (home.status() == SS_FAILED)
466 return arithmetic_failed
467 ("initial identity: x, zero, m, one",
468 Gecode::IntVarArgs() << x << zero << m << one);
469 if (kind == 2) {
470 // Zero residue alone does not establish a positive modulus.
471 if (b.assigned() || (m.min() != 0))
472 return arithmetic_failed
473 ("modulus positivity unknown: x, zero, m, one",
474 Gecode::IntVarArgs() << x << zero << m << one);
475 rel(home,m,IRT_GQ,1);
476 if (home.status() == SS_FAILED)
477 return arithmetic_failed
478 ("positive modulus: x, zero, m, one",
479 Gecode::IntVarArgs() << x << zero << m << one);
480 }
481 if ((rm == RM_IMP) ? b.assigned() :
482 (!b.assigned() || (b.val() != 1)))
483 return arithmetic_failed
484 ("reified truth: x, zero, m, one",
485 Gecode::IntVarArgs() << x << zero << m << one);
486 if ((x.min() != -7) || (x.max() != 7))
487 return arithmetic_failed
488 ("unconstrained factor: x, zero, m, one",
489 Gecode::IntVarArgs() << x << zero << m << one);
490 }
491 return true;
492 }
493 };
494
496 // Observe rewriting after the modulus changes at an initial fixpoint.
498 class TestSpace : public Gecode::Space {
499 public:
500 virtual Gecode::Space* copy(void) { return nullptr; }
501 };
502 public:
504 : ::Test::Base("Int::Arithmetic::NumberTheoryLimits") {}
505 virtual bool run(void) {
506 using namespace Gecode;
507 const int hi=Gecode::Int::Limits::max;
508 // hi-1 is -1 modulo hi. Its square is 1 and its cube is -1;
509 // negating one factor reverses these residues. The cube exceeds
510 // signed 64-bit range, but its residue remains exactly known.
511 for (int n=2; n<=3; n++)
512 for (int negative=0; negative<=1; negative++)
513 for (int variable=0; variable<=1; variable++) {
514 const int expected=((n+negative)%2 == 0) ? 1 : hi-1;
515 {
516 TestSpace home;
517 IntVarArgs x(home,n,hi-1,hi-1);
518 if (negative) x[0]=IntVar(home,1-hi,1-hi);
519 IntVar m(home,hi-1,hi);
520 IntVar y(home,0,hi-1);
521 if (variable) product_mod(home,x,m,y);
522 else product_mod(home,x,hi,y);
523 if (home.status() == SS_FAILED)
524 return arithmetic_failed
525 ("large product before rewrite: x, m, y",
526 Gecode::IntVarArgs() << x << m << y);
527 // Force variable-modulus rewriting after initial propagation.
528 rel(home,m,IRT_EQ,hi);
529 if ((home.status() == SS_FAILED) || !y.assigned() ||
530 (y.val() != expected))
531 return arithmetic_failed
532 ("large product residue: x, m, y",
533 Gecode::IntVarArgs() << x << m << y);
534 }
535 }
536 return true;
537 }
538 };
539
541 class ProductModLimits : public Test {
542 int n;
543 bool negative;
544 bool variable;
545 public:
546 ProductModLimits(int n0, bool neg, bool var)
547 : Test("Arithmetic::ProductMod::Limits::"+str(n0)+"::"+
548 str(neg)+"::"+str(var),1,
549 Gecode::IntSet({0,1,Gecode::Int::Limits::max-1}),true),
550 n(n0), negative(neg), variable(var) {
551 // Reified modular products do not promise a propagation fixpoint.
552 testfix = false;
553 }
554 virtual bool solution(const Assignment& x) const {
555 return x[0] == (((n+negative)%2 == 0) ?
557 }
558 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
559 using namespace Gecode;
560 const int hi = Gecode::Int::Limits::max;
561 IntVarArgs factors(home,n,hi-1,hi-1);
562 if (negative)
563 factors[0] = IntVar(home,1-hi,1-hi);
564 if (variable) {
565 IntVar modulus(home,hi,hi);
566 product_mod(home,factors,modulus,x[0]);
567 } else {
568 product_mod(home,factors,hi,x[0]);
569 }
570 }
571 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
573 using namespace Gecode;
574 const int hi = Gecode::Int::Limits::max;
575 IntVarArgs factors(home,n,hi-1,hi-1);
576 if (negative)
577 factors[0] = IntVar(home,1-hi,1-hi);
578 if (variable) {
579 IntVar modulus(home,hi,hi);
580 product_mod(home,factors,modulus,x[0],r);
581 } else {
582 product_mod(home,factors,hi,x[0],r);
583 }
584 }
585 };
586
588 bool product_value(const Assignment& x, int n, int& product) {
589 for (int i=0; i<n; i++)
590 if (x[i] == 0) {
591 product = 0;
592 return true;
593 }
594 long long int p = 1;
595 for (int i=0; i<n; i++) {
597 (p,static_cast<long long int>(x[i])))
598 return false;
599 p *= static_cast<long long int>(x[i]);
600 }
602 return false;
603 product = static_cast<int>(p);
604 return true;
605 }
606
608 // Zero membership and assigned-product membership can change without
609 // a bounds event, including when factors or the result are aliased.
610 class ProductXYZR : public Test {
611 public:
612 ProductXYZR(const std::string& s, const Gecode::IntSet& d,
614 : Test("Arithmetic::Product::XYZR::"+str(ipl)+"::"+s,
615 4,d,true,ipl) { contest = CTL_NONE; testfix=false; }
616 virtual bool solution(const Assignment& x) const {
617 int p;
618 return product_value(x,3,p) && (p == x[3]);
619 }
620 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
621 Gecode::IntVarArgs f({x[0],x[1],x[2]});
622 Gecode::product(home,f,x[3],ipl);
623 }
624 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
626 Gecode::IntVarArgs f({x[0],x[1],x[2]});
627 Gecode::product(home,f,x[3],r,ipl);
628 }
629 };
630
632 class ProductEmpty : public Test {
633 public:
634 ProductEmpty(const std::string& s, const Gecode::IntSet& d,
636 : Test("Arithmetic::Product::Empty::"+str(ipl)+"::"+s,
637 1,d,true,ipl) {
639 }
640 virtual bool solution(const Assignment& x) const {
641 return x[0] == 1;
642 }
643 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
645 }
646 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
649 }
650 };
651
653 class ProductSingleton : public Test {
654 public:
655 ProductSingleton(const std::string& s, const Gecode::IntSet& d,
657 : Test("Arithmetic::Product::Singleton::"+str(ipl)+"::"+s,
658 2,d,true,ipl) { contest = CTL_NONE; }
659 virtual bool solution(const Assignment& x) const {
660 return x[0] == x[1];
661 }
662 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
663 Gecode::product(home,Gecode::IntVarArgs({x[0]}),x[1],ipl);
664 }
665 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
667 Gecode::product(home,Gecode::IntVarArgs({x[0]}),x[1],r,ipl);
668 }
669 };
670
672 class ProductXXYAlias : public Test {
673 public:
674 ProductXXYAlias(const std::string& s, const Gecode::IntSet& d,
676 : Test("Arithmetic::Product::XXYAlias::"+str(ipl)+"::"+s,
677 2,d,true,ipl) { contest = CTL_NONE; testfix=false; }
678 virtual bool solution(const Assignment& x) const {
679 long long int p = static_cast<long long int>(x[0]) * x[0] * x[1];
680 return (p >= Gecode::Int::Limits::min) &&
681 (p <= Gecode::Int::Limits::max) && (p == x[1]);
682 }
683 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
684 Gecode::product(home,Gecode::IntVarArgs({x[0],x[0],x[1]}),x[1],ipl);
685 }
686 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
688 Gecode::product(home,Gecode::IntVarArgs({x[0],x[0],x[1]}),x[1],r,ipl);
689 }
690 };
691
693 class ProductInterleaved : public Test {
694 int kind;
695 Gecode::IntVarArgs factors(const Gecode::IntVarArray& x) const {
696 return Gecode::IntVarArgs({x[0],x[1],x[0],x[2],x[1],x[0]});
697 }
698 public:
700 : Test("Arithmetic::Product::Interleaved::"+str(k),k == 2 ? 5 : 4,
701 Gecode::IntSet(-2,3),true,Gecode::IPL_BND), kind(k) {
702 contest=CTL_NONE; testfix=false;
703 }
704 virtual bool solution(const Assignment& x) const {
705 const int p=x[0]*x[0]*x[0]*x[1]*x[1]*x[2];
706 if (kind == 0) return p == x[3];
707 const int m=kind == 1 ? 7 : x[4];
708 return (m > 0) && (((p % m)+m) % m == x[3]);
709 }
710 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
711 if (kind == 0) Gecode::product(home,factors(x),x[3],ipl);
712 else if (kind == 1)
713 Gecode::product_mod(home,factors(x),7,x[3],ipl);
714 else Gecode::product_mod(home,factors(x),x[4],x[3],ipl);
715 }
716 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
718 if (kind == 0) Gecode::product(home,factors(x),x[3],r,ipl);
719 else if (kind == 1)
720 Gecode::product_mod(home,factors(x),7,x[3],r,ipl);
721 else Gecode::product_mod(home,factors(x),x[4],x[3],r,ipl);
722 }
723 };
724
727 protected:
728 class TestSpace : public Gecode::Space {
729 public:
730 virtual Gecode::Space* copy(void) { return nullptr; }
731 };
732 public:
734 : ::Test::Base("Int::Arithmetic::Product::BoundsLarge") {}
735 virtual bool run(void) {
736 using namespace Gecode;
737 {
738 TestSpace home;
739 IntVarArgs x(home,3,2,100);
741 BoolVar b(home,1,1);
742 product(home,x,y,Reify(b));
743 if ((home.status() == SS_FAILED) ||
744 (y.min() != 8) || (y.max() != 1000000))
745 return arithmetic_failed
746 ("forward bounds: x, y",
747 Gecode::IntVarArgs() << x << y);
748 }
749 {
750 TestSpace home;
751 IntVarArgs x(home,3,10,100);
752 IntVar y(home,1000,1000);
753 product(home,x,y);
754 if (home.status() == SS_FAILED)
755 return arithmetic_failed
756 ("inverse bounds failure: x, y",
757 Gecode::IntVarArgs() << x << y);
758 for (int i=0; i<x.size(); i++)
759 if (!x[i].assigned() || (x[i].val() != 10))
760 return arithmetic_failed
761 ("inverse factor assignment: x, y",
762 Gecode::IntVarArgs() << x << y);
763 }
764 return true;
765 }
766 };
767
770 protected:
771 class TestSpace : public Gecode::Space {
772 public:
773 virtual Gecode::Space* copy(void) { return nullptr; }
774 };
775 class CloneSpace : public Gecode::Space {
776 public:
781 : x(*this,-2,2), q(*this,2,2), y(*this,-10,10) {
783 }
785 x.update(*this,s.x); q.update(*this,s.q); y.update(*this,s.y);
786 }
787 virtual Gecode::Space* copy(void) { return new CloneSpace(*this); }
788 };
789 public:
791 : ::Test::Base("Int::Arithmetic::Product::SimplifySign") {}
792 virtual bool run(void) {
793 using namespace Gecode;
794 {
795 TestSpace home;
796 IntVar z(home,0,0);
797 IntVar x(home,-100,100);
798 IntVar y(home,-100,100);
799 product(home,IntVarArgs({x,z}),y);
800 if ((home.status() == SS_FAILED) || !y.assigned() || (y.val()!=0))
801 return arithmetic_failed
802 ("zero factor: z, x, y",
803 Gecode::IntVarArgs() << z << x << y);
804 }
805 {
806 TestSpace home;
807 IntVar one(home,1,1);
808 IntVar minus(home,-1,-1);
809 IntVar x(home,2,5);
810 IntVar y(home,-100,100);
811 product(home,IntVarArgs({one,minus,minus,x}),y);
812 if ((home.status() == SS_FAILED) || (y.min()!=2) || (y.max()!=5))
813 return arithmetic_failed
814 ("paired negative units: one, minus, x, y",
815 Gecode::IntVarArgs() << one << minus << x << y);
816 }
817 {
818 TestSpace home;
819 IntVar minus(home,-1,-1);
820 IntVar x(home,2,5);
821 IntVar y(home,-100,100);
822 product(home,IntVarArgs({minus,x}),y);
823 if ((home.status() == SS_FAILED) || (y.min()!=-5) || (y.max()!=-2))
824 return arithmetic_failed
825 ("negative unit: minus, x, y",
826 Gecode::IntVarArgs() << minus << x << y);
827 }
828 {
829 TestSpace home;
830 IntVar x(home,0,5);
831 IntVar q(home,1,3);
832 IntVar y(home,2,10);
833 product(home,IntVarArgs({x,q}),y);
834 if ((home.status() == SS_FAILED) || (x.min()!=1))
835 return arithmetic_failed
836 ("nonzero result: x, q, y",
837 Gecode::IntVarArgs() << x << q << y);
838 }
839 {
840 TestSpace home;
841 IntVar x(home,-2,2);
842 IntVar q(home,1,3);
843 IntVar y(home,0,0);
844 product(home,IntVarArgs({x,q}),y);
845 if ((home.status() == SS_FAILED) || !x.assigned() || (x.val()!=0))
846 return arithmetic_failed
847 ("unique zero factor: x, q, y",
848 Gecode::IntVarArgs() << x << q << y);
849 }
850 {
851 TestSpace home;
852 IntVar x(home,-5,0);
853 IntVar q(home,2,4);
854 IntVar y(home,-100,100);
855 product(home,IntVarArgs({x,q}),y);
856 if ((home.status() == SS_FAILED) || (y.max()!=0))
857 return arithmetic_failed
858 ("nonpositive product: x, q, y",
859 Gecode::IntVarArgs() << x << q << y);
860 }
861 {
862 TestSpace home;
863 IntVar x(home,-5,-2);
864 IntVar q(home,-4,-2);
865 IntVar y(home,-100,100);
866 product(home,IntVarArgs({x,q}),y);
867 if ((home.status() == SS_FAILED) || (y.min()!=4))
868 return arithmetic_failed
869 ("positive product: x, q, y",
870 Gecode::IntVarArgs() << x << q << y);
871 }
872 {
873 TestSpace home;
874 const int hi=Gecode::Int::Limits::max;
875 IntVar minus(home,-1,-1);
876 IntVar x(home,hi,hi);
877 IntVar y(home,-hi,hi);
878 product(home,IntVarArgs({minus,x}),y);
879 if ((home.status() == SS_FAILED) || !y.assigned() || (y.val()!=-hi))
880 return arithmetic_failed
881 ("negative limit: minus, x, y",
882 Gecode::IntVarArgs() << minus << x << y);
883 }
884 {
885 CloneSpace home;
886 if (home.status() == SS_FAILED)
887 return arithmetic_failed
888 ("initial clone state: home.x, home.q, home.y",
889 Gecode::IntVarArgs() << home.x << home.q << home.y);
890 CloneSpace* clone=static_cast<CloneSpace*>(home.clone());
891 PropagatorGroup::all.disable(*clone);
892 rel(home,home.x,IRT_NQ,0);
893 rel(*clone,clone->x,IRT_NQ,0);
894 (void) home.status();
895 (void) clone->status();
896 rel(home,home.y,IRT_GQ,2);
897 rel(*clone,clone->y,IRT_GQ,2);
898 PropagatorGroup::all.enable(*clone);
899 const bool ok=(home.status()!=SS_FAILED) &&
900 (clone->status()!=SS_FAILED) && (home.x.min()==1) &&
901 (clone->x.min()==1) && (home.y.min()==2) &&
902 (clone->y.min()==2);
903 delete clone;
904 if (!ok)
905 return arithmetic_failed
906 ("rescheduling: home.x, home.q, home.y",
907 Gecode::IntVarArgs() << home.x << home.q << home.y);
908 }
909 return true;
910 }
911 };
912
915 protected:
916 class TestSpace : public Gecode::Space {
917 public:
918 virtual Gecode::Space* copy(void) { return nullptr; }
919 };
920 public:
922 : ::Test::Base("Int::Arithmetic::Product::PowerAlias") {}
923 virtual bool run(void) {
924 using namespace Gecode;
925 {
926 TestSpace home;
927 IntVar x(home,-10,10);
928 IntVar y(home,-10,100);
929 product(home,IntVarArgs({x,x}),y);
930 if ((home.status() == SS_FAILED) || (y.min() != 0))
931 return arithmetic_failed
932 ("square sign: x, y",
933 Gecode::IntVarArgs() << x << y);
934 }
935 {
936 TestSpace home;
937 IntVar x(home,-10,10);
938 IntVar y(home,20,30);
939 product(home,IntVarArgs({x,x}),y);
940 if ((home.status() == SS_FAILED) ||
941 (x.min() != -5) || (x.max() != 5))
942 return arithmetic_failed
943 ("square inverse: x, y",
944 Gecode::IntVarArgs() << x << y);
945 }
946 {
947 TestSpace home;
948 IntVar x(home,-10,10);
949 IntVar y(home,20,30);
950 product(home,IntVarArgs({x,x,x}),y);
951 if ((home.status() == SS_FAILED) || !x.assigned() ||
952 (x.val() != 3))
953 return arithmetic_failed
954 ("cube inverse: x, y",
955 Gecode::IntVarArgs() << x << y);
956 }
957 {
958 TestSpace home;
959 IntVar x(home,-10,10);
960 IntVar y(home,-30,-20);
961 product(home,IntVarArgs({x,x,x}),y);
962 if ((home.status() == SS_FAILED) || !x.assigned() ||
963 (x.val() != -3))
964 return arithmetic_failed
965 ("negative cube inverse: x, y",
966 Gecode::IntVarArgs() << x << y);
967 }
968 {
969 TestSpace home;
970 IntVar x(home,-10,10);
971 IntVar two(home,2,2);
972 IntVar y(home,50,72);
973 product(home,IntVarArgs({x,x,two}),y);
974 if ((home.status() == SS_FAILED) ||
975 (x.min() != -6) || (x.max() != 6))
976 return arithmetic_failed
977 ("square cofactor: x, two, y",
978 Gecode::IntVarArgs() << x << two << y);
979 }
980 {
981 // x*y=y has only the zero branch when x cannot be one.
982 TestSpace home;
983 IntVar x(home,2,4);
984 IntVar y(home,-10,10);
985 product(home,IntVarArgs({x,y}),y);
986 if ((home.status() == SS_FAILED) || !y.assigned() ||
987 (y.val() != 0))
988 return arithmetic_failed
989 ("zero result alias: x, y",
990 Gecode::IntVarArgs() << x << y);
991 }
992 {
993 // A nonzero result permits cancellation of one result occurrence.
994 TestSpace home;
995 IntVar x(home,0,2);
996 IntVar y(home,2,10);
997 product(home,IntVarArgs({x,y}),y);
998 if ((home.status() == SS_FAILED) || !x.assigned() ||
999 (x.val() != 1))
1000 return arithmetic_failed
1001 ("nonzero cancellation: x, y",
1002 Gecode::IntVarArgs() << x << y);
1003 }
1004 {
1005 // Cancelling one of two result occurrences leaves y*x=1.
1006 TestSpace home;
1007 IntVar x(home,-1,0);
1008 IntVar y(home,-2,-1);
1009 product(home,IntVarArgs({y,y,x}),y);
1010 if ((home.status() == SS_FAILED) || !x.assigned() ||
1011 !y.assigned() || (x.val() != -1) || (y.val() != -1))
1012 return arithmetic_failed
1013 ("repeated result cancellation: x, y",
1014 Gecode::IntVarArgs() << x << y);
1015 }
1016 {
1017 // Direct n-ary evaluation retains zero after an overflowing prefix.
1018 TestSpace home;
1019 const int hi=Gecode::Int::Limits::max;
1020 IntVar a(home,hi,hi);
1021 IntVar z(home,0,0);
1022 IntVar y(home,0,0);
1023 product(home,IntVarArgs({a,a,z}),y);
1024 if (home.status() == SS_FAILED)
1025 return arithmetic_failed
1026 ("overflow followed by zero: a, z, y",
1027 Gecode::IntVarArgs() << a << z << y);
1028 }
1029 {
1030 TestSpace home;
1031 IntVar x(home,-10,10);
1032 IntVar y(home,20,30);
1033 BoolVar b(home,1,1);
1034 product(home,IntVarArgs({x,x}),y,Reify(b,RM_EQV));
1035 if ((home.status() == SS_FAILED) ||
1036 (x.min() != -5) || (x.max() != 5))
1037 return arithmetic_failed
1038 ("reified square: x, y",
1039 Gecode::IntVarArgs() << x << y);
1040 }
1041 return true;
1042 }
1043 };
1044
1047 protected:
1048 class TestSpace : public Gecode::Space {
1049 public:
1050 virtual Gecode::Space* copy(void) { return nullptr; }
1051 };
1052 static bool bounds(const Gecode::IntVar& x, int min, int max) {
1053 return (x.min() == min) && (x.max() == max);
1054 }
1055 public:
1057 : ::Test::Base("Int::Arithmetic::Product::InverseBounds") {}
1058 virtual bool run(void) {
1059 using namespace Gecode;
1060 struct Case { int qmin; int qmax; int xmin; int xmax; };
1061 const Case cases[] = {
1062 { 2, 4, 5, 12}, // Positive
1063 { 0, 4, 5, 24}, // Nonnegative
1064 {-4,-2, -12, -5}, // Negative
1065 {-4, 0, -24, -5}, // Nonpositive
1066 {-4, 3, -24, 24} // Mixed across zero
1067 };
1068 for (unsigned int i=0; i<sizeof(cases)/sizeof(Case); i++) {
1069 TestSpace home;
1070 IntVar x(home,-100,100);
1071 IntVar q(home,cases[i].qmin,cases[i].qmax);
1072 IntVar y(home,20,24);
1073 product(home,IntVarArgs({x,q}),y);
1074 if ((home.status() == SS_FAILED) ||
1075 !bounds(x,cases[i].xmin,cases[i].xmax)) {
1076 if (opt.log)
1077 olog << "Inverse bounds, cofactor [" << cases[i].qmin
1078 << "," << cases[i].qmax << "]: x=" << x
1079 << ", q=" << q << ", y=" << y << "; expected x=["
1080 << cases[i].xmin << "," << cases[i].xmax << "]"
1081 << std::endl;
1082 return false;
1083 }
1084 }
1085 {
1086 // When both the cofactor and result can be zero, no inverse
1087 // pruning of the other factor is sound.
1088 TestSpace home;
1089 IntVar x(home,-100,100);
1090 IntVar q(home,-2,3);
1091 IntVar y(home,-10,10);
1092 product(home,IntVarArgs({x,q}),y);
1093 if ((home.status() == SS_FAILED) || !bounds(x,-100,100))
1094 return arithmetic_failed
1095 ("unconstrained zero product: x, q, y",
1096 Gecode::IntVarArgs() << x << q << y);
1097 }
1098 {
1099 TestSpace home;
1100 IntVar x(home,-100,100);
1101 IntVar zero(home,0,0);
1102 IntVar y(home,1,2);
1103 product(home,IntVarArgs({x,zero}),y);
1104 if (home.status() != SS_FAILED)
1105 return arithmetic_failed
1106 ("impossible zero product: x, zero, y",
1107 Gecode::IntVarArgs() << x << zero << y);
1108 }
1109 {
1110 TestSpace home;
1111 const int hi=Gecode::Int::Limits::max;
1112 IntVar x(home,-1,1);
1113 IntVar a(home,hi,hi);
1114 IntVar b(home,hi,hi);
1115 IntVar y(home,0,0);
1116 product(home,IntVarArgs({x,a,b}),y);
1117 if ((home.status() == SS_FAILED) || !x.assigned() ||
1118 (x.val() != 0))
1119 return arithmetic_failed
1120 ("overflowing cofactor: x, a, b, y",
1121 Gecode::IntVarArgs() << x << a << b << y);
1122 }
1123 return true;
1124 }
1125 };
1126
1129 class TestSpace : public Gecode::Space {
1130 public:
1131 virtual Gecode::Space* copy(void) { return nullptr; }
1132 };
1133 public:
1135 : ::Test::Base("Int::Arithmetic::Product::CompactionZero") {}
1136 virtual bool run(void) {
1137 using namespace Gecode;
1138 TestSpace home;
1139 IntVarArgs x(home,5,0,2);
1140 IntVar y(home,0,32);
1141 product(home,x,y);
1142 if (home.status() == SS_FAILED)
1143 return false;
1144 rel(home,x[0],IRT_EQ,1);
1145 rel(home,x[4],IRT_EQ,1);
1146 rel(home,x[2],IRT_EQ,0);
1147 if ((home.status() == SS_FAILED) || !y.assigned() || (y.val() != 0))
1148 return arithmetic_failed("compaction with a zero factor",x);
1149 return true;
1150 }
1151 };
1152
1154 int product_mod_value(const Assignment& x, int n, int m) {
1155 long long int p = 1 % m;
1156 for (int i=0; i<n; i++) {
1157 long long int q = static_cast<long long int>(x[i]) % m;
1158 if (q < 0)
1159 q += m;
1160 p = (p*q) % m;
1161 }
1162 return static_cast<int>(p);
1163 }
1164
1166 // Reified status tests observe interior residue membership, including
1167 // the empty-product residue, but subscribe only to bounds changes.
1168 class ProductModXYZR : public Test {
1169 protected:
1170 int m;
1171 public:
1172 ProductModXYZR(const std::string& s, const Gecode::IntSet& d,
1173 int m0, Gecode::IntPropLevel ipl, bool r=true)
1174 : Test("Arithmetic::ProductMod::XYZR::"+str(ipl)+"::"+s+(r ? "" : "::Plain"),
1175 4,d,r,ipl), m(m0) { contest=CTL_NONE; testfix=!r; }
1176 virtual bool solution(const Assignment& x) const {
1177 return product_mod_value(x,3,m) == x[3];
1178 }
1179 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1180 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[1],x[2]}),
1181 m,x[3],ipl);
1182 }
1184 Gecode::Reify r) {
1185 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[1],x[2]}),
1186 m,x[3],r,ipl);
1187 }
1188 };
1189
1192 bool variable;
1193 public:
1195 : Test("Arithmetic::ProductMod::Coefficient::"+
1196 std::string(v ? "Variable" : "Fixed"),3,
1197 Gecode::IntSet(-3,6),true,Gecode::IPL_BND), variable(v) {
1198 contest=CTL_NONE; testfix=false;
1199 }
1200 virtual bool solution(const Assignment& x) const {
1201 const int p=6*x[0]*x[1];
1202 return ((p%12)+12)%12 == x[2];
1203 }
1204 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1205 Gecode::IntVar c(home,6,6);
1206 const Gecode::IntVarArgs factors({x[0],c,x[1]});
1207 if (variable) {
1208 Gecode::IntVar m(home,12,12);
1209 Gecode::product_mod(home,factors,m,x[2],ipl);
1210 } else {
1211 Gecode::product_mod(home,factors,12,x[2],ipl);
1212 }
1213 }
1215 Gecode::Reify r) {
1216 Gecode::IntVar c(home,6,6);
1217 const Gecode::IntVarArgs factors({x[0],c,x[1]});
1218 if (variable) {
1219 Gecode::IntVar m(home,12,12);
1220 Gecode::product_mod(home,factors,m,x[2],r,ipl);
1221 } else {
1222 Gecode::product_mod(home,factors,12,x[2],r,ipl);
1223 }
1224 }
1225 };
1226
1228 class ProductModFolded : public Test {
1229 int c;
1230 int m;
1231 public:
1232 ProductModFolded(int c0, int m0, bool r)
1233 : Test("Arithmetic::ProductMod::Folded::"+str(c0)+"::"+str(m0)+
1234 (r ? "::Reified" : "::Plain"),3,Gecode::IntSet(-2,6),
1235 r,Gecode::IPL_BND), c(c0), m(m0) {
1237 }
1238 virtual bool solution(const Assignment& x) const {
1239 long long int p=1;
1240 const int v[]={c,x[0],c,x[0],x[1],c};
1241 for (int i=0; i<6; i++)
1242 p=(p*((v[i]%m+m)%m))%m;
1243 return p == x[2];
1244 }
1245 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1246 Gecode::IntVar a(home,c,c);
1248 Gecode::IntVarArgs({a,x[0],a,x[0],x[1],a}),
1249 m,x[2],ipl);
1250 }
1252 Gecode::Reify r) {
1253 Gecode::IntVar a(home,c,c);
1255 Gecode::IntVarArgs({a,x[0],a,x[0],x[1],a}),
1256 m,x[2],r,ipl);
1257 }
1258 };
1259
1262 class TestSpace : public Gecode::Space {
1263 public:
1264 virtual Gecode::Space* copy(void) { return nullptr; }
1265 };
1266 public:
1268 : ::Test::Base("Int::Arithmetic::ProductMod::Nonzero") {}
1269 virtual bool run(void) {
1270 using namespace Gecode;
1271 for (int negative=0; negative<2; negative++)
1272 for (int variable=0; variable<2; variable++) {
1273 TestSpace home;
1274 IntVarArgs x(home,4,negative ? -1 : 0,negative ? 0 : 1);
1275 IntVar y(home,0,6);
1276 IntVar m(home,7,11);
1277 if (variable)
1278 product_mod(home,x,m,y);
1279 else
1280 product_mod(home,x,7,y);
1281 if (home.status() == SS_FAILED)
1282 return false;
1283 rel(home,y,IRT_EQ,1);
1284 if (home.status() == SS_FAILED)
1285 return arithmetic_failed("nonzero result failed",x);
1286 for (int i=0; i<x.size(); i++)
1287 if (!x[i].assigned() || (x[i].val() != (negative ? -1 : 1)))
1288 return arithmetic_failed("nonzero result left zero endpoint",x);
1289 }
1290 return true;
1291 }
1292 };
1293
1295 class ProductModEmpty : public Test {
1296 protected:
1297 int m;
1298 public:
1299 ProductModEmpty(const std::string& s, const Gecode::IntSet& d,
1300 int m0, Gecode::IntPropLevel ipl)
1301 : Test("Arithmetic::ProductMod::Empty::"+str(ipl)+"::"+s,
1302 1,d,true,ipl), m(m0) { contest=CTL_NONE; }
1303 virtual bool solution(const Assignment& x) const {
1304 return x[0] == (1 % m);
1305 }
1306 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1308 }
1310 Gecode::Reify r) {
1312 }
1313 };
1314
1316 class ProductModSingleton : public Test {
1317 protected:
1318 int m;
1319 public:
1320 ProductModSingleton(const std::string& s, const Gecode::IntSet& d,
1321 int m0, Gecode::IntPropLevel ipl, bool r=true)
1322 : Test("Arithmetic::ProductMod::Singleton::"+str(ipl)+"::"+s+(r ? "" : "::Plain"),
1323 2,d,r,ipl), m(m0) { contest=CTL_NONE; testfix=!r; }
1324 virtual bool solution(const Assignment& x) const {
1325 int r = x[0] % m;
1326 if (r < 0)
1327 r += m;
1328 return r == x[1];
1329 }
1330 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1331 Gecode::product_mod(home,Gecode::IntVarArgs({x[0]}),m,x[1],ipl);
1332 }
1334 Gecode::Reify r) {
1335 Gecode::product_mod(home,Gecode::IntVarArgs({x[0]}),m,x[1],r,ipl);
1336 }
1337 };
1338
1340 class ProductModXXYAlias : public Test {
1341 protected:
1342 int m;
1343 public:
1344 ProductModXXYAlias(const std::string& s, const Gecode::IntSet& d,
1345 int m0, Gecode::IntPropLevel ipl, bool r=true)
1346 : Test("Arithmetic::ProductMod::XXYAlias::"+str(ipl)+"::"+s+(r ? "" : "::Plain"),
1347 2,d,r,ipl), m(m0) { contest=CTL_NONE; testfix=!r; }
1348 virtual bool solution(const Assignment& x) const {
1349 long long int a = x[0] % m;
1350 long long int b = x[1] % m;
1351 if (a < 0) a += m;
1352 if (b < 0) b += m;
1353 return (((a*a) % m)*b) % m == x[1];
1354 }
1355 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1356 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[0],x[1]}),
1357 m,x[1],ipl);
1358 }
1360 Gecode::Reify r) {
1361 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[0],x[1]}),
1362 m,x[1],r,ipl);
1363 }
1364 };
1365
1368 protected:
1369 class TestSpace : public Gecode::Space {
1370 public:
1371 virtual Gecode::Space* copy(void) { return nullptr; }
1372 };
1373 public:
1375 : ::Test::Base("Int::Arithmetic::ProductMod::Algebraic") {}
1376 virtual bool run(void) {
1377 using namespace Gecode;
1378 {
1379 TestSpace home;
1380 IntVarArgs x(home,3,-100,100);
1381 IntVar y(home,-10,10);
1382 product_mod(home,x,1,y);
1383 if ((home.status() == SS_FAILED) || !y.assigned() ||
1384 (y.val() != 0))
1385 return arithmetic_failed
1386 ("modulus one: x, y",
1387 Gecode::IntVarArgs() << x << y);
1388 }
1389 {
1390 TestSpace home;
1391 IntVar z(home,14,14);
1392 IntVar x(home,-1000,1000);
1393 IntVar y(home,0,6);
1394 product_mod(home,IntVarArgs({z,x}),7,y);
1395 if ((home.status() == SS_FAILED) || !y.assigned() ||
1396 (y.val() != 0))
1397 return arithmetic_failed
1398 ("zero residue factor: z, x, y",
1399 Gecode::IntVarArgs() << z << x << y);
1400 }
1401 {
1402 TestSpace home;
1403 IntVar one(home,1,1);
1404 IntVar x(home,20,30);
1405 IntVar y(home,0,99);
1406 product_mod(home,IntVarArgs({one,x}),100,y);
1407 if ((home.status() == SS_FAILED) ||
1408 (y.min() != 20) || (y.max() != 30))
1409 return arithmetic_failed
1410 ("nonwrapping product: one, x, y",
1411 Gecode::IntVarArgs() << one << x << y);
1412 }
1413 {
1414 TestSpace home;
1415 IntVar x(home,15,19);
1416 IntVar y(home,0,6);
1417 product_mod(home,IntVarArgs({x}),7,y);
1418 if ((home.status() == SS_FAILED) ||
1419 (y.min() != 1) || (y.max() != 5))
1420 return arithmetic_failed
1421 ("positive quotient band: x, y",
1422 Gecode::IntVarArgs() << x << y);
1423 }
1424 {
1425 TestSpace home;
1426 IntVar x(home,-20,-16);
1427 IntVar y(home,0,6);
1428 product_mod(home,IntVarArgs({x}),7,y);
1429 if ((home.status() == SS_FAILED) ||
1430 (y.min() != 1) || (y.max() != 5))
1431 return arithmetic_failed
1432 ("negative quotient band: x, y",
1433 Gecode::IntVarArgs() << x << y);
1434 }
1435 {
1436 TestSpace home;
1437 IntVar c(home,6,6);
1438 IntVar x(home,-100,100);
1439 IntVar y(home,9,9);
1440 product_mod(home,IntVarArgs({c,x}),15,y);
1441 if ((home.status() == SS_FAILED) ||
1442 (x.min() != -96) || (x.max() != 99))
1443 return arithmetic_failed
1444 ("linear congruence: c, x, y",
1445 Gecode::IntVarArgs() << c << x << y);
1446 }
1447 {
1448 TestSpace home;
1449 IntVar c(home,6,6);
1450 IntVar x(home,-100,100);
1451 IntVar y(home,8,8);
1452 product_mod(home,IntVarArgs({c,x}),15,y);
1453 if (home.status() != SS_FAILED)
1454 return arithmetic_failed
1455 ("inconsistent congruence: c, x, y",
1456 Gecode::IntVarArgs() << c << x << y);
1457 }
1458 {
1459 TestSpace home;
1460 IntVar z(home,0,0);
1461 IntVar x(home,-100,100);
1462 IntVar y(home,0,1);
1463 BoolVar b(home,0,1);
1464 product_mod(home,IntVarArgs({z,x}),7,y,Reify(b));
1465 rel(home,y,IRT_EQ,0);
1466 if ((home.status() == SS_FAILED) || !b.assigned() ||
1467 (b.val() != 1))
1468 return arithmetic_failed
1469 ("reified zero residue: z, x, y",
1470 Gecode::IntVarArgs() << z << x << y);
1471 }
1472 return true;
1473 }
1474 };
1475
1477 // Empty-product status observes membership of 0/1 in the result, and
1478 // divisor status observes interior modulus membership. Reposting can
1479 // therefore strengthen propagation without a subscribed bounds event.
1480 class ProductModVarXYMR : public Test {
1481 public:
1482 ProductModVarXYMR(const std::string& s, const Gecode::IntSet& d,
1484 : Test("Arithmetic::ProductModVar::XYMR::"+str(ipl)+"::"+s,
1485 4,d,true,ipl) { contest=CTL_NONE; testfix=false; }
1486 virtual bool solution(const Assignment& x) const {
1487 return (x[2] > 0) &&
1488 (product_mod_value(x,2,x[2]) == x[3]);
1489 }
1490 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1491 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[1]}),
1492 x[2],x[3],ipl);
1493 }
1495 Gecode::Reify r) {
1496 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[1]}),
1497 x[2],x[3],r,ipl);
1498 }
1499 };
1500
1502 class ProductModVarEmpty : public Test {
1503 public:
1504 ProductModVarEmpty(const std::string& s, const Gecode::IntSet& d,
1506 : Test("Arithmetic::ProductModVar::Empty::"+str(ipl)+"::"+s,
1507 2,d,true,ipl) { contest=CTL_NONE; testfix=false; }
1508 virtual bool solution(const Assignment& x) const {
1509 return (x[0] > 0) && (x[1] == (1 % x[0]));
1510 }
1511 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1512 Gecode::product_mod(home,Gecode::IntVarArgs(),x[0],x[1],ipl);
1513 }
1515 Gecode::Reify r) {
1516 Gecode::product_mod(home,Gecode::IntVarArgs(),x[0],x[1],r,ipl);
1517 }
1518 };
1519
1522 public:
1523 ProductModVarSingleton(const std::string& s, const Gecode::IntSet& d,
1525 : Test("Arithmetic::ProductModVar::Singleton::"+str(ipl)+"::"+s,
1526 3,d,true,ipl) { contest=CTL_NONE; testfix=false; }
1527 virtual bool solution(const Assignment& x) const {
1528 if (x[1] <= 0)
1529 return false;
1530 int r = x[0] % x[1];
1531 if (r < 0)
1532 r += x[1];
1533 return r == x[2];
1534 }
1535 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1537 x[1],x[2],ipl);
1538 }
1540 Gecode::Reify r) {
1542 x[1],x[2],r,ipl);
1543 }
1544 };
1545
1548 public:
1549 ProductModVarRepeated(const std::string& s, const Gecode::IntSet& d,
1551 : Test("Arithmetic::ProductModVar::Repeated::"+str(ipl)+"::"+s,
1552 3,d,true,ipl) { contest=CTL_NONE; testfix=false; }
1553 virtual bool solution(const Assignment& x) const {
1554 if (x[1] <= 0)
1555 return false;
1556 long long int a = x[0] % x[1];
1557 if (a < 0) a += x[1];
1558 return (a*a) % x[1] == x[2];
1559 }
1560 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1561 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[0]}),
1562 x[1],x[2],ipl);
1563 }
1565 Gecode::Reify r) {
1566 Gecode::product_mod(home,Gecode::IntVarArgs({x[0],x[0]}),
1567 x[1],x[2],r,ipl);
1568 }
1569 };
1570
1573 public:
1574 ProductModVarModFactorAlias(const std::string& s,
1575 const Gecode::IntSet& d,
1577 : Test("Arithmetic::ProductModVar::ModFactorAlias::"+
1578 str(ipl)+"::"+s,2,d,true,ipl) { contest=CTL_NONE; testfix=false; }
1579 virtual bool solution(const Assignment& x) const {
1580 return (x[0] > 0) && (x[1] == 0);
1581 }
1582 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1584 x[0],x[1],ipl);
1585 }
1587 Gecode::Reify r) {
1589 x[0],x[1],r,ipl);
1590 }
1591 };
1592
1595 public:
1597 const Gecode::IntSet& d,
1599 : Test("Arithmetic::ProductModVar::FactorResultAlias::"+
1600 str(ipl)+"::"+s,2,d,true,ipl) { contest=CTL_NONE; testfix=false; }
1601 virtual bool solution(const Assignment& x) const {
1602 if (x[1] <= 0)
1603 return false;
1604 int r = x[0] % x[1];
1605 if (r < 0) r += x[1];
1606 return r == x[0];
1607 }
1608 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1610 x[1],x[0],ipl);
1611 }
1613 Gecode::Reify r) {
1615 x[1],x[0],r,ipl);
1616 }
1617 };
1618
1621 public:
1622 ProductModVarModResultAlias(const std::string& s,
1623 const Gecode::IntSet& d,
1625 : Test("Arithmetic::ProductModVar::ModResultAlias::"+
1626 str(ipl)+"::"+s,2,d,true,ipl) { contest=CTL_NONE; }
1627 virtual bool solution(const Assignment&) const { return false; }
1628 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1630 x[1],x[1],ipl);
1631 }
1633 Gecode::Reify r) {
1635 x[1],x[1],r,ipl);
1636 }
1637 };
1638
1641 protected:
1642 class TestSpace : public Gecode::Space {
1643 public:
1644 virtual Gecode::Space* copy(void) { return nullptr; }
1645 };
1646 public:
1648 : ::Test::Base("Int::Arithmetic::ProductModVar::Bounds") {}
1649 virtual bool run(void) {
1650 using namespace Gecode;
1651 {
1652 TestSpace home;
1653 const int vm[5] = {-7,0,1,4,9};
1654 IntVar m(home,IntSet(vm,5));
1655 IntVar y(home,-5,12);
1656 product_mod(home,IntVarArgs(),m,y);
1657 if ((home.status() == SS_FAILED) || (m.min() != 1) ||
1658 (y.min() != 0) || (y.max() >= m.max()))
1659 return arithmetic_failed
1660 ("empty product range: m, y",
1661 Gecode::IntVarArgs() << m << y);
1662 }
1663 {
1664 TestSpace home;
1667 IntVar y(home,0,0);
1668 product_mod(home,IntVarArgs({x}),m,y);
1669 if (home.status() == SS_FAILED)
1670 return arithmetic_failed
1671 ("integer limit modulus: x, m, y",
1672 Gecode::IntVarArgs() << x << m << y);
1673 }
1674 {
1675 TestSpace home;
1676 IntVar x(home,3,5);
1677 IntVar c(home,2,2);
1678 IntVar m(home,5,6);
1679 IntVar y(home,2,2);
1680 product_mod(home,IntVarArgs({x,c}),m,y);
1681 if (home.status() == SS_FAILED)
1682 return arithmetic_failed
1683 ("delayed factor bounds: x, c, m, y",
1684 Gecode::IntVarArgs() << x << c << m << y);
1685 rel(home,x,IRT_LQ,4);
1686 if ((home.status() == SS_FAILED) || !x.assigned() ||
1687 (x.val() != 4))
1688 return arithmetic_failed
1689 ("nonwrapping variable modulus: x, c, m, y",
1690 Gecode::IntVarArgs() << x << c << m << y);
1691 }
1692 {
1693 TestSpace home;
1694 IntVar x(home,10,20);
1695 IntVar c(home,10,20);
1696 IntVar m(home,401,509);
1697 IntVar y(home,0,600);
1698 product_mod(home,IntVarArgs({x,c}),m,y);
1699 if ((home.status() == SS_FAILED) || (y.min() != 100) ||
1700 (y.max() != 400))
1701 return arithmetic_failed
1702 ("check 5: x, c, m, y",
1703 Gecode::IntVarArgs() << x << c << m << y);
1704 }
1705 return true;
1706 }
1707 };
1708
1711 class TestSpace : public Gecode::Space {
1712 public:
1713 virtual Gecode::Space* copy(void) { return nullptr; }
1714 };
1716 bool supported;
1717 public:
1719 : ::Test::Base("Int::Arithmetic::ProductModVar::SparseModulus::"+
1720 std::string(s ? "Assignment" : "Failure")),
1721 supported(s) {}
1722 virtual bool run(void) {
1723 using namespace Gecode;
1724 TestSpace home;
1725 IntVar p(home,60,60);
1726 IntVar y(home,4,4);
1727 IntVar m(home,IntSet({20,supported ? 28 : 25,100}));
1728 product_mod(home,IntVarArgs({p}),m,y);
1729 branch(home,m,INT_VAL_MIN());
1730 const SpaceStatus status=home.status();
1731 // m must divide 60-4=56. Dropping 100 exposes a single quotient
1732 // band, which must be reconsidered without an external wakeup.
1733 if (supported) {
1734 if ((status == SS_FAILED) || !m.assigned() || (m.val() != 28))
1735 return arithmetic_failed
1736 ("sparse modulus must become 28: p, m, y",
1737 IntVarArgs() << p << m << y);
1738 } else if (status != SS_FAILED) {
1739 return arithmetic_failed
1740 ("sparse modulus has no divisor of 56: p, m, y",
1741 IntVarArgs() << p << m << y);
1742 }
1743 return true;
1744 }
1745 };
1746
1749 protected:
1750 class TestSpace : public Gecode::Space {
1751 public:
1752 virtual Gecode::Space* copy(void) { return nullptr; }
1753 };
1754 static bool domain(const Gecode::IntVar& x, const int* v, int n) {
1755 if (x.size() != static_cast<unsigned int>(n))
1756 return false;
1757 for (int i=0; i<n; i++)
1758 if (!x.in(v[i]))
1759 return false;
1760 return true;
1761 }
1762 public:
1764 : ::Test::Base("Int::Arithmetic::ProductModVar::Algebraic") {}
1765 virtual bool run(void) {
1766 using namespace Gecode;
1767 {
1768 TestSpace home;
1770 IntVar z(home,-100,100);
1771 IntVar y(home,0,100);
1772 product_mod(home,IntVarArgs({z,m}),m,y);
1773 if ((home.status() == SS_FAILED) || !y.assigned() || (y.val()!=0))
1774 return arithmetic_failed
1775 ("modulus factor: m, z, y",
1776 Gecode::IntVarArgs() << m << z << y);
1777 }
1778 {
1779 TestSpace home;
1780 const int mv[5] = {-3,0,1,7,1000000};
1781 IntVar m(home,IntSet(mv,5));
1782 IntVar y(home,0,0);
1783 product_mod(home,IntVarArgs(),m,y);
1784 if ((home.status() == SS_FAILED) || !m.assigned() || (m.val()!=1))
1785 return arithmetic_failed
1786 ("empty product zero: m, y",
1787 Gecode::IntVarArgs() << m << y);
1788 }
1789 {
1790 TestSpace home;
1791 const int mv[4] = {1,2,17,1000000};
1792 const int keep[3] = {2,17,1000000};
1793 IntVar m(home,IntSet(mv,4));
1794 IntVar y(home,1,1);
1795 product_mod(home,IntVarArgs(),m,y);
1796 if ((home.status() == SS_FAILED) || !domain(m,keep,3))
1797 return arithmetic_failed
1798 ("empty product one: m, y",
1799 Gecode::IntVarArgs() << m << y);
1800 }
1801 {
1802 TestSpace home;
1803 const int mv[10] = {5,7,8,9,14,16,28,55,56,1000000};
1804 IntVar a(home,6,6);
1805 IntVar c(home,10,10);
1806 IntVar m(home,IntSet(mv,10));
1807 IntVar y(home,4,4);
1808 product_mod(home,IntVarArgs({a,c}),m,y);
1809 if ((home.status() == SS_FAILED) || (m.min() != 5) ||
1810 (m.max() != 56))
1811 return arithmetic_failed
1812 ("divisor upper bound: a, c, m, y",
1813 Gecode::IntVarArgs() << a << c << m << y);
1814 }
1815 {
1816 TestSpace home;
1817 const int mv[7] = {4,5,6,7,10,11,60};
1818 IntVar a(home,6,6);
1819 IntVar c(home,10,10);
1820 IntVar m(home,IntSet(mv,7));
1821 IntVar y(home,0,0);
1822 product_mod(home,IntVarArgs({a,c}),m,y);
1823 if ((home.status() == SS_FAILED) || (m.min() != 4) ||
1824 (m.max() != 60))
1825 return arithmetic_failed
1826 ("sparse divisor range: a, c, m, y",
1827 Gecode::IntVarArgs() << a << c << m << y);
1828 }
1829 {
1830 TestSpace home;
1831 const int mv[3] = {13,101,1000000};
1832 IntVar y(home,12,12);
1833 IntVar m(home,IntSet(mv,3));
1834 product_mod(home,IntVarArgs({y}),m,y);
1835 if ((home.status() == SS_FAILED) || !domain(m,mv,3))
1836 return arithmetic_failed
1837 ("result factor: y, m",
1838 Gecode::IntVarArgs() << y << m);
1839 }
1840 {
1841 TestSpace home;
1842 const int mv[3] = {2,17,1000000};
1843 IntVar z(home,0,0);
1844 IntVar y(home,0,0);
1845 IntVar m(home,IntSet(mv,3));
1846 product_mod(home,IntVarArgs({z}),m,y);
1847 if ((home.status() == SS_FAILED) || !domain(m,mv,3))
1848 return arithmetic_failed
1849 ("zero factor: z, y, m",
1850 Gecode::IntVarArgs() << z << y << m);
1851 }
1852 {
1853 TestSpace home;
1854 const int hi=Gecode::Int::Limits::max;
1855 IntVar a(home,hi,hi);
1856 IntVar c(home,hi,hi);
1857 IntVar d(home,hi,hi);
1858 IntVar m(home,2,hi);
1859 IntVar y(home,0,0);
1860 product_mod(home,IntVarArgs({a,c,d}),m,y);
1861 if (home.status() == SS_FAILED)
1862 return arithmetic_failed
1863 ("overflowing assigned product: a, c, d, m, y",
1864 Gecode::IntVarArgs() << a << c << d << m << y);
1865 }
1866 {
1867 TestSpace home;
1868 const int hi=Gecode::Int::Limits::max;
1869 IntVar x(home,hi,hi);
1870 IntVar m(home,1,hi);
1871 IntVar y(home,0,0);
1872 product_mod(home,IntVarArgs({x}),m,y);
1873 if ((home.status() == SS_FAILED) || (m.min() != 1) ||
1874 (m.max() != hi))
1875 return arithmetic_failed
1876 ("limit dividend: x, m, y",
1877 Gecode::IntVarArgs() << x << m << y);
1878 }
1879 {
1880 TestSpace home;
1881 const int hi=Gecode::Int::Limits::max;
1882 IntVar x(home,hi,hi);
1883 IntVar m(home,2,hi);
1884 IntVar y(home,1,1);
1885 product_mod(home,IntVarArgs({x}),m,y);
1886 if ((home.status() == SS_FAILED) || (m.min() != 2) ||
1887 (m.max() != hi-1))
1888 return arithmetic_failed
1889 ("limit difference: x, m, y",
1890 Gecode::IntVarArgs() << x << m << y);
1891 }
1892 return true;
1893 }
1894 };
1895
1898 int mode;
1899 public:
1901 : Test("Arithmetic::ProductModVar::Inactive::"+str(m),3,-4,7),
1902 mode(m) {}
1903 virtual bool solution(const Assignment& x) const {
1904 return ((x[0] == -3) || (x[0] == -1) || (x[0] == 0) ||
1905 (x[0] == 2) || (x[0] == 5)) &&
1906 ((x[1] == -2) || (x[1] == 0) || (x[1] == 1) ||
1907 (x[1] == 4)) &&
1908 ((x[2] == -4) || (x[2] == 0) || (x[2] == 3) ||
1909 (x[2] == 7));
1910 }
1911 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1912 using namespace Gecode;
1913 Gecode::dom(home,x[0],IntSet({-3,-1,0,2,5}));
1914 Gecode::dom(home,x[1],IntSet({-2,0,1,4}));
1915 Gecode::dom(home,x[2],IntSet({-4,0,3,7}));
1916 BoolVar b(home,mode,mode);
1917 product_mod(home,IntVarArgs({x[0],x[0]}),x[1],x[2],
1918 Reify(b,mode == 0 ? RM_IMP : RM_PMI));
1919 }
1920 };
1921
1924 protected:
1925 class TestSpace : public Gecode::Space {
1926 public:
1927 virtual Gecode::Space* copy(void) { return nullptr; }
1928 };
1929 public:
1931 : ::Test::Base("Int::Arithmetic::ProductMod::InvalidModulus") {}
1932 virtual bool run(void) {
1933 TestSpace home;
1934 Gecode::IntVar y(home,0,1);
1935 for (int m=0; m>=-1; m--) {
1936 try {
1938 return arithmetic_failed
1939 ("accepted invalid modulus: y",
1940 Gecode::IntVarArgs() << y);
1941 } catch (const Gecode::Int::OutOfLimits&) {
1942 }
1943 }
1944 return true;
1945 }
1946 };
1947
1949 class MultXYZ : public Test {
1950 public:
1952 MultXYZ(const std::string& s, const Gecode::IntSet& d,
1954 : Test("Arithmetic::Mult::XYZ::"+str(ipl)+"::"+s,3,d,false,ipl) {}
1955
1956 virtual bool solution(const Assignment& x) const {
1957 double d0 = static_cast<double>(x[0]);
1958 double d1 = static_cast<double>(x[1]);
1959 double d2 = static_cast<double>(x[2]);
1960 return d0*d1 == d2;
1961 }
1962
1963 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1964 Gecode::mult(home, x[0], x[1], x[2], ipl);
1965 }
1966 };
1967
1969 class MultXXY : public Test {
1970 public:
1972 MultXXY(const std::string& s, const Gecode::IntSet& d,
1974 : Test("Arithmetic::Mult::XXY::"+str(ipl)+"::"+s,2,d,false,ipl) {}
1975
1976 virtual bool solution(const Assignment& x) const {
1977 double d0 = static_cast<double>(x[0]);
1978 double d1 = static_cast<double>(x[0]);
1979 double d2 = static_cast<double>(x[1]);
1980 return d0*d1 == d2;
1981 }
1982
1983 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
1984 Gecode::mult(home, x[0], x[0], x[1], ipl);
1985 }
1986 };
1987
1989 class MultXYX : public Test {
1990 public:
1992 MultXYX(const std::string& s, const Gecode::IntSet& d,
1994 : Test("Arithmetic::Mult::XYX::"+str(ipl)+"::"+s,2,d,false,ipl) {}
1995
1996 virtual bool solution(const Assignment& x) const {
1997 double d0 = static_cast<double>(x[0]);
1998 double d1 = static_cast<double>(x[1]);
1999 double d2 = static_cast<double>(x[0]);
2000 return d0*d1 == d2;
2001 }
2002
2003 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2004 Gecode::mult(home, x[0], x[1], x[0], ipl);
2005 }
2006 };
2007
2009 class MultXYY : public Test {
2010 public:
2012 MultXYY(const std::string& s, const Gecode::IntSet& d,
2014 : Test("Arithmetic::Mult::XYY::"+str(ipl)+"::"+s,2,d,false,ipl) {}
2015
2016 virtual bool solution(const Assignment& x) const {
2017 double d0 = static_cast<double>(x[0]);
2018 double d1 = static_cast<double>(x[1]);
2019 double d2 = static_cast<double>(x[1]);
2020 return d0*d1 == d2;
2021 }
2022
2023 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2024 Gecode::mult(home, x[0], x[1], x[1], ipl);
2025 }
2026 };
2027
2029 class MultXXX : public Test {
2030 public:
2032 MultXXX(const std::string& s, const Gecode::IntSet& d,
2034 : Test("Arithmetic::Mult::XXX::"+str(ipl)+"::"+s,1,d,false,ipl) {}
2035
2036 virtual bool solution(const Assignment& x) const {
2037 double d0 = static_cast<double>(x[0]);
2038 double d1 = static_cast<double>(x[0]);
2039 double d2 = static_cast<double>(x[0]);
2040 return d0*d1 == d2;
2041 }
2042
2043 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2044 Gecode::mult(home, x[0], x[0], x[0], ipl);
2045 }
2046 };
2047
2049 class SqrXY : public Test {
2050 public:
2052 SqrXY(const std::string& s, const Gecode::IntSet& d,
2054 : Test("Arithmetic::Sqr::XY::"+str(ipl)+"::"+s,2,d,false,ipl) {}
2055
2056 virtual bool solution(const Assignment& x) const {
2057 double d0 = static_cast<double>(x[0]);
2058 double d1 = static_cast<double>(x[1]);
2059 return d0*d0 == d1;
2060 }
2061
2062 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2063 Gecode::sqr(home, x[0], x[1], ipl);
2064 }
2065 };
2066
2068 class SqrXX : public Test {
2069 public:
2071 SqrXX(const std::string& s, const Gecode::IntSet& d,
2073 : Test("Arithmetic::Sqr::XX::"+str(ipl)+"::"+s,1,d,false,ipl) {}
2074
2075 virtual bool solution(const Assignment& x) const {
2076 double d0 = static_cast<double>(x[0]);
2077 return d0*d0 == d0;
2078 }
2079
2080 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2081 Gecode::sqr(home, x[0], x[0], ipl);
2082 }
2083 };
2084
2086 class SqrtXY : public Test {
2087 public:
2089 SqrtXY(const std::string& s, const Gecode::IntSet& d,
2091 : Test("Arithmetic::Sqrt::XY::"+str(ipl)+"::"+s,2,d,false,ipl) {}
2092
2093 virtual bool solution(const Assignment& x) const {
2094 double d0 = static_cast<double>(x[0]);
2095 double d1 = static_cast<double>(x[1]);
2096 return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
2097 }
2098
2099 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2100 Gecode::sqrt(home, x[0], x[1], ipl);
2101 }
2102 };
2103
2105 class SqrtXX : public Test {
2106 public:
2108 SqrtXX(const std::string& s, const Gecode::IntSet& d,
2110 : Test("Arithmetic::Sqrt::XX::"+str(ipl)+"::"+s,1,d,false,ipl) {}
2111
2112 virtual bool solution(const Assignment& x) const {
2113 double d0 = static_cast<double>(x[0]);
2114 return (d0 >= 0) && (d0 >= d0*d0) && (d0 < (d0+1)*(d0+1));
2115 }
2116
2117 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2118 Gecode::sqrt(home, x[0], x[0], ipl);
2119 }
2120 };
2121
2123 class PowXY : public Test {
2124 protected:
2126 int n;
2127 public:
2129 PowXY(TestTags tags, const std::string& s, int n0,
2131 : Test(tags,
2132 "Arithmetic::Pow::XY::"+str(n0)+"::"+str(ipl)+"::"+s,
2133 2,d,false,ipl), n(n0) {}
2134
2135 virtual bool solution(const Assignment& x) const {
2136 long long int p = 1;
2137 for (int i=0; i<n; i++) {
2138 p *= x[0];
2139 if ((p < Gecode::Int::Limits::min) ||
2141 return false;
2142 }
2143 return p == x[1];
2144 }
2145
2146 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2147 using namespace Gecode;
2148 if (n > 4)
2149 pow(home, x[0], n, x[1], ipl);
2150 else
2151 rel(home, expr(home, pow(x[0],n), ipl), IRT_EQ, x[1], ipl);
2152 }
2153 };
2154
2156 class PowXX : public Test {
2157 protected:
2159 int n;
2160 public:
2162 PowXX(TestTags tags, const std::string& s,
2163 int n0, const Gecode::IntSet& d,
2165 : Test(tags,
2166 "Arithmetic::Pow::XX::"+str(n0)+"::"+str(ipl)+"::"+s,
2167 1,d,false,ipl), n(n0) {}
2168
2169 virtual bool solution(const Assignment& x) const {
2170 long long int p = 1;
2171 for (int i=0; i<n; i++) {
2172 p *= x[0];
2173 if ((p < Gecode::Int::Limits::min) ||
2175 return false;
2176 }
2177 return p == x[0];
2178 }
2179
2180 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2181 Gecode::pow(home, x[0], n, x[0], ipl);
2182 }
2183 };
2184
2185 bool powgr(int n, long long int r, int x) {
2186 assert(r >= 0);
2187 long long int y = r;
2188 long long int p = 1;
2189 do {
2190 p *= y; n--;
2191 if (p > x)
2192 return true;
2193 } while (n > 0);
2194 return false;
2195 }
2196
2197 int fnroot(int n, int x) {
2198 if (x < 2)
2199 return x;
2200 /*
2201 * We look for l such that: l^n <= x < (l+1)^n
2202 */
2203 long long int l = 1;
2204 long long int u = x;
2205 do {
2206 long long int m = (l + u) >> 1;
2207 if (powgr(n,m,x)) u=m; else l=m;
2208 } while (l+1 < u);
2209 return static_cast<int>(l);
2210 }
2211
2212 bool powle(int n, long long int r, int x) {
2213 assert(r >= 0);
2214 long long int y = r;
2215 long long int p = 1;
2216 do {
2217 p *= y; n--;
2218 if (p >= x)
2219 return false;
2220 } while (n > 0);
2221 assert(y < x);
2222 return true;
2223 }
2224
2225 int cnroot(int n, int x) {
2226 if (x < 2)
2227 return x;
2228 /*
2229 * We look for u such that: (u-1)^n < x <= u^n
2230 */
2231 long long int l = 1;
2232 long long int u = x;
2233 do {
2234 long long int m = (l + u) >> 1;
2235 if (powle(n,m,x)) l=m; else u=m;
2236 } while (l+1 < u);
2237 return static_cast<int>(u);
2238 }
2239
2241 class NrootXY : public Test {
2242 protected:
2244 int n;
2246 public:
2248 NrootXY(TestTags tags, const std::string& s, int n0,
2250 : Test(tags,
2251 "Arithmetic::Nroot::XY::"+str(n0)+"::"+str(ipl)+"::"+s,
2252 2,d,false,ipl), n(n0) {}
2253
2254 virtual bool solution(const Assignment& x) const {
2255 if (n == 1)
2256 return x[0] == x[1];
2257 if ((n % 2 == 0) && ((x[0] < 0) || (x[1] < 0)))
2258 return false;
2259 int r = (x[0] < 0) ? -cnroot(n,-x[0]) : fnroot(n,x[0]);
2260 return r == x[1];
2261 }
2262
2263 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2264 using namespace Gecode;
2265 if (n > 4)
2266 nroot(home, x[0], n, x[1], ipl);
2267 else
2268 rel(home, expr(home, nroot(x[0],n), ipl), IRT_EQ, x[1], ipl);
2269 }
2270 };
2271
2273 class NrootXX : public Test {
2274 protected:
2276 int n;
2277 public:
2279 NrootXX(TestTags tags, const std::string& s,
2280 int n0, const Gecode::IntSet& d,
2282 : Test(tags,
2283 "Arithmetic::Nroot::XX::"+str(n0)+"::"+str(ipl)+"::"+s,
2284 1,d,false,ipl), n(n0) {}
2285
2286 virtual bool solution(const Assignment& x) const {
2287 if (n == 1)
2288 return true;
2289 if (n % 2 == 0) {
2290 return (x[0] >= 0) && (x[0] <= 1);
2291 } else {
2292 return (x[0] >= -2) && (x[0] <= 1);
2293 }
2294 }
2295
2296 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2297 Gecode::nroot(home, x[0], n, x[0], ipl);
2298 }
2299 };
2300
2302 class DivMod : public Test {
2303 private:
2305 static int abs(int a) { return a<0 ? -a:a; }
2307 static int sgn(int a) { return a<0 ? -1:1; }
2308 public:
2310 DivMod(const std::string& s, const Gecode::IntSet& d)
2311 : Test("Arithmetic::DivMod::"+s,4,d) {}
2312
2313 virtual bool solution(const Assignment& x) const {
2314 return x[0] == static_cast<long long int>(x[1])*x[2]+x[3] &&
2315 abs(x[3]) < abs(x[1]) &&
2316 (x[3] == 0 || sgn(x[3]) == sgn(x[0]));
2317 }
2318
2319 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2320 Gecode::divmod(home, x[0], x[1], x[2], x[3]);
2321 }
2322 };
2323
2325 class Div : public Test {
2326 public:
2328 Div(const std::string& s, const Gecode::IntSet& d)
2329 : Test("Arithmetic::Div::"+s,3,d) {}
2330
2331 virtual bool solution(const Assignment& x) const {
2332 if (x[1] == 0)
2333 return false;
2334 int divsign = (x[0] / x[1] < 0) ? -1 : 1;
2335 int divresult =
2336 divsign *
2337 static_cast<int>(floor(static_cast<double>(std::abs(x[0]))/
2338 static_cast<double>(std::abs(x[1]))));
2339 return x[2] == divresult;
2340 }
2341
2342 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2343 Gecode::div(home, x[0], x[1], x[2]);
2344 }
2345 };
2346
2348 class Mod : public Test {
2349 public:
2351 Mod(const std::string& s, const Gecode::IntSet& d)
2352 : Test("Arithmetic::Mod::"+s,3,d) {}
2353
2354 virtual bool solution(const Assignment& x) const {
2355 if (x[1] == 0)
2356 return false;
2357 int divsign = (x[0] / x[1] < 0) ? -1 : 1;
2358 int divresult =
2359 divsign *
2360 static_cast<int>(floor(static_cast<double>(std::abs(x[0]))/
2361 static_cast<double>(std::abs(x[1]))));
2362 return x[0] == static_cast<long long int>(x[1])*divresult+x[2];
2363 }
2364
2365 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2366 Gecode::mod(home, x[0], x[1], x[2]);
2367 }
2368 };
2369
2371 class AbsXY : public Test {
2372 public:
2374 AbsXY(const std::string& s, const Gecode::IntSet& d,
2376 : Test(TestTags::check(),
2377 "Arithmetic::Abs::XY::"+str(ipl)+"::"+s,2,d,false,ipl) {}
2378
2379 virtual bool solution(const Assignment& x) const {
2380 double d0 = static_cast<double>(x[0]);
2381 double d1 = static_cast<double>(x[1]);
2382 return (d0<0 ? -d0 : d0) == d1;
2383 }
2384
2385 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2386 Gecode::abs(home, x[0], x[1], ipl);
2387 }
2388 };
2389
2391 class AbsXX : public Test {
2392 public:
2394 AbsXX(const std::string& s, const Gecode::IntSet& d,
2396 : Test(TestTags::check(),
2397 "Arithmetic::Abs::XX::"+str(ipl)+"::"+s,1,d,false,ipl) {}
2398
2399 virtual bool solution(const Assignment& x) const {
2400 double d0 = static_cast<double>(x[0]);
2401 double d1 = static_cast<double>(x[0]);
2402 return (d0<0 ? -d0 : d0) == d1;
2403 }
2404
2405 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2406 Gecode::abs(home, x[0], x[0], ipl);
2407 }
2408 };
2409
2411 class MinXYZ : public Test {
2412 public:
2414 MinXYZ(const std::string& s, const Gecode::IntSet& d,
2416 : Test("Arithmetic::Min::Bin::XYZ::"+str(ipl)+"::"+s,3,d,false,ipl) {}
2417
2418 virtual bool solution(const Assignment& x) const {
2419 return std::min(x[0],x[1]) == x[2];
2420 }
2421
2422 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2423 Gecode::min(home, x[0], x[1], x[2], ipl);
2424 }
2425 };
2426
2428 class MinXXY : public Test {
2429 public:
2431 MinXXY(const std::string& s, const Gecode::IntSet& d,
2433 : Test("Arithmetic::Min::Bin::XYX::"+str(ipl)+"::"+s,2,d) {}
2434
2435 virtual bool solution(const Assignment& x) const {
2436 return std::min(x[0],x[0]) == x[1];
2437 }
2438
2439 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2440 Gecode::min(home, x[0], x[0], x[1], ipl);
2441 }
2442 };
2443
2445 class MinXYX : public Test {
2446 public:
2448 MinXYX(const std::string& s, const Gecode::IntSet& d,
2450 : Test("Arithmetic::Min::Bin::XYX::"+str(ipl)+"::"+s,2,d) {}
2451
2452 virtual bool solution(const Assignment& x) const {
2453 return std::min(x[0],x[1]) == x[0];
2454 }
2455
2456 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2457 Gecode::min(home, x[0], x[1], x[0], ipl);
2458 }
2459 };
2460
2462 class MinXYY : public Test {
2463 public:
2465 MinXYY(const std::string& s, const Gecode::IntSet& d,
2467 : Test("Arithmetic::Min::Bin::XYY::"+str(ipl)+"::"+s,2,d) {}
2468
2469 virtual bool solution(const Assignment& x) const {
2470 return std::min(x[0],x[1]) == x[1];
2471 }
2472
2473 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2474 Gecode::min(home, x[0], x[1], x[1], ipl);
2475 }
2476 };
2477
2479 class MinXXX : public Test {
2480 public:
2482 MinXXX(const std::string& s, const Gecode::IntSet& d,
2484 : Test("Arithmetic::Min::Bin::XXX::"+str(ipl)+"::"+s,1,d) {}
2485
2486 virtual bool solution(const Assignment& x) const {
2487 return std::min(x[0],x[0]) == x[0];
2488 }
2489
2490 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2491 Gecode::min(home, x[0], x[0], x[0], ipl);
2492 }
2493 };
2494
2496 class MaxXYZ : public Test {
2497 public:
2499 MaxXYZ(const std::string& s, const Gecode::IntSet& d,
2501 : Test("Arithmetic::Max::Bin::XYZ::"+str(ipl)+"::"+s,3,d) {
2503 }
2504
2505 virtual bool solution(const Assignment& x) const {
2506 return std::max(x[0],x[1]) == x[2];
2507 }
2508
2509 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2510 Gecode::max(home, x[0], x[1], x[2], ipl);
2511 }
2512 };
2513
2515 class MaxXXY : public Test {
2516 public:
2518 MaxXXY(const std::string& s, const Gecode::IntSet& d,
2520 : Test("Arithmetic::Max::Bin::XXY::"+str(ipl)+"::"+s,2,d) {}
2521
2522 virtual bool solution(const Assignment& x) const {
2523 return std::max(x[0],x[0]) == x[1];
2524 }
2525
2526 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2527 Gecode::max(home, x[0], x[0], x[1], ipl);
2528 }
2529 };
2530
2532 class MaxXYX : public Test {
2533 public:
2535 MaxXYX(const std::string& s, const Gecode::IntSet& d,
2537 : Test("Arithmetic::Max::Bin::XYX::"+str(ipl)+"::"+s,2,d) {}
2538
2539 virtual bool solution(const Assignment& x) const {
2540 return std::max(x[0],x[1]) == x[0];
2541 }
2542
2543 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2544 Gecode::max(home, x[0], x[1], x[0], ipl);
2545 }
2546 };
2547
2549 class MaxXYY : public Test {
2550 public:
2552 MaxXYY(const std::string& s, const Gecode::IntSet& d,
2554 : Test("Arithmetic::Max::Bin::XYY::"+str(ipl)+"::"+s,2,d) {}
2555
2556 virtual bool solution(const Assignment& x) const {
2557 return std::max(x[0],x[1]) == x[1];
2558 }
2559
2560 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2561 Gecode::max(home, x[0], x[1], x[1], ipl);
2562 }
2563 };
2564
2566 class MaxXXX : public Test {
2567 public:
2569 MaxXXX(const std::string& s, const Gecode::IntSet& d,
2571 : Test("Arithmetic::Max::Bin::XXX::"+str(ipl)+"::"+s,1,d) {}
2572
2573 virtual bool solution(const Assignment& x) const {
2574 return std::max(x[0],x[0]) == x[0];
2575 }
2576
2577 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2578 Gecode::max(home, x[0], x[0], x[0], ipl);
2579 }
2580 };
2581
2583 class MinNary : public Test {
2584 public:
2587 : Test("Arithmetic::Min::Nary::"+str(ipl),4,-4,4,false,ipl) {}
2588
2589 virtual bool solution(const Assignment& x) const {
2590 return std::min(std::min(x[0],x[1]), x[2]) == x[3];
2591 }
2592
2593 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2594 Gecode::IntVarArgs m(3);
2595 m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
2596 Gecode::min(home, m, x[3], ipl);
2597 }
2598 };
2599
2601 class MinNaryShared : public Test {
2602 public:
2605 : Test("Arithmetic::Min::Nary::Shared::"+str(ipl),3,-4,4,false,ipl) {}
2606
2607 virtual bool solution(const Assignment& x) const {
2608 return std::min(std::min(x[0],x[1]), x[2]) == x[1];
2609 }
2610
2611 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2612 Gecode::IntVarArgs m(3);
2613 m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
2614 Gecode::min(home, m, x[1], ipl);
2615 }
2616 };
2617
2619 class MaxNary : public Test {
2620 public:
2623 : Test(TestTags::check(),
2624 "Arithmetic::Max::Nary::"+str(ipl),4,-4,4,false,ipl) {}
2625
2626 virtual bool solution(const Assignment& x) const {
2627 return std::max(std::max(x[0],x[1]), x[2]) == x[3];
2628 }
2629
2630 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2631 Gecode::IntVarArgs m(3);
2632 m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
2633 Gecode::max(home, m, x[3], ipl);
2634 }
2635 };
2636
2638 class MaxNaryShared : public Test {
2639 public:
2642 : Test(TestTags::check(),
2643 "Arithmetic::Max::Nary::Shared::"+str(ipl),
2644 3,-4,4,false,ipl) {}
2645
2646 virtual bool solution(const Assignment& x) const {
2647 return std::max(std::max(x[0],x[1]), x[2]) == x[1];
2648 }
2649
2650 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2651 Gecode::IntVarArgs m(3);
2652 m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
2653 Gecode::max(home, m, x[1], ipl);
2654 }
2655 };
2656
2658 class ArgMax : public Test {
2659 protected:
2664 public:
2666 ArgMax(int n, int o, bool tb)
2667 : Test(TestTags::check(),
2668 "Arithmetic::ArgMax::"+str(o)+"::"+str(tb)+"::"+str(n),
2669 n+1,0,n+1,
2670 false,tb ? Gecode::IPL_DEF : Gecode::IPL_DOM),
2671 offset(o), tiebreak(tb) {}
2672
2673 virtual bool solution(const Assignment& x) const {
2674 int n=x.size()-1;
2675 if ((x[n] < offset) || (x[n] >= n + offset))
2676 return false;
2677 int m=x[0]; int p=0;
2678 for (int i=1; i<n; i++)
2679 if (x[i] > m) {
2680 p=i; m=x[i];
2681 }
2682 return tiebreak ? (p + offset == x[n]) : (m == x[x[n]-offset]);
2683 }
2684
2685 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2686 int n=x.size()-1;
2687 Gecode::IntVarArgs m(n);
2688 for (int i=0; i<n; i++)
2689 m[i]=x[i];
2690 Gecode::argmax(home, m, offset, x[n], tiebreak);
2691 }
2692 };
2693
2695 class ArgMaxShared : public Test {
2696 protected:
2699 public:
2701 ArgMaxShared(int n, bool tb)
2702 : Test(TestTags::check(),
2703 "Arithmetic::ArgMax::Shared::"+str(tb)+"::"+str(n),n+1,0,n+1,
2704 false),
2705 tiebreak(tb) {
2706 testfix=false;
2707 }
2708
2709 virtual bool solution(const Assignment& x) const {
2710 int n=x.size()-1;
2711 if ((x[n] < 0) || (x[n] >= 2*n))
2712 return false;
2713 Gecode::IntArgs y(2*n);
2714 for (int i=0; i<n; i++)
2715 y[2*i+0]=y[2*i+1]=x[i];
2716 int m=y[0]; int p=0;
2717 for (int i=1; i<2*n; i++)
2718 if (y[i] > m) {
2719 p=i; m=y[i];
2720 }
2721 return tiebreak ? (p == x[n]) : (m == y[x[n]]);
2722 }
2723
2724 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2725 int n=x.size()-1;
2726 Gecode::IntVarArgs m(2*n);
2727 for (int i=0; i<n; i++)
2728 m[2*i+0]=m[2*i+1]=x[i];
2729 Gecode::argmax(home, m, x[n], tiebreak);
2730 }
2731 };
2732
2734 class ArgMin : public Test {
2735 protected:
2740 public:
2742 ArgMin(int n, int o, bool tb)
2743 : Test("Arithmetic::ArgMin::"+str(o)+"::"+str(tb)+"::"+str(n),
2744 n+1,0,n+1,
2745 false,tb ? Gecode::IPL_DEF : Gecode::IPL_DOM),
2746 offset(o), tiebreak(tb) {}
2747
2748 virtual bool solution(const Assignment& x) const {
2749 int n=x.size()-1;
2750 if ((x[n] < offset) || (x[n] >= n + offset))
2751 return false;
2752 int m=x[0]; int p=0;
2753 for (int i=1; i<n; i++)
2754 if (x[i] < m) {
2755 p=i; m=x[i];
2756 }
2757 return tiebreak ? (p+offset == x[n]) : (m == x[x[n]-offset]);
2758 }
2759
2760 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2761 int n=x.size()-1;
2762 Gecode::IntVarArgs m(n);
2763 for (int i=0; i<n; i++)
2764 m[i]=x[i];
2765 Gecode::argmin(home, m, offset, x[n], tiebreak);
2766 }
2767 };
2768
2770 class ArgMinShared : public Test {
2771 protected:
2774 public:
2776 ArgMinShared(int n, bool tb)
2777 : Test("Arithmetic::ArgMin::Shared::"+str(tb)+"::"+str(n),n+1,0,n+1,
2778 false),
2779 tiebreak(tb) {
2780 testfix=false;
2781 }
2782
2783 virtual bool solution(const Assignment& x) const {
2784 int n=x.size()-1;
2785 if ((x[n] < 0) || (x[n] >= 2*n))
2786 return false;
2787 Gecode::IntArgs y(2*n);
2788 for (int i=0; i<n; i++)
2789 y[2*i+0]=y[2*i+1]=x[i];
2790 int m=y[0]; int p=0;
2791 for (int i=1; i<2*n; i++)
2792 if (y[i] < m) {
2793 p=i; m=y[i];
2794 }
2795 return tiebreak ? (p == x[n]) : (m == y[x[n]]);
2796 }
2797
2798 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2799 int n=x.size()-1;
2800 Gecode::IntVarArgs m(2*n);
2801 for (int i=0; i<n; i++)
2802 m[2*i+0]=m[2*i+1]=x[i];
2803 Gecode::argmin(home, m, x[n], tiebreak);
2804 }
2805 };
2806
2808 class ArgMaxBool : public Test {
2809 protected:
2814 public:
2816 ArgMaxBool(int n, int o, bool tb)
2817 : Test(TestTags::check(),
2818 "Arithmetic::ArgMaxBool::"+str(o)+"::"+str(tb)+"::"+str(n),
2819 n+1,0,n+1,
2820 false,tb ? Gecode::IPL_DEF : Gecode::IPL_DOM),
2821 offset(o), tiebreak(tb) {}
2822
2823 virtual bool solution(const Assignment& x) const {
2824 int n=x.size()-1;
2825 if ((x[n] < offset) || (x[n] >= n + offset))
2826 return false;
2827 int m=x[0]; int p=0;
2828 if (x[0] > 1)
2829 return false;
2830 for (int i=1; i<n; i++) {
2831 if (x[i] > 1)
2832 return false;
2833 if (x[i] > m) {
2834 p=i; m=x[i];
2835 }
2836 }
2837 return tiebreak ? (p + offset == x[n]) : (m == x[x[n]-offset]);
2838 }
2839
2840 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2841 int n=x.size()-1;
2843 for (int i=0; i<n; i++)
2844 m[i]=channel(home,x[i]);
2845 Gecode::argmax(home, m, offset, x[n], tiebreak);
2846 }
2847 };
2848
2850 class ArgMaxBoolShared : public Test {
2851 protected:
2854 public:
2856 ArgMaxBoolShared(int n, bool tb)
2857 : Test(TestTags::check(),
2858 "Arithmetic::ArgMaxBool::Shared::"+str(tb)+"::"+str(n),n+1,0,n+1,
2859 false),
2860 tiebreak(tb) {
2861 testfix=false;
2862 }
2863
2864 virtual bool solution(const Assignment& x) const {
2865 int n=x.size()-1;
2866 if ((x[n] < 0) || (x[n] >= 2*n))
2867 return false;
2868 Gecode::IntArgs y(2*n);
2869 for (int i=0; i<n; i++)
2870 y[2*i+0]=y[2*i+1]=x[i];
2871 int m=y[0]; int p=0;
2872 if (y[0] > 1)
2873 return false;
2874 for (int i=1; i<2*n; i++) {
2875 if (y[i] > 1)
2876 return false;
2877 if (y[i] > m) {
2878 p=i; m=y[i];
2879 }
2880 }
2881 return tiebreak ? (p == x[n]) : (m == y[x[n]]);
2882 }
2883
2884 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2885 int n=x.size()-1;
2886 Gecode::BoolVarArgs m(2*n);
2887 for (int i=0; i<n; i++)
2888 m[2*i+0]=m[2*i+1]=channel(home,x[i]);
2889 Gecode::argmax(home, m, x[n], tiebreak);
2890 }
2891 };
2892
2894 class ArgMinBool : public Test {
2895 protected:
2900 public:
2902 ArgMinBool(int n, int o, bool tb)
2903 : Test("Arithmetic::ArgMinBool::"+str(o)+"::"+str(tb)+"::"+str(n),
2904 n+1,0,n+1,
2905 false,tb ? Gecode::IPL_DEF : Gecode::IPL_DOM),
2906 offset(o), tiebreak(tb) {}
2907
2908 virtual bool solution(const Assignment& x) const {
2909 int n=x.size()-1;
2910 if ((x[n] < offset) || (x[n] >= n + offset))
2911 return false;
2912 int m=x[0]; int p=0;
2913 if (x[0] < 0 || x[0] > 1)
2914 return false;
2915 for (int i=1; i<n; i++) {
2916 if (x[i] < 0 || x[i] > 1)
2917 return false;
2918 if (x[i] < m) {
2919 p=i; m=x[i];
2920 }
2921 }
2922 return tiebreak ? (p+offset == x[n]) : (m == x[x[n]-offset]);
2923 }
2924
2925 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2926 int n=x.size()-1;
2928 for (int i=0; i<n; i++)
2929 m[i]=channel(home,x[i]);
2930 Gecode::argmin(home, m, offset, x[n], tiebreak);
2931 }
2932 };
2933
2935 class ArgMinBoolShared : public Test {
2936 protected:
2939 public:
2941 ArgMinBoolShared(int n, bool tb)
2942 : Test("Arithmetic::ArgMinBool::Shared::"+str(tb)+"::"+str(n),n+1,0,n+1,
2943 false),
2944 tiebreak(tb) {
2945 testfix=false;
2946 }
2947
2948 virtual bool solution(const Assignment& x) const {
2949 int n=x.size()-1;
2950 if ((x[n] < 0) || (x[n] >= 2*n))
2951 return false;
2952 Gecode::IntArgs y(2*n);
2953 for (int i=0; i<n; i++)
2954 y[2*i+0]=y[2*i+1]=x[i];
2955 int m=y[0]; int p=0;
2956 if (y[0] > 1)
2957 return false;
2958 for (int i=1; i<2*n; i++) {
2959 if (y[i] > 1)
2960 return false;
2961 if (y[i] < m) {
2962 p=i; m=y[i];
2963 }
2964 }
2965 return tiebreak ? (p == x[n]) : (m == y[x[n]]);
2966 }
2967
2968 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
2969 int n=x.size()-1;
2970 Gecode::BoolVarArgs m(2*n);
2971 for (int i=0; i<n; i++)
2972 m[2*i+0]=m[2*i+1]=channel(home,x[i]);
2973 Gecode::argmin(home, m, x[n], tiebreak);
2974 }
2975 };
2976
2978 class Create {
2979 public:
2981 Create(void) {
2982
2983 const int va[7] = {
2985 -1,0,1,
2987 };
2988 const int vb[9] = {
2989 static_cast<int>(-sqrt(static_cast<double>
2991 -4,-2,-1,0,1,2,4,
2992 static_cast<int>(sqrt(static_cast<double>
2994 };
2995
2996 Gecode::IntSet a(va,7);
2997 Gecode::IntSet b(vb,9);
2998 Gecode::IntSet c(-8,8);
2999 Gecode::IntSet d(-70,70);
3000 const int vg[7] = {-12,-6,-1,0,4,9,12};
3001 Gecode::IntSet g(vg,7);
3002 const int vp[5] = {
3004 };
3005 Gecode::IntSet p(vp,5);
3006 Gecode::IntSet q(-2,2);
3007
3008 (void) new DivMod("A",a);
3009 (void) new DivMod("B",b);
3010 (void) new DivMod("C",c);
3011
3012 (void) new Div("A",a);
3013 (void) new Div("B",b);
3014 (void) new Div("C",c);
3015
3016 (void) new Mod("A",a);
3017 (void) new Mod("B",b);
3018 (void) new Mod("C",c);
3019
3020
3021 // These constraints ignore the propagation level.
3022 (void) new GcdXYZ("C",c,Gecode::IPL_BND);
3023 (void) new GcdXYZ("C",c,Gecode::IPL_BND,false);
3024 (void) new GcdXYZ("Sparse",g,Gecode::IPL_BND);
3025 (void) new GcdXYZ("Sparse",g,Gecode::IPL_BND,false);
3026 (void) new GcdXYZ("Limits",p,Gecode::IPL_BND);
3027 (void) new GcdXYZ("Limits",p,Gecode::IPL_BND,false);
3028 (void) new GcdXXY("C",c,Gecode::IPL_BND);
3029 (void) new GcdXXY("C",c,Gecode::IPL_BND,false);
3030 (void) new GcdXYX("C",c,Gecode::IPL_BND);
3031 (void) new GcdXYX("C",c,Gecode::IPL_BND,false);
3032 for (int sw=0; sw<2; sw++) {
3033 (void) new GcdXYX("Nonpositive",Gecode::IntSet(-2,0),
3034 Gecode::IPL_BND,true,sw != 0);
3035 (void) new GcdXYX("Nonpositive",Gecode::IntSet(-2,0),
3036 Gecode::IPL_BND,false,sw != 0);
3037 }
3038 (void) new GcdXXX("C",c,Gecode::IPL_BND);
3039
3040 (void) new DividesXY("C",c,Gecode::IPL_BND);
3041 (void) new DividesXY("Sparse",g,Gecode::IPL_BND);
3042 (void) new DividesXX("C",c,Gecode::IPL_BND);
3043
3044 (void) new ProductXYZR("C",q,Gecode::IPL_BND);
3045 (void) new ProductXYZR("Sparse",g,Gecode::IPL_BND);
3046 (void) new ProductXYZR("Limits",p,Gecode::IPL_BND);
3047 (void) new ProductEmpty("C",q,Gecode::IPL_BND);
3048 (void) new ProductSingleton("C",q,Gecode::IPL_BND);
3049 (void) new ProductXXYAlias("C",q,Gecode::IPL_BND);
3050
3051 (void) new ProductModXYZR("C",q,5,Gecode::IPL_BND);
3052 (void) new ProductModXYZR("C",q,5,Gecode::IPL_BND,false);
3053 (void) new ProductModXYZR("Sparse",g,7,Gecode::IPL_BND);
3054 (void) new ProductModXYZR("Sparse",g,7,Gecode::IPL_BND,false);
3055 (void) new ProductModEmpty("C",q,5,Gecode::IPL_BND);
3056 (void) new ProductModEmpty("ModulusOne",q,1,Gecode::IPL_BND);
3057 (void) new ProductModSingleton("C",g,5,Gecode::IPL_BND);
3058 (void) new ProductModSingleton("C",g,5,Gecode::IPL_BND,false);
3059 (void) new ProductModXXYAlias("C",q,5,Gecode::IPL_BND);
3060 (void) new ProductModXXYAlias("C",q,5,Gecode::IPL_BND,false);
3061
3062 (void) new ProductModVarXYMR("C",q,Gecode::IPL_BND);
3063 (void) new ProductModVarXYMR("Sparse",g,Gecode::IPL_BND);
3064 (void) new ProductModVarEmpty("C",q,Gecode::IPL_BND);
3065 (void) new ProductModVarSingleton("C",q,Gecode::IPL_BND);
3066 (void) new ProductModVarRepeated("C",q,Gecode::IPL_BND);
3070
3071 for (IntPropLevels ipls; ipls(); ++ipls) {
3072 (void) new AbsXY("A",a,ipls.ipl());
3073 (void) new AbsXY("B",b,ipls.ipl());
3074 (void) new AbsXY("C",c,ipls.ipl());
3075
3076 (void) new AbsXX("A",a,ipls.ipl());
3077 (void) new AbsXX("B",b,ipls.ipl());
3078 (void) new AbsXX("C",c,ipls.ipl());
3079 if (ipls.ipl() != Gecode::IPL_VAL) {
3080 (void) new MultXYZ("A",a,ipls.ipl());
3081 (void) new MultXYZ("B",b,ipls.ipl());
3082 (void) new MultXYZ("C",c,ipls.ipl());
3083
3084 (void) new MultXXY("A",a,ipls.ipl());
3085 (void) new MultXXY("B",b,ipls.ipl());
3086 (void) new MultXXY("C",c,ipls.ipl());
3087
3088 (void) new MultXYX("A",a,ipls.ipl());
3089 (void) new MultXYX("B",b,ipls.ipl());
3090 (void) new MultXYX("C",c,ipls.ipl());
3091
3092 (void) new MultXYY("A",a,ipls.ipl());
3093 (void) new MultXYY("B",b,ipls.ipl());
3094 (void) new MultXYY("C",c,ipls.ipl());
3095
3096 (void) new MultXXX("A",a,ipls.ipl());
3097 (void) new MultXXX("B",b,ipls.ipl());
3098 (void) new MultXXX("C",c,ipls.ipl());
3099
3100 (void) new SqrXY("A",a,ipls.ipl());
3101 (void) new SqrXY("B",b,ipls.ipl());
3102 (void) new SqrXY("C",c,ipls.ipl());
3103
3104 (void) new SqrXX("A",a,ipls.ipl());
3105 (void) new SqrXX("B",b,ipls.ipl());
3106 (void) new SqrXX("C",c,ipls.ipl());
3107
3108 for (int n=0; n<=6; n++) {
3109 TestTags distinct_tags = (n == 3) &&
3110 (ipls.ipl() == Gecode::IPL_BND)
3112 (void) new PowXY(TestTags::sweep(),"A",n,a,ipls.ipl());
3113 (void) new PowXY(TestTags::sweep(),"B",n,b,ipls.ipl());
3114 (void) new PowXY(distinct_tags,"C",n,c,ipls.ipl());
3115 (void) new PowXY(TestTags::sweep(),"D",n,d,ipls.ipl());
3116
3117 TestTags tags = (n == 0) && (ipls.ipl() == Gecode::IPL_BND)
3119 (void) new PowXX(tags,"A",n,a,ipls.ipl());
3120 (void) new PowXX(TestTags::sweep(),"B",n,b,ipls.ipl());
3121 (void) new PowXX(TestTags::sweep(),"C",n,c,ipls.ipl());
3122 (void) new PowXX(TestTags::sweep(),"D",n,d,ipls.ipl());
3123 }
3124
3125 for (int n=1; n<=6; n++) {
3126 TestTags distinct_tags = (n == 3) &&
3127 (ipls.ipl() == Gecode::IPL_BND)
3129 (void) new NrootXY(TestTags::sweep(),"A",n,a,ipls.ipl());
3130 (void) new NrootXY(TestTags::sweep(),"B",n,b,ipls.ipl());
3131 (void) new NrootXY(distinct_tags,"C",n,c,ipls.ipl());
3132 (void) new NrootXY(TestTags::sweep(),"D",n,d,ipls.ipl());
3133
3134 TestTags tags = (n == 1) && (ipls.ipl() == Gecode::IPL_BND)
3136 (void) new NrootXX(tags,"A",n,a,ipls.ipl());
3137 (void) new NrootXX(TestTags::sweep(),"B",n,b,ipls.ipl());
3138 (void) new NrootXX(TestTags::sweep(),"C",n,c,ipls.ipl());
3139 (void) new NrootXX(TestTags::sweep(),"D",n,d,ipls.ipl());
3140 }
3141
3142 for (int n=30; n<=34; n++) {
3143 (void) new PowXY(TestTags::sweep(),"C",n,c,ipls.ipl());
3144 (void) new PowXX(TestTags::sweep(),"C",n,c,ipls.ipl());
3145 (void) new NrootXY(TestTags::sweep(),"C",n,c,ipls.ipl());
3146 (void) new NrootXX(TestTags::sweep(),"C",n,c,ipls.ipl());
3147 }
3148
3149 (void) new SqrtXY("A",a,ipls.ipl());
3150 (void) new SqrtXY("B",b,ipls.ipl());
3151 (void) new SqrtXY("C",c,ipls.ipl());
3152
3153 (void) new SqrtXX("A",a,ipls.ipl());
3154 (void) new SqrtXX("B",b,ipls.ipl());
3155 (void) new SqrtXX("C",c,ipls.ipl());
3156
3157 (void) new MinXYZ("A",a,ipls.ipl());
3158 (void) new MinXYZ("B",b,ipls.ipl());
3159 (void) new MinXYZ("C",c,ipls.ipl());
3160
3161 (void) new MinXXY("A",a,ipls.ipl());
3162 (void) new MinXXY("B",b,ipls.ipl());
3163 (void) new MinXXY("C",c,ipls.ipl());
3164
3165 (void) new MinXYX("A",a,ipls.ipl());
3166 (void) new MinXYX("B",b,ipls.ipl());
3167 (void) new MinXYX("C",c,ipls.ipl());
3168
3169 (void) new MinXYY("A",a,ipls.ipl());
3170 (void) new MinXYY("B",b,ipls.ipl());
3171 (void) new MinXYY("C",c,ipls.ipl());
3172
3173 (void) new MinXXX("A",a,ipls.ipl());
3174 (void) new MinXXX("B",b,ipls.ipl());
3175 (void) new MinXXX("C",c,ipls.ipl());
3176
3177 (void) new MaxXYZ("A",a,ipls.ipl());
3178 (void) new MaxXYZ("B",b,ipls.ipl());
3179 (void) new MaxXYZ("C",c,ipls.ipl());
3180
3181 (void) new MaxXXY("A",a,ipls.ipl());
3182 (void) new MaxXXY("B",b,ipls.ipl());
3183 (void) new MaxXXY("C",c,ipls.ipl());
3184
3185 (void) new MaxXYX("A",a,ipls.ipl());
3186 (void) new MaxXYX("B",b,ipls.ipl());
3187 (void) new MaxXYX("C",c,ipls.ipl());
3188
3189 (void) new MaxXYY("A",a,ipls.ipl());
3190 (void) new MaxXYY("B",b,ipls.ipl());
3191 (void) new MaxXYY("C",c,ipls.ipl());
3192
3193 (void) new MaxXXX("A",a,ipls.ipl());
3194 (void) new MaxXXX("B",b,ipls.ipl());
3195 (void) new MaxXXX("C",c,ipls.ipl());
3196
3197 (void) new MinNary(ipls.ipl());
3198 (void) new MinNaryShared(ipls.ipl());
3199 (void) new MaxNary(ipls.ipl());
3200 (void) new MaxNaryShared(ipls.ipl());
3201 }
3202 }
3203
3204 for (int i=1; i<5; i++) {
3205 (void) new ArgMax(i,0,true);
3206 (void) new ArgMax(i,1,true);
3207 (void) new ArgMaxShared(i,true);
3208 (void) new ArgMin(i,0,true);
3209 (void) new ArgMin(i,1,true);
3210 (void) new ArgMinShared(i,true);
3211 (void) new ArgMax(i,0,false);
3212 (void) new ArgMax(i,1,false);
3213 (void) new ArgMaxShared(i,false);
3214 (void) new ArgMin(i,0,false);
3215 (void) new ArgMin(i,1,false);
3216 (void) new ArgMinShared(i,false);
3217
3218 (void) new ArgMaxBool(i,0,true);
3219 (void) new ArgMaxBool(i,1,true);
3220 (void) new ArgMaxBoolShared(i,true);
3221 (void) new ArgMinBool(i,0,true);
3222 (void) new ArgMinBool(i,1,true);
3223 (void) new ArgMinBoolShared(i,true);
3224 (void) new ArgMaxBool(i,0,false);
3225 (void) new ArgMaxBool(i,1,false);
3226 (void) new ArgMaxBoolShared(i,false);
3227 (void) new ArgMinBool(i,0,false);
3228 (void) new ArgMinBool(i,1,false);
3229 (void) new ArgMinBoolShared(i,false);
3230 }
3231 (void) new NumberTheorySparseBounds;
3232 for (int k=0; k<13; k++)
3233 (void) new NumberTheoryAlgebraic(k);
3234 (void) new NumberTheoryLifecycle;
3235 (void) new NumberTheoryLimits;
3236 (void) new DividesXY("Limits",p,Gecode::IPL_BND);
3237 for (int n=2; n<=3; n++)
3238 for (bool negative : {false,true})
3239 for (bool variable : {false,true})
3240 (void) new ProductModLimits(n,negative,variable);
3241 (void) new NumberTheoryIdentities;
3242 (void) new ProductModInvalidModulus;
3243 (void) new ProductModAlgebraic;
3244 (void) new ProductModNonzero;
3245 (void) new ProductCompactionZero;
3246 for (int r=0; r<2; r++) {
3247 (void) new ProductModFolded(-2,7,r != 0);
3248 (void) new ProductModFolded(6,12,r != 0);
3249 (void) new ProductModFolded(6,15,r != 0);
3250 (void) new ProductModFolded(Gecode::Int::Limits::max,5,r != 0);
3251 }
3252 for (int k=0; k<3; k++)
3253 (void) new ProductInterleaved(k);
3254 (void) new ProductModCoefficient(false);
3255 (void) new ProductModCoefficient(true);
3256 (void) new ProductModVarBounds;
3257 (void) new ProductModVarSparseModulus(false);
3258 (void) new ProductModVarSparseModulus(true);
3259 (void) new ProductModVarAlgebraic;
3260 (void) new ProductModVarInactive(0);
3261 (void) new ProductModVarInactive(1);
3262 (void) new ProductBoundsLarge;
3263 (void) new ProductSimplifySign;
3264 (void) new ProductPowerAlias;
3265 (void) new ProductInverseBounds;
3266 }
3267 };
3268
3271
3272 }
3273}}
3274
3275// STATISTICS: test-int
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1606
Passing Boolean variables.
Definition int.hh:739
Boolean integer variables.
Definition int.hh:534
Passing integer arguments.
Definition int.hh:653
Integer sets.
Definition int.hh:179
Passing integer variables.
Definition int.hh:681
Integer variable array.
Definition int.hh:792
Integer variables.
Definition int.hh:390
unsigned int size(void) const
Return size (cardinality) of domain.
Definition int.hpp:80
int min(void) const
Return minimum of domain.
Definition int.hpp:62
int val(void) const
Return assigned value.
Definition int.hpp:56
bool in(int n) const
Test whether n is contained in domain.
Definition int.hpp:97
int max(void) const
Return maximum of domain.
Definition int.hpp:70
Exception: Value out of limits
Definition exception.hpp:44
static PropagatorGroup all
Group of all propagators.
Definition core.hpp:796
Reification specification.
Definition int.hh:1176
Computation spaces.
Definition core.hpp:1787
struct Gecode::Space::@055132133326276162005044145100211202071356247106::@155123175027073262103111264343315000271204104107 c
Data available only during copying.
Argument array for variables.
Definition array.hpp:790
int size(void) const
Return size of array (number of elements).
Definition array.hpp:945
void update(Space &home, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition var.hpp:116
bool assigned(void) const
Test whether view is assigned.
Definition var.hpp:111
Base(std::string s)
Create and register a standard test with name s.
Definition test.cpp:125
static std::string str(bool b)
Map bool to string.
Definition test.hpp:121
TestTags tags(void) const
Return tags for test.
Definition test.hpp:95
Test for absolute value constraint with shared variables
virtual bool solution(const Assignment &x) const
Test whether x is solution
AbsXX(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.
Test for absolute value constraint
virtual bool solution(const Assignment &x) const
Test whether x is solution
AbsXY(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.
Test for argument maximum constraint with shared variables
virtual bool solution(const Assignment &x) const
Test whether x is solution
ArgMaxBoolShared(int n, bool tb)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
bool tiebreak
Whether to use tie-breaking.
Test for Boolean argument maximum constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
bool tiebreak
Whether to use tie-breaking.
virtual bool solution(const Assignment &x) const
Test whether x is solution
ArgMaxBool(int n, int o, bool tb)
Create and register test.
Test for argument maximum constraint with shared variables
ArgMaxShared(int n, bool tb)
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
bool tiebreak
Whether to use tie-breaking.
Test for argument 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.
int offset
Offset to be used.
bool tiebreak
Whether to use tie-breaking.
ArgMax(int n, int o, bool tb)
Create and register test.
Test for argument minimum constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
bool tiebreak
Whether to use tie-breaking.
ArgMinBoolShared(int n, bool tb)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for argument minimum constraint
int offset
Which offset to use.
bool tiebreak
Whether to use tie-breaking.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
ArgMinBool(int n, int o, bool tb)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for argument minimum constraint with shared variables
bool tiebreak
Whether to use tie-breaking.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
ArgMinShared(int n, bool tb)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for argument minimum 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.
ArgMin(int n, int o, bool tb)
Create and register test.
bool tiebreak
Whether to use tie-breaking.
int offset
Which offset to use.
Help class to create and register tests.
Create(void)
Perform creation and registration.
Test for division/modulo constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
DivMod(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 division 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.
Div(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Test divisibility with aliased operands
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x.
virtual bool solution(const Assignment &) const
Every integer divides itself, including zero.
DividesXX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
The constraint is deliberately exposed only as reified.
Test for the reified divisibility constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
The constraint is deliberately exposed only as reified.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x.
virtual bool solution(const Assignment &x) const
Test whether x is solution
DividesXY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for gcd with all variables aliased
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x.
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
GcdXXX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for gcd with identical operands
GcdXXY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl, bool r=true)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x.
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 gcd with result aliased to the first operand
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x.
GcdXYX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl, bool r=true, bool sw=false)
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 the ternary greatest-common-divisor 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.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x.
GcdXYZ(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl, bool r=true)
Create and register test.
Test for n-ary maximum constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
MaxNaryShared(Gecode::IntPropLevel ipl)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
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(Gecode::IntPropLevel ipl)
Create and register test.
Test for binary maximum constraint with shared variables
MaxXXX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
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 binary maximum constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
MaxXXY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for binary maximum constraint with shared variables
virtual bool solution(const Assignment &x) const
Test whether x is solution
MaxXYX(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.
Test for binary maximum constraint with shared variables
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.
MaxXYY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for binary maximum constraint
virtual bool solution(const Assignment &x) const
Test whether x is solution
MaxXYZ(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.
Test for n-ary minimmum constraint with shared variables
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
MinNaryShared(Gecode::IntPropLevel ipl)
Create and register test.
Test for n-ary minimmum 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.
MinNary(Gecode::IntPropLevel ipl)
Create and register test.
Test for binary minimum constraint with shared variables
MinXXX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
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 binary minimum constraint with shared variables
virtual bool solution(const Assignment &x) const
Test whether x is solution
MinXXY(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.
Test for binary minimum constraint with shared variables
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.
MinXYX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for binary minimum constraint with shared variables
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
MinXYY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for binary minimum 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
MinXYZ(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for modulo constraint
Mod(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 multiplication constraint with shared variables
virtual bool solution(const Assignment &x) const
Test whether x is solution
MultXXX(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.
Test for multiplication constraint with shared variables
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
MultXXY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for multiplication constraint with shared variables
MultXYX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
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 multiplication constraint with shared variables
virtual bool solution(const Assignment &x) const
Test whether x is solution
MultXYY(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.
Test for multiplication 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.
MultXYZ(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for nroot constraint with shared variables
NrootXX(TestTags tags, const std::string &s, int n0, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
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 nroot 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
NrootXY(TestTags tags, const std::string &s, int n0, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Floor.
Algebraic deductions must not wait for unrelated variables to be fixed.
Reified identities do not require the remaining factors to be fixed.
Activation and rescheduling survive cloning and arithmetic rewrites.
Known identities at the integer limits, independent of the oracles.
Sparse endpoints must reach a fixpoint after a divisor is assigned.
Test for power constraint with shared variables
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.
PowXX(TestTags tags, const std::string &s, int n0, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for power constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
PowXY(TestTags tags, const std::string &s, int n0, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual Gecode::Space * copy(void)
Copying member function.
Test forward and inverse product bounds on large factor domains
Unit compaction must finish before a zero factor subsumes the actor.
Test for the empty product
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
ProductEmpty(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual bool solution(const Assignment &x) const
Check for solution.
Interleaved powers exercise grouping, cofactors, and cloned rewrites.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
virtual Gecode::Space * copy(void)
Copying member function.
Test zero-aware inverse bounds for every cofactor sign class.
static bool bounds(const Gecode::IntVar &x, int min, int max)
virtual Gecode::Space * copy(void)
Copying member function.
Targeted algebraic and quotient-band propagation for fixed modulus.
Assigned coefficients with several free factors and all reification modes.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
Test for the empty modular product, including modulus one
virtual bool solution(const Assignment &x) const
Check for solution.
ProductModEmpty(const std::string &s, const Gecode::IntSet &d, int m0, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
Folded coefficients, repeated factors, and products beyond machine range.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
ProductModFolded(int c0, int m0, bool r)
virtual Gecode::Space * copy(void)
Copying member function.
Test that a nonpositive modular-product modulus is rejected
Known modular residues whose unreduced product exceeds 64 bits.
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
ProductModLimits(int n0, bool neg, bool var)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
A nonzero modular result excludes every zero factor.
Test for a singleton modular product
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
ProductModSingleton(const std::string &s, const Gecode::IntSet &d, int m0, Gecode::IntPropLevel ipl, bool r=true)
virtual Gecode::Space * copy(void)
Copying member function.
Targeted algebraic propagation for a variable modulus.
static bool domain(const Gecode::IntVar &x, const int *v, int n)
virtual Gecode::Space * copy(void)
Copying member function.
Targeted bounds and arithmetic checks for a variable modulus.
Test for the empty product with a variable modulus
ProductModVarEmpty(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
ProductModVarFactorResultAlias(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
Inactive implications preserve every assignment in the input domains.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
ProductModVarModFactorAlias(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
Test modulus/result aliasing, which is necessarily inconsistent
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
ProductModVarModResultAlias(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual bool solution(const Assignment &) const
Check for solution.
Test repeated factors with a variable modulus
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
ProductModVarRepeated(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
Test for a singleton product with a variable modulus
ProductModVarSingleton(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
Modulus bounds must reach a fixpoint after jumping over a domain hole.
Test for an ordinary two-factor product with a variable modulus
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
ProductModVarXYMR(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
Test repeated factors with the result aliased to a factor
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
ProductModXXYAlias(const std::string &s, const Gecode::IntSet &d, int m0, Gecode::IntPropLevel ipl, bool r=true)
Test for an ordinary and reified three-factor modular product
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
ProductModXYZR(const std::string &s, const Gecode::IntSet &d, int m0, Gecode::IntPropLevel ipl, bool r=true)
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual Gecode::Space * copy(void)
Copying member function.
Test repeated powers and result-alias cancellation.
virtual bool run(void)
Run test.
virtual Gecode::Space * copy(void)
Copying member function.
virtual Gecode::Space * copy(void)
Copying member function.
Test zero, unit, and aggregate-sign product simplifications.
Test for the singleton product
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
ProductSingleton(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
Test repeated factors and result aliasing
virtual bool solution(const Assignment &x) const
Check for solution.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
ProductXXYAlias(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
Test for an ordinary and reified three-factor product
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint.
virtual bool solution(const Assignment &x) const
Check for solution.
ProductXYZR(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Test for squaring constraint with shared variables
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
SqrXX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for squaring constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
SqrXY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for square root constraint with shared variables
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
SqrtXX(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Test for square root 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
SqrtXY(const std::string &s, const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Base class for assignments
Definition int.hh:73
int size(void) const
Return number of variables.
Definition int.hpp:46
Iterator for simple integer propagation levels.
Definition int.hh:430
static std::string str(bool b)
Map bool to string.
Definition int.hpp:278
bool testfix
Whether to perform fixpoint test.
Definition int.hh:287
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:281
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:283
Set of test tags.
Definition test.hh:105
static TestTags sweep(void)
Expensive parameter sweeps.
Definition test.hpp:59
static TestTags standard(void)
Standard suite without expensive sweeps.
Definition test.hpp:55
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
void product(Home home, const IntVarArgs &x, IntVar y, IntPropLevel ipl=IPL_DEF)
Constrain y to the exact product of the variables in x.
ReifyMode
Mode for reification.
Definition int.hh:1148
void product_mod(Home home, const IntVarArgs &x, int m, IntVar y, IntPropLevel ipl=IPL_DEF)
Constrain y to the product of x modulo m.
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:1274
@ IRT_EQ
Equality ( ).
Definition int.hh:1226
@ IRT_NQ
Disequality ( ).
Definition int.hh:1227
@ IRT_GQ
Greater or equal ( ).
Definition int.hh:1230
@ IRT_LQ
Less or equal ( ).
Definition int.hh:1228
@ RM_IMP
Implication for reification.
Definition int.hh:1162
@ RM_PMI
Inverse implication for reification.
Definition int.hh:1169
@ RM_EQV
Equivalence for reification (default).
Definition int.hh:1155
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:1279
@ IPL_VAL
Value propagation.
Definition int.hh:1277
@ IPL_DEF
Simple propagation levels.
Definition int.hh:1276
@ IPL_BND
Bounds propagation.
Definition int.hh:1278
Space(void)
Default constructor.
Definition core.cpp:121
virtual Space * copy(void)=0
Copying member function.
Space * clone(void) const
Clone space.
Definition core.hpp:3384
SpaceStatus status(StatusStatistics &stat)
Query space status.
Definition core.cpp:281
SpaceStatus
Space status
Definition core.hpp:1726
@ SS_FAILED
Space is failed
Definition core.hpp:1727
bool overflow_mul(int n, int m)
Check whether multiplying n and m would overflow.
Definition limits.hpp:107
const int min
Smallest allowed integer value.
Definition int.hh:123
bool valid(int n)
Return whether n is in range.
Definition limits.hpp:37
const int max
Largest allowed integer value.
Definition int.hh:121
Gecode toplevel namespace
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:988
void divides(Home home, IntVar divisor, IntVar dividend, Reify r, IntPropLevel ipl=IPL_DEF)
Reify whether divisor divides dividend.
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
IntRelType neg(IntRelType irt)
Return negated relation type of irt.
Definition irt.hpp:52
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void argmax(Home home, const IntVarArgs &x, IntVar y, bool tiebreak=true, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void mult(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void argmin(Home home, const IntVarArgs &x, IntVar y, bool tiebreak=true, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for .
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for .
void gcd(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Tests for arithmetic constraints
int gcd_value(int a, int b)
Compute the mathematical greatest common divisor for testing.
bool powgr(int n, long long int r, int x)
int cnroot(int n, int x)
int fnroot(int n, int x)
bool arithmetic_failed(const std::string &what, const Gecode::VarArgArray< Gecode::IntVar > &x)
Report a targeted arithmetic failure through the standard test log.
bool powle(int n, long long int r, int x)
int product_mod_value(const Assignment &x, int n, int m)
Compute a canonical modular product for testing.
bool product_value(const Assignment &x, int n, int &product)
Evaluate an exact product for testing without overflowing.
Testing finite domain integers.
Definition int.cpp:41
@ CTL_BOUNDS_Z
Test for bounds(z)-consistency.
Definition int.hh:157
@ CTL_NONE
No consistency-test.
Definition int.hh:154
General test support.
Definition afc.cpp:41
std::ostringstream olog
Stream used for logging.
Definition test.cpp:54
Region r
Definition region.cpp:65
@ check
Basic integrity tests.
Definition test.hh:99
Options opt
The options.
Definition test.cpp:163
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56