forked from Gecode/gecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnogoods.cpp
More file actions
302 lines (283 loc) · 9.51 KB
/
Copy pathnogoods.cpp
File metadata and controls
302 lines (283 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Contributing authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christian Schulte, 2013
* Guido Tack, 2004
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/minimodel.hh>
#include <gecode/search.hh>
#include "test/test.hh"
namespace Test {
/// Tests for search using no-goods
namespace NoGoods {
using namespace Gecode;
/// A dummy function for branching
void dummy(Space&) {
}
/// Example for testing integer no-goods
class Queens : public Space {
public:
/// Number of queens (must be even)
const static int n = 18;
/// Position of queens on boards
IntVarArray q;
/// The actual problem
Queens(IntValBranch ivb, bool assign, bool null)
: q(*this,n,0,n-1) {
distinct(*this, IntArgs::create(n,0,1), q, IPL_VAL);
distinct(*this, IntArgs::create(n,0,-1), q, IPL_VAL);
distinct(*this, q, IPL_VAL);
if (assign) {
IntVar x(*this,0,1); Gecode::assign(*this, x, INT_ASSIGN_MIN());
}
{
IntVarArgs q1(n/2), q2(n/2);
for (int i=0; i<n/2; i++) {
q1[i] = q[i]; q2[i] = q[n/2 + i];
}
branch(*this, q1, INT_VAR_NONE(), ivb);
if (null)
branch(*this, &dummy);
branch(*this, q2, INT_VAR_NONE(), ivb);
}
if (assign) {
IntVar x(*this,0,1); Gecode::assign(*this, x, INT_ASSIGN_MIN());
}
}
/// Constructor for cloning \a s
Queens(Queens& s) : Space(s) {
q.update(*this, s.q);
}
/// Perform copying during cloning
virtual Space* copy(void) {
return new Queens(*this);
}
/// Check whether two solutions are the same
bool same(const Queens& s) const {
for (int i=0; i<q.size(); i++)
if (q[i].val() != s.q[i].val())
return false;
return true;
}
/// Return increment for node stop
static unsigned int nodeinc(void) {
return 500;
}
/// Return name
static std::string name(void) {
return "Queens";
}
/// Return name for branching
static std::string val(IntValBranch ivb) {
switch (ivb.select()) {
case IntValBranch::SEL_MIN: return "INT_VAL_MIN";
case IntValBranch::SEL_MAX: return "INT_VAL_MAX";
case IntValBranch::SEL_SPLIT_MIN: return "INT_VAL_SPLIT_MIN";
case IntValBranch::SEL_SPLIT_MAX: return "INT_VAL_SPLIT_MAX";
case IntValBranch::SEL_VALUES_MIN: return "INT_VALUES_MIN";
case IntValBranch::SEL_VALUES_MAX: return "INT_VALUES_MAX";
default: GECODE_NEVER;
}
return "";
}
};
#ifdef GECODE_HAS_SET_VARS
/// Example for testing set no-goods
class Hamming : public Space {
private:
/// Number of symbols (must be even)
static const int size = 16;
/// Minimal Hamming-distance
static const int distance = 4;
/// Number of bits
static const int bits = 8;
/// The hamming code
SetVarArray x;
public:
/// Actual model
Hamming(SetValBranch svb, bool assign, bool null) :
x(*this,size,IntSet::empty,1,bits) {
SetVarArgs cx(x.size());
for (int i=x.size(); i--;)
cx[i] = expr(*this, -x[i]);
for (int i=0; i<x.size(); i++)
for (int j=i+1; j<x.size(); j++)
rel(*this,
cardinality(x[j] & cx[i]) +
cardinality(x[i] & cx[j]) >= distance);
if (assign) {
IntVar x(*this,0,1); Gecode::assign(*this, x, INT_ASSIGN_MIN());
}
{
SetVarArgs x1(size/2), x2(size/2);
for (int i=0; i<size/2; i++) {
x1[i] = x[i]; x2[i] = x[size/2 + i];
}
branch(*this, x1, SET_VAR_NONE(), svb);
if (null)
branch(*this, &dummy);
branch(*this, x2, SET_VAR_NONE(), svb);
}
if (assign) {
IntVar x(*this,0,1); Gecode::assign(*this, x, INT_ASSIGN_MIN());
}
}
/// Constructor for copying \a s
Hamming(Hamming& s) : Space(s) {
x.update(*this, s.x);
}
/// Copy during cloning
virtual Space* copy(void) {
return new Hamming(*this);
}
/// Check whether two solutions are the same
bool same(const Hamming& s) const {
for (int i=0; i<x.size(); i++) {
SetVarGlbRanges a(x[i]), b(s.x[i]);
if (!Iter::Ranges::equal(a,b))
return false;
}
return true;
}
/// Return increment for node stop
static unsigned int nodeinc(void) {
return 35;
}
/// Return name
static std::string name(void) {
return "Hamming";
}
/// Return name for branching
static std::string val(SetValBranch svb) {
switch (svb.select()) {
case SetValBranch::SEL_MIN_INC: return "SET_VAL_MIN_INC";
case SetValBranch::SEL_MAX_INC: return "SET_VAL_MAX_INC";
case SetValBranch::SEL_MIN_EXC: return "SET_VAL_MIN_EXC";
case SetValBranch::SEL_MAX_EXC: return "SET_VAL_MAX_EXC";
default: GECODE_NEVER;
}
return "";
}
};
#endif
/// %Base class for no-good tests
template<class Model, class ValBranch>
class NoGoods : public Base {
protected:
/// How to branch
ValBranch vb;
/// Number of threads to use
unsigned int t;
/// Whether to also assign some variables
bool a;
/// Whether to also create branchers without no-good literals
bool n;
public:
/// Map unsigned integer to string
static std::string str(unsigned int i) {
std::stringstream s;
s << i;
return s.str();
}
/// Initialize test
NoGoods(ValBranch vb0, unsigned int t0, bool a0, bool n0)
: Base("NoGoods::"+Model::name()+"::"+Model::val(vb0)+"::"+str(t0)+
"::"+(a0 ? "+" : "-")+"::"+(n0 ? "+" : "-")),
vb(vb0), t(t0), a(a0), n(n0) {}
/// Run test
virtual bool run(void) {
Model* m = new Model(vb,a,n);
// Solution without no-goods
Model* s_plain = dfs(m);
// Stop and re-start for a while to collect no-goods
{
Search::NodeStop ns(Model::nodeinc());
Search::Options o;
o.stop = &ns;
o.threads = t;
o.nogoods_limit = 256U;
Search::Engine* e = Search::dfsengine(m,o);
while (true) {
Model* s = static_cast<Model*>(e->next());
delete s;
if (!e->stopped())
break;
// Add no-goods
e->nogoods().post(*m);
ns.limit(ns.limit()+Model::nodeinc());
}
delete e;
}
// Compare whether the a or the same solution is found with no-goods
Model* s_nogoods = dfs(m);
bool ok = ((s_nogoods != NULL) &&
((t != 1) || s_plain->same(*s_nogoods)));
delete m;
delete s_nogoods;
delete s_plain;
return ok;
}
};
/// Help class to create and register tests
class Create {
public:
/// Perform creation and registration
Create(void) {
bool a = false;
do {
bool n = false;
do {
for (unsigned int t = 1; t<=4; t++) {
(void) new NoGoods<Queens,IntValBranch>(INT_VAL_MIN(),t,a,n);
(void) new NoGoods<Queens,IntValBranch>(INT_VAL_MAX(),t,a,n);
(void) new NoGoods<Queens,IntValBranch>(INT_VAL_SPLIT_MIN(),t,a,n);
(void) new NoGoods<Queens,IntValBranch>(INT_VAL_SPLIT_MAX(),t,a,n);
(void) new NoGoods<Queens,IntValBranch>(INT_VALUES_MIN(),t,a,n);
(void) new NoGoods<Queens,IntValBranch>(INT_VALUES_MAX(),t,a,n);
#ifdef GECODE_HAS_SET_VARS
(void) new NoGoods<Hamming,SetValBranch>(SET_VAL_MIN_INC(),t,a,n);
(void) new NoGoods<Hamming,SetValBranch>(SET_VAL_MIN_EXC(),t,a,n);
(void) new NoGoods<Hamming,SetValBranch>(SET_VAL_MAX_INC(),t,a,n);
(void) new NoGoods<Hamming,SetValBranch>(SET_VAL_MAX_EXC(),t,a,n);
#endif
}
n = !n;
} while (n);
a = !a;
} while (a);
}
};
Create c;
}
}
// STATISTICS: test-search