~ubuntu-branches/ubuntu/natty/gecode/natty

« back to all changes in this revision

Viewing changes to test/set/int.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2005-12-24 07:51:25 UTC
  • Revision ID: james.westby@ubuntu.com-20051224075125-klkiqofvbfvusfvt
Tags: upstream-1.0.0.dfsg.1
ImportĀ upstreamĀ versionĀ 1.0.0.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Main authors:
 
3
 *     Guido Tack <tack@gecode.org>
 
4
 *
 
5
 *  Copyright:
 
6
 *     Guido Tack, 2005
 
7
 *
 
8
 *  Last modified:
 
9
 *     $Date: 2005-11-01 16:01:21 +0100 (Tue, 01 Nov 2005) $ by $Author: zayenz $
 
10
 *     $Revision: 2465 $
 
11
 *
 
12
 *  This file is part of Gecode, the generic constraint
 
13
 *  development environment:
 
14
 *     http://www.gecode.org
 
15
 *
 
16
 *  See the file "LICENSE" for information on usage and
 
17
 *  redistribution of this file, and for a
 
18
 *     DISCLAIMER OF ALL WARRANTIES.
 
19
 *
 
20
 */
 
21
 
 
22
#include "test/set.hh"
 
23
#include "test/log.hh"
 
24
 
 
25
static const int d1r[4][2] = {
 
26
  {-4,-3},{-1,-1},{1,1},{3,5}
 
27
};
 
28
static IntSet d1(d1r,4);
 
29
 
 
30
static IntSet d2(-1,3);
 
31
static IntSet d3(0,3);
 
32
 
 
33
static IntSet ds_33(-3,3);
 
34
 
 
35
class Card : public SetTest {
 
36
public:
 
37
  Card(const char* t) 
 
38
    : SetTest(t,1,ds_33,false,1) {}
 
39
  virtual bool solution(const SetAssignment& x) const {
 
40
    unsigned int s = 0;
 
41
    for (CountableSetRanges xr(x.lub, x[0]);xr();++xr) s+= xr.width();
 
42
    if (x.intval() < 0)
 
43
      return false;
 
44
    return s==(unsigned int)x.intval();
 
45
  }
 
46
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
47
    Gecode::cardinality(home, x[0], y[0]);
 
48
  }
 
49
};
 
50
Card _card("Int::Card");
 
51
 
 
52
class MinElem : public SetTest {
 
53
public:
 
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();
 
59
  }
 
60
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
61
    Gecode::minElement(home, x[0], y[0]);
 
62
  }
 
63
};
 
64
MinElem _minelem("Int::MinElem");
 
65
 
 
66
class MaxElem : public SetTest {
 
67
public:
 
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]);
 
72
    IntSet x0(xr0);
 
73
    return x0.size() > 0 && x0.max()==x.intval();
 
74
  }
 
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]);
 
78
  }
 
79
};
 
80
MaxElem _maxelem("Int::MaxElem");
 
81
 
 
82
class Elem : public SetTest {
 
83
public:
 
84
  Elem(const char* t) 
 
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())
 
89
        return true;
 
90
    return false;
 
91
  }
 
92
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
93
    Gecode::rel(home, x[0], SRT_SUP, y[0]);
 
94
  }
 
95
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
 
96
    Gecode::rel(home, x[0], SRT_SUP, y[0], b);
 
97
  }
 
98
};
 
99
Elem _elem("Int::Elem");
 
100
 
 
101
class NoElem : public SetTest {
 
102
public:
 
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())
 
108
        return false;
 
109
    return true;
 
110
  }
 
111
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
112
    Gecode::rel(home, x[0], SRT_DISJ, y[0]);
 
113
  }
 
114
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
 
115
    Gecode::rel(home, x[0], SRT_DISJ, y[0], b);
 
116
  }
 
117
};
 
118
NoElem _noelem("Int::NoElem");
 
119
 
 
120
class The : public SetTest {
 
121
public:
 
122
  The(const char* t) 
 
123
    : SetTest(t,1,ds_33,true,1) {}
 
124
  virtual bool solution(const SetAssignment& x) const {    
 
125
    CountableSetRanges xr0(x.lub, x[0]);
 
126
    IntSet x0(xr0);
 
127
    return x0.size()==1 && x0.min()==x0.max() &&
 
128
      x0.max()==x.intval();
 
129
  }
 
130
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
131
    Gecode::rel(home, x[0], SRT_EQ, y[0]);
 
132
  }
 
133
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
 
134
    Gecode::rel(home, x[0], SRT_EQ, y[0], b);
 
135
  }
 
136
};
 
137
The _the("Int::The");
 
138
 
 
139
template <class I>
 
140
int weightI(const IntArgs& elements,
 
141
            const IntArgs& weights,
 
142
            I& iter) {
 
143
  int sum = 0;
 
144
  int i = 0;
 
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());
 
149
    sum += weights[i];
 
150
  }
 
151
  return sum;
 
152
}
 
153
 
 
154
class Weights : public SetTest {
 
155
public:
 
156
  IntArgs elements;
 
157
  IntArgs weights;
 
158
 
 
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++)
 
163
      elements[i+3] = i;
 
164
    for (int i=0; i<7; i++)
 
165
      weights[i] = 1;
 
166
    weights[1] = -2;
 
167
    weights[5] = 6;
 
168
  }
 
169
  virtual bool solution(const SetAssignment& x) const {    
 
170
    CountableSetRanges x0(x.lub, x[0]);
 
171
    return x.intval()==weightI(elements,weights,x0);
 
172
  }
 
173
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
174
    Gecode::weights(home, elements, weights, x[0], y[0]);
 
175
  }
 
176
};
 
177
Weights _weights("Int::Weights");
 
178
 
 
179
class Match : public SetTest {
 
180
public:
 
181
  Match(const char* t) 
 
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])
 
186
      return false;
 
187
    CountableSetValues xr(x.lub, x[0]);
 
188
    if (!xr())
 
189
      return false;
 
190
    if (xr.val() != x.ints()[0])
 
191
      return false;
 
192
    ++xr;
 
193
    if (!xr())
 
194
      return false;
 
195
    if (xr.val() != x.ints()[1])
 
196
      return false;
 
197
    ++xr;
 
198
    if (!xr())
 
199
      return false;
 
200
    if (xr.val() != x.ints()[2])
 
201
      return false;
 
202
    ++xr;
 
203
    if (xr())
 
204
      return false;
 
205
    return true;
 
206
  }
 
207
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
208
    Gecode::match(home, x[0], y);
 
209
  }
 
210
};
 
211
Match _match("Int::Match");
 
212
 
 
213
class Channel : public SetTest {
 
214
private:
 
215
  int ssize, isize;
 
216
public:
 
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)
 
222
        return false;
 
223
      Iter::Ranges::Singleton single(i,i);
 
224
      CountableSetRanges csr(x.lub, x[x.ints()[i]]);
 
225
      if (!Iter::Ranges::subset(single, csr))
 
226
        return false;
 
227
    }
 
228
    for (int i=0; i<ssize; i++) {
 
229
      int size = 0;
 
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;
 
233
        size++;
 
234
      }
 
235
    }
 
236
    return true;
 
237
  }
 
238
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
239
    Gecode::channelVarVal(home, y, x);
 
240
  }
 
241
};
 
242
 
 
243
Channel _channel1("Int::Channel::1", d2, 2, 3);
 
244
Channel _channel2("Int::Channel::2", d3, 3, 3);
 
245
 
 
246
// STATISTICS: test-set