3
* Guido Tack <tack@gecode.org>
9
* $Date: 2005-11-01 16:01:21 +0100 (Tue, 01 Nov 2005) $ by $Author: zayenz $
12
* This file is part of Gecode, the generic constraint
13
* development environment:
14
* http://www.gecode.org
16
* See the file "LICENSE" for information on usage and
17
* redistribution of this file, and for a
18
* DISCLAIMER OF ALL WARRANTIES.
22
#include "test/set.hh"
23
#include "test/log.hh"
25
static const int d1r[4][2] = {
26
{-4,-3},{-1,-1},{1,1},{3,5}
28
static IntSet d1(d1r,4);
30
static IntSet d2(-1,3);
31
static IntSet d3(0,3);
33
static IntSet ds_33(-3,3);
35
class Card : public SetTest {
38
: SetTest(t,1,ds_33,false,1) {}
39
virtual bool solution(const SetAssignment& x) const {
41
for (CountableSetRanges xr(x.lub, x[0]);xr();++xr) s+= xr.width();
44
return s==(unsigned int)x.intval();
46
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
47
Gecode::cardinality(home, x[0], y[0]);
50
Card _card("Int::Card");
52
class MinElem : public SetTest {
54
MinElem(const char* t)
55
: SetTest(t,1,ds_33,false,1) {}
56
virtual bool solution(const SetAssignment& x) const {
57
CountableSetRanges xr0(x.lub, x[0]);
58
return xr0() && xr0.min()==x.intval();
60
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
61
Gecode::minElement(home, x[0], y[0]);
64
MinElem _minelem("Int::MinElem");
66
class MaxElem : public SetTest {
68
MaxElem(const char* t)
69
: SetTest(t,1,ds_33,false,1) {}
70
virtual bool solution(const SetAssignment& x) const {
71
CountableSetRanges xr0(x.lub, x[0]);
73
return x0.size() > 0 && x0.max()==x.intval();
75
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
76
Log::log("Gecode::maxElement(home, x[0], y[0]);","Gecode::maxElement(home, x[0], y[0]);");
77
Gecode::maxElement(home, x[0], y[0]);
80
MaxElem _maxelem("Int::MaxElem");
82
class Elem : public SetTest {
85
: SetTest(t,1,ds_33,true,1) {}
86
virtual bool solution(const SetAssignment& x) const {
87
for(CountableSetValues xr(x.lub, x[0]);xr();++xr)
88
if (xr.val()==x.intval())
92
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
93
Gecode::rel(home, x[0], SRT_SUP, y[0]);
95
virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
96
Gecode::rel(home, x[0], SRT_SUP, y[0], b);
99
Elem _elem("Int::Elem");
101
class NoElem : public SetTest {
103
NoElem(const char* t)
104
: SetTest(t,1,ds_33,true,1) {}
105
virtual bool solution(const SetAssignment& x) const {
106
for(CountableSetValues xr(x.lub, x[0]);xr();++xr)
107
if (xr.val()==x.intval())
111
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
112
Gecode::rel(home, x[0], SRT_DISJ, y[0]);
114
virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
115
Gecode::rel(home, x[0], SRT_DISJ, y[0], b);
118
NoElem _noelem("Int::NoElem");
120
class The : public SetTest {
123
: SetTest(t,1,ds_33,true,1) {}
124
virtual bool solution(const SetAssignment& x) const {
125
CountableSetRanges xr0(x.lub, x[0]);
127
return x0.size()==1 && x0.min()==x0.max() &&
128
x0.max()==x.intval();
130
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
131
Gecode::rel(home, x[0], SRT_EQ, y[0]);
133
virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
134
Gecode::rel(home, x[0], SRT_EQ, y[0], b);
137
The _the("Int::The");
140
int weightI(const IntArgs& elements,
141
const IntArgs& weights,
145
for (Iter::Ranges::ToValues<I> v(iter); v(); ++v) {
146
// Skip all elements below the current
147
while (elements[i]<v.val()) i++;
148
assert(elements[i] == v.val());
154
class Weights : public SetTest {
159
Weights(const char* t)
160
: SetTest(t,1,ds_33,false,1),
161
elements(7), weights(7) {
162
for (int i=-3; i<=3; i++)
164
for (int i=0; i<7; i++)
169
virtual bool solution(const SetAssignment& x) const {
170
CountableSetRanges x0(x.lub, x[0]);
171
return x.intval()==weightI(elements,weights,x0);
173
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
174
Gecode::weights(home, elements, weights, x[0], y[0]);
177
Weights _weights("Int::Weights");
179
class Match : public SetTest {
182
: SetTest(t,1,ds_33,false,3) {}
183
virtual bool solution(const SetAssignment& x) const {
184
if (x.ints()[0]>=x.ints()[1] ||
185
x.ints()[1]>=x.ints()[2])
187
CountableSetValues xr(x.lub, x[0]);
190
if (xr.val() != x.ints()[0])
195
if (xr.val() != x.ints()[1])
200
if (xr.val() != x.ints()[2])
207
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
208
Gecode::match(home, x[0], y);
211
Match _match("Int::Match");
213
class Channel : public SetTest {
217
Channel(const char* t, const IntSet& d, int _ssize, int _isize)
218
: SetTest(t,_ssize,d,false,_isize), ssize(_ssize), isize(_isize) {}
219
virtual bool solution(const SetAssignment& x) const {
220
for (int i=0; i<isize; i++) {
221
if (x.ints()[i] < 0 || x.ints()[i] >= ssize)
223
Iter::Ranges::Singleton single(i,i);
224
CountableSetRanges csr(x.lub, x[x.ints()[i]]);
225
if (!Iter::Ranges::subset(single, csr))
228
for (int i=0; i<ssize; i++) {
230
for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
231
if (csv.val() < 0 || csv.val() >= isize) return false;
232
if (x.ints()[csv.val()] != i) return false;
238
virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
239
Gecode::channelVarVal(home, y, x);
243
Channel _channel1("Int::Channel::1", d2, 2, 3);
244
Channel _channel2("Int::Channel::2", d3, 3, 3);
246
// STATISTICS: test-set