3
* Christian Schulte <schulte@gecode.org>
4
* Mikael Lagerkvist <lagerkvist@gecode.org>
7
* Christian Schulte, 2005
8
* Mikael Lagerkvist, 2005
11
* $Date: 2005-12-02 14:47:51 +0100 (Fri, 02 Dec 2005) $ by $Author: zayenz $
14
* This file is part of Gecode, the generic constraint
15
* development environment:
16
* http://www.gecode.org
18
* See the file "LICENSE" for information on usage and
19
* redistribution of this file, and for a
20
* DISCLAIMER OF ALL WARRANTIES.
24
#include "test/int.hh"
25
#include "test/log.hh"
28
Assignment::Assignment(int n0, const IntSet& d0)
29
: n(n0), dsv(new IntSetValues[n]), d(d0) {
34
Assignment::reset(void) {
41
Assignment::operator++(void) {
57
operator<<(std::ostream& os, const Assignment& a) {
60
for (int i=0; i<n; i++)
61
os << a[i] << ((i!=n-1) ? "," : "}");
65
#define FORCE_FIXFLUSH \
67
if (random(opt.fixprob) == 0) { \
70
if (status(alt) == SS_FAILED) return; \
72
if (random(opt.flushprob) == 0) { \
78
class IntTestSpace : public Space {
85
IntTestSpace(int n, IntSet& d, const Options& o)
86
: x(this, n, d), opt(o) {
89
IntTestSpace(bool share, IntTestSpace& s) : Space(share,s), opt(s.opt) {
90
x.update(this, share, s.x);
92
virtual Space* copy(bool share) {
93
return new IntTestSpace(share,*this);
95
bool is_failed(void) {
98
return status(alt) == SS_FAILED;
100
void assign(const Assignment& a) {
101
for (int i=a.size(); i--; ) {
102
Log::assign(Log::mk_name("x", i), a[i]);
103
rel(this, x[i], IRT_EQ, a[i]);
107
bool assigned(void) const {
108
for (int i=x.size(); i--; )
109
if (!x[i].assigned())
113
void prune(const Assignment& a) {
114
// Select variable to be pruned
115
int i = random(x.size());
116
while (x[i].assigned()) {
117
i = (i+1) % x.size();
119
// Select mode for pruning
121
if ((m == 0) && (a[i] < x[i].max())) {
122
int v=a[i]+1+random(static_cast<unsigned int>(x[i].max()-a[i]));
123
assert((v > a[i]) && (v <= x[i].max()));
124
Log::prune(x[i], Log::mk_name("x", i), IRT_LE, v);
125
rel(this, x[i], IRT_LE, v);
126
Log::prune_result(x[i]);
127
} else if ((m == 1) && (a[i] > x[i].min())) {
128
int v=x[i].min()+random(static_cast<unsigned int>(a[i]-x[i].min()));
129
assert((v < a[i]) && (v >= x[i].min()));
130
Log::prune(x[i], Log::mk_name("x", i), IRT_GR, v);
131
rel(this, x[i], IRT_GR, v);
132
Log::prune_result(x[i]);
135
Int::ViewRanges<Int::IntView> it(x[i]);
136
unsigned int skip = random(x[i].size()-1);
138
if (it.width() > skip) {
141
if (it.width() == 1) {
143
} else if (v < it.max()) {
154
Log::prune(x[i], Log::mk_name("x", i), IRT_NQ, v);
155
rel(this, x[i], IRT_NQ, v);
156
Log::prune_result(x[i]);
166
IntTest::make_assignment() {
167
return new Assignment(arity, dom);
171
IntTest::do_search_test() {
183
IntTest::run(const Options& opt) {
184
const char* test = "NONE";
185
const char* problem = "NONE";
186
// Set up assignments
187
Assignment* ap = make_assignment();
189
// Set up space for all solution search
190
IntTestSpace* search_s = new IntTestSpace(arity,dom,opt);
191
post(search_s,search_s->x);
192
branch(search_s,search_s->x,BVAR_NONE,BVAL_MIN);
193
Gecode::DFS<IntTestSpace> e_s(search_s);
196
bool is_sol = solution(a);
197
if (do_search_test()) {
200
IntTestSpace* s = e_s.next();
201
CHECK(s != NULL, "Solutions exhausted");
202
CHECK(!s->actors(), "No subsumtion");
203
for (int i=a.size(); i--; ) {
204
CHECK(s->x[i].assigned(), "Unassigned variable");
205
CHECK(a[i] == s->x[i].val(), "Wrong value in solution");
211
test = "Assignment (after posting)";
213
IntTestSpace* s = new IntTestSpace(arity,dom,opt);
214
post(s,s->x); s->assign(a);
216
CHECK(!s->is_failed(), "Failed on solution");
217
CHECK(!s->actors(), "No subsumtion");
219
CHECK(s->is_failed(), "Solved on non-solution");
224
test = "Assignment (before posting)";
226
IntTestSpace* s = new IntTestSpace(arity,dom,opt);
227
s->assign(a); post(s,s->x);
229
CHECK(!s->is_failed(), "Failed on solution");
230
CHECK(!s->actors(), "No subsumtion");
232
CHECK(s->is_failed(), "Solved on non-solution");
237
test = "Assignment reified (before posting)";
239
IntTestSpace* s = new IntTestSpace(arity,dom,opt);
241
s->assign(a); post(s,s->x,b);
242
CHECK(!s->is_failed(), "Failed");
243
CHECK(!s->actors(), "No subsumtion");
244
CHECK(b.assigned(), "Control variable unassigned");
246
CHECK(b.val()==1, "Zero on solution");
248
CHECK(b.val()==0, "One on non-solution");
253
test = "Assignment reified (after posting)";
255
IntTestSpace* s = new IntTestSpace(arity,dom,opt);
257
post(s,s->x,b); s->assign(a);
258
CHECK(!s->is_failed(), "Failed");
259
CHECK(!s->actors(), "No subsumtion");
260
CHECK(b.assigned(), "Control variable unassigned");
262
CHECK(b.val()==1, "Zero on solution");
264
CHECK(b.val()==0, "One on non-solution");
271
IntTestSpace* s = new IntTestSpace(arity,dom,opt);
273
while (!s->failed() && !s->assigned())
277
CHECK(!s->is_failed(), "Failed on solution");
278
CHECK(!s->actors(), "No subsumtion");
280
CHECK(s->is_failed(), "Solved on non-solution");
285
test = "Prune reified";
287
IntTestSpace* s = new IntTestSpace(arity,dom,opt);
290
while (!s->failed() && !s->assigned() && !b.assigned())
292
CHECK(!s->is_failed(), "Failed");
293
CHECK(!s->actors(), "No subsumtion");
294
CHECK(b.assigned(), "Control variable unassigned");
296
CHECK(b.val()==1, "Zero on solution");
298
CHECK(b.val()==0, "One on non-solution");
304
if (do_search_test()) {
306
if (e_s.next() != NULL) {
307
problem = "Excess solutions";
314
std::cout << "FAILURE" << std::endl
315
<< "\t" << "Test: " << test << std::endl
316
<< "\t" << "Problem: " << problem << std::endl
317
<< "\t" << "Assignment: " << a << std::endl;
324
// STATISTICS: test-int