Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
dom.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2005
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <gecode/minimodel.hh>
35
36#include "test/int.hh"
37
38namespace Test { namespace Int {
39
41 namespace Dom {
42
49 class DomInt : public Test {
50 public:
52 DomInt(int n)
53 : Test("Dom::Int::"+str(n),n,-4,4,n == 1,
54 Gecode::IPL_DOM) {}
56 virtual bool solution(const Assignment& x) const {
57 for (int i=x.size(); i--; )
58 if (x[i] != -2)
59 return false;
60 return true;
61 }
63 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
64 if (x.size() == 1)
65 Gecode::dom(home, x[0], -2);
66 else
67 Gecode::dom(home, x, -2);
68 }
72 assert(x.size() == 1);
73 if (Base::rand(2) != 0) {
74 Gecode::dom(home, x[0], -2, r);
75 } else {
76 switch (r.mode()) {
77 case Gecode::RM_EQV:
78 Gecode::rel(home, Gecode::dom(x[0], -2) == r.var()); break;
79 case Gecode::RM_IMP:
80 Gecode::rel(home, Gecode::dom(x[0], -2) << r.var()); break;
81 case Gecode::RM_PMI:
82 Gecode::rel(home, Gecode::dom(x[0], -2) >> r.var()); break;
83 default: GECODE_NEVER;
84 }
85 }
86 }
87 };
88
89
91 class DomRange : public Test {
92 public:
95 : Test("Dom::Range::"+str(n),n,-4,4,n == 1,
96 Gecode::IPL_DOM) {}
98 virtual bool solution(const Assignment& x) const {
99 for (int i=x.size(); i--; )
100 if ((x[i] < -2) || (x[i] > 2))
101 return false;
102 return true;
103 }
105 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
106 if (x.size() == 1)
107 Gecode::dom(home, x[0], -2, 2);
108 else
109 Gecode::dom(home, x, -2, 2);
110 }
114 assert(x.size() == 1);
115 if (Base::rand(2) != 0) {
116 Gecode::dom(home, x[0], -2, 2, r);
117 } else {
118 switch (r.mode()) {
119 case Gecode::RM_EQV:
120 Gecode::rel(home, Gecode::dom(x[0], -2, 2) == r.var()); break;
121 case Gecode::RM_IMP:
122 Gecode::rel(home, Gecode::dom(x[0], -2, 2) << r.var()); break;
123 case Gecode::RM_PMI:
124 Gecode::rel(home, Gecode::dom(x[0], -2, 2) >> r.var()); break;
125 default: GECODE_NEVER;
126 }
127 }
128 }
129 };
130
132 class DomRangeEmpty : public Test {
133 public:
135 DomRangeEmpty(void) : Test("Dom::Range::Empty",1,-4,4,true) {}
137 virtual bool solution(const Assignment&) const {
138 return false;
139 }
141 virtual void post(Gecode::Space& home, Gecode::IntVarArray&) {
142 home.fail();
143 }
147 Gecode::dom(home, x[0], 3, 2, r);
148 }
149 };
150
151
152 const int r[4][2] = {
153 {-4,-3},{-1,-1},{1,1},{3,5}
154 };
156
158 class DomDom : public Test {
159 public:
162 : Test("Dom::Dom::"+str(n),n,-6,6,n == 1,
163 Gecode::IPL_DOM) {}
165 virtual bool solution(const Assignment& x) const {
166 for (int i=x.size(); i--; )
167 if (!(((x[i] >= -4) && (x[i] <= -3)) ||
168 ((x[i] >= -1) && (x[i] <= -1)) ||
169 ((x[i] >= 1) && (x[i] <= 1)) ||
170 ((x[i] >= 3) && (x[i] <= 5))))
171 return false;
172 return true;
173 }
175 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
176 if (x.size() == 1)
177 Gecode::dom(home, x[0], d);
178 else
179 Gecode::dom(home, x, d);
180 }
184 assert(x.size() == 1);
185 if (Base::rand(2) != 0) {
186 Gecode::dom(home, x[0], d, r);
187 } else {
188 switch (r.mode()) {
189 case Gecode::RM_EQV:
190 Gecode::rel(home, Gecode::dom(x[0], d) == r.var()); break;
191 case Gecode::RM_IMP:
192 Gecode::rel(home, Gecode::dom(x[0], d) << r.var()); break;
193 case Gecode::RM_PMI:
194 Gecode::rel(home, Gecode::dom(x[0], d) >> r.var()); break;
195 default: GECODE_NEVER;
196 }
197 }
198 }
199 };
200
209
210 }
211}}
212
213// STATISTICS: test-int
214
int n
Number of negative literals for node type.
Node * x
Pointer to corresponding Boolean expression node.
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Integer sets.
Definition int.hh:174
Integer variable array.
Definition int.hh:763
Reification specification.
Definition int.hh:876
Computation spaces.
Definition core.hpp:1742
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Base class for assignments
Definition int.hh:59
Test for domain constraint (full integer set)
Definition dom.cpp:158
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition dom.cpp:175
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:182
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition dom.cpp:165
DomDom(int n)
Create and register test.
Definition dom.cpp:161
Test for domain constraint (integer)
Definition dom.cpp:49
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:70
DomInt(int n)
Create and register test.
Definition dom.cpp:52
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition dom.cpp:56
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition dom.cpp:63
Test for domain constraint (empty range)
Definition dom.cpp:132
virtual void post(Gecode::Space &home, Gecode::IntVarArray &)
Post constraint on x.
Definition dom.cpp:141
virtual bool solution(const Assignment &) const
Test whether x is solution
Definition dom.cpp:137
DomRangeEmpty(void)
Create and register test.
Definition dom.cpp:135
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:145
Test for domain constraint (range)
Definition dom.cpp:91
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:112
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition dom.cpp:98
DomRange(int n)
Create and register test.
Definition dom.cpp:94
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition dom.cpp:105
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
void fail(void)
Fail space.
Definition core.hpp:4030
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ RM_IMP
Implication for reification.
Definition int.hh:862
@ RM_PMI
Inverse implication for reification.
Definition int.hh:869
@ RM_EQV
Equivalence for reification (default)
Definition int.hh:855
Gecode toplevel namespace
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
DomRangeEmpty dre
Definition dom.cpp:207
DomRange dr1(1)
DomDom dd1(1)
DomDom dd3(3)
DomRange dr3(3)
const int r[4][2]
Definition dom.cpp:152
DomInt di1(1)
Gecode::IntSet d(r, 4)
DomInt di3(3)
General test support.
Definition afc.cpp:39
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56