main page
modules
namespaces
classes
files
Gecode home
Generated on Sat May 25 2013 18:00:38 for Gecode by
doxygen
1.8.3.1
test
int
nvalues.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, 2011
8
*
9
* Last modified:
10
* $Date: 2011-08-17 22:25:49 +0200 (Wed, 17 Aug 2011) $ by $Author: schulte $
11
* $Revision: 12308 $
12
*
13
* This file is part of Gecode, the generic constraint
14
* development environment:
15
* http://www.gecode.org
16
*
17
* Permission is hereby granted, free of charge, to any person obtaining
18
* a copy of this software and associated documentation files (the
19
* "Software"), to deal in the Software without restriction, including
20
* without limitation the rights to use, copy, modify, merge, publish,
21
* distribute, sublicense, and/or sell copies of the Software, and to
22
* permit persons to whom the Software is furnished to do so, subject to
23
* the following conditions:
24
*
25
* The above copyright notice and this permission notice shall be
26
* included in all copies or substantial portions of the Software.
27
*
28
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
*
36
*/
37
38
#include <
gecode/minimodel.hh
>
39
#include "
test/int.hh
"
40
41
namespace
Test {
namespace
Int {
42
44
namespace
NValues {
45
51
52
class
IntInt
:
public
Test
{
53
protected
:
55
Gecode::IntRelType
irt
;
57
int
m
;
58
public
:
60
IntInt
(
int
n
,
int
m0,
Gecode::IntRelType
irt0)
61
:
Test
(
"NValues::Int::Int::"
+
str
(irt0)+
"::"
+
str
(n)+
"::"
+
str
(m0),
62
n,0,n),
63
irt
(irt0),
m
(m0) {
64
testfix
=
false
;
65
if
(
arity
> 5)
66
testsearch
=
false
;
67
}
69
virtual
Assignment
*
assignment
(
void
)
const
{
70
if
(
arity
> 5)
71
return
new
RandomAssignment
(
arity
,
dom
,500);
72
else
73
return
new
CpltAssignment
(
arity
,
dom
);
74
}
76
virtual
bool
solution
(
const
Assignment
&
x
)
const
{
77
int
n
= x.
size
();
78
bool
*
v
=
new
bool
[n+1];
79
for
(
int
i
=n+1;
i
--; )
80
v[
i
] =
false
;
81
int
k = 0;
82
for
(
int
i
=n;
i
--; )
83
if
(!v[x[
i
]]) {
84
k++;
85
v[x[
i
]] =
true
;
86
}
87
delete
[]
v
;
88
return
cmp
(k,
irt
,
m
);
89
}
91
virtual
void
post
(
Gecode::Space
& home,
Gecode::IntVarArray
&
x
) {
92
Gecode::nvalues
(home, x,
irt
,
m
);
93
}
94
};
95
97
class
IntVar
:
public
Test
{
98
protected
:
100
Gecode::IntRelType
irt
;
101
public
:
103
IntVar
(
int
n
,
Gecode::IntRelType
irt0)
104
:
Test
(
"NValues::Int::Var::"
+
str
(irt0)+
"::"
+
str
(n),n+1,0,n),
105
irt
(irt0) {
106
testfix
=
false
;
107
}
109
virtual
bool
solution
(
const
Assignment
&
x
)
const
{
110
int
n
= x.
size
() - 1;
111
bool
*
v
=
new
bool
[n+1];
112
for
(
int
i
=n+1;
i
--; )
113
v[
i
] =
false
;
114
int
k = 0;
115
for
(
int
i
=n;
i
--; )
116
if
(!v[x[
i
]]) {
117
k++;
118
v[x[
i
]] =
true
;
119
}
120
delete
[]
v
;
121
return
cmp
(k,
irt
,x[n]);
122
}
124
virtual
void
post
(
Gecode::Space
& home,
Gecode::IntVarArray
& xy) {
125
int
n
= xy.
size
() - 1;
126
Gecode::IntVarArgs
x
(n);
127
for
(
int
i
=n;
i
--; )
128
x[
i
] = xy[
i
];
129
Gecode::nvalues
(home, x,
irt
, xy[n]);
130
}
131
};
132
134
class
BoolInt
:
public
Test
{
135
protected
:
137
Gecode::IntRelType
irt
;
139
int
m
;
140
public
:
142
BoolInt
(
int
n
,
int
m0,
Gecode::IntRelType
irt0)
143
:
Test
(
"NValues::Bool::Int::"
+
str
(irt0)+
"::"
+
str
(n)+
"::"
+
str
(m0),
144
n,0,2),
145
irt
(irt0),
m
(m0) {}
147
virtual
bool
solution
(
const
Assignment
&
x
)
const
{
148
int
n
= x.
size
();
149
for
(
int
i
=n;
i
--; )
150
if
(x[
i
] > 1)
151
return
false
;
152
bool
*
v
=
new
bool
[n+1];
153
for
(
int
i
=n+1;
i
--; )
154
v[
i
] =
false
;
155
int
k = 0;
156
for
(
int
i
=n;
i
--; )
157
if
(!v[x[
i
]]) {
158
k++;
159
v[x[
i
]] =
true
;
160
}
161
delete
[]
v
;
162
return
cmp
(k,
irt
,
m
);
163
}
165
virtual
void
post
(
Gecode::Space
& home,
Gecode::IntVarArray
&
x
) {
166
using namespace
Gecode;
167
BoolVarArgs
y(x.
size
());
168
for
(
int
i
=x.
size
();
i
--; )
169
y[
i
] =
channel
(home, x[
i
]);
170
nvalues
(home, y,
irt
,
m
);
171
}
172
};
173
175
class
BoolVar
:
public
Test
{
176
protected
:
178
Gecode::IntRelType
irt
;
179
public
:
181
BoolVar
(
int
n
,
Gecode::IntRelType
irt0)
182
:
Test
(
"NValues::Bool::Var::"
+
str
(irt0)+
"::"
+
str
(n),n+1,0,2),
183
irt
(irt0) {}
185
virtual
bool
solution
(
const
Assignment
&
x
)
const
{
186
int
n
= x.
size
() - 1;
187
for
(
int
i
=n;
i
--; )
188
if
(x[
i
] > 1)
189
return
false
;
190
bool
*
v
=
new
bool
[n+1];
191
for
(
int
i
=n+1;
i
--; )
192
v[
i
] =
false
;
193
int
k = 0;
194
for
(
int
i
=n;
i
--; )
195
if
(!v[x[
i
]]) {
196
k++;
197
v[x[
i
]] =
true
;
198
}
199
delete
[]
v
;
200
return
cmp
(k,
irt
,x[n]);
201
}
203
virtual
void
post
(
Gecode::Space
& home,
Gecode::IntVarArray
& xy) {
204
using namespace
Gecode;
205
int
n
= xy.
size
() - 1;
206
BoolVarArgs
x
(
n
);
207
for
(
int
i
=
n
;
i
--; )
208
x[
i
] =
channel
(home, xy[
i
]);
209
nvalues
(home, x,
irt
, xy[
n
]);
210
}
211
};
212
214
class
Create
{
215
public
:
217
Create
(
void
) {
218
for
(
IntRelTypes
irts; irts(); ++irts) {
219
for
(
int
i
=1;
i
<=7;
i
+= 3) {
220
for
(
int
m
=0;
m
<=3;
m
++)
221
(
void
)
new
BoolInt
(
i
,
m
, irts.irt());
222
(void)
new
BoolVar
(
i
, irts.irt());
223
}
224
for
(
int
i
=1;
i
<=7;
i
+= 2) {
225
for
(
int
m
=0;
m
<=
i
+1;
m
++)
226
(
void
)
new
IntInt
(
i
,
m
, irts.irt());
227
if
(
i
<= 5)
228
(void)
new
IntVar
(
i
, irts.irt());
229
}
230
}
231
}
232
};
233
234
Create
c
;
236
237
}
238
}}
239
240
// STATISTICS: test-int
241