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
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2011
*
* 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 "test/int.hh"
namespace Test { namespace Int {
/// %Tests for number of values constraints
namespace NValues {
/**
* \defgroup TaskTestIntNValues Number of values constraints
* \ingroup TaskTestInt
*/
//@{
/// %Test number of values of integer variables equal to integer
class IntInt : public Test {
protected:
/// Integer relation type to propagate
Gecode::IntRelType irt;
/// Number of values
int m;
public:
/// Create and register test
IntInt(int n, int m0, Gecode::IntRelType irt0)
: Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
n,0,n),
irt(irt0), m(m0) {
testfix = false;
if (arity > 5)
testsearch = false;
}
/// Create and register initial assignment
virtual Assignment* assignment(void) const {
if (arity > 5)
return new RandomAssignment(arity, dom, 500, _rand);
else
return new CpltAssignment(arity,dom);
}
/// %Test whether \a x is solution
virtual bool solution(const Assignment& x) const {
int n = x.size();
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,m);
}
/// Post constraint on \a x
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::nvalues(home, x, irt, m);
}
};
/// %Test number of values of integer variables equal to integer variable
class IntVar : public Test {
protected:
/// Integer relation type to propagate
Gecode::IntRelType irt;
public:
/// Create and register test
IntVar(int n, Gecode::IntRelType irt0)
: Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n),
irt(irt0) {
testfix = false;
}
/// %Test whether \a x is solution
virtual bool solution(const Assignment& x) const {
int n = x.size() - 1;
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,x[n]);
}
/// Post constraint on \a xy
virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
int n = xy.size() - 1;
Gecode::IntVarArgs x(n);
for (int i=n; i--; )
x[i] = xy[i];
Gecode::nvalues(home, x, irt, xy[n]);
}
};
/// %Test number of values of Boolean variables equal to integer
class BoolInt : public Test {
protected:
/// Integer relation type to propagate
Gecode::IntRelType irt;
/// Number of values
int m;
public:
/// Create and register test
BoolInt(int n, int m0, Gecode::IntRelType irt0)
: Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
n,0,2),
irt(irt0), m(m0) {}
/// %Test whether \a x is solution
virtual bool solution(const Assignment& x) const {
int n = x.size();
for (int i=n; i--; )
if (x[i] > 1)
return false;
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,m);
}
/// Post constraint on \a x
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
BoolVarArgs y(x.size());
for (int i=x.size(); i--; )
y[i] = channel(home, x[i]);
nvalues(home, y, irt, m);
}
};
/// %Test number of values of Boolean variables equal to integer variable
class BoolVar : public Test {
protected:
/// Integer relation type to propagate
Gecode::IntRelType irt;
public:
/// Create and register test
BoolVar(int n, Gecode::IntRelType irt0)
: Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2),
irt(irt0) {}
/// %Test whether \a x is solution
virtual bool solution(const Assignment& x) const {
int n = x.size() - 1;
for (int i=n; i--; )
if (x[i] > 1)
return false;
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,x[n]);
}
/// Post constraint on \a xy
virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
using namespace Gecode;
int n = xy.size() - 1;
BoolVarArgs x(n);
for (int i=n; i--; )
x[i] = channel(home, xy[i]);
nvalues(home, x, irt, xy[n]);
}
};
/// Help class to create and register tests
class Create {
public:
/// Perform creation and registration
Create(void) {
for (IntRelTypes irts; irts(); ++irts) {
for (int i=1; i<=7; i += 3) {
for (int m=0; m<=3; m++)
(void) new BoolInt(i, m, irts.irt());
(void) new BoolVar(i, irts.irt());
}
for (int i=1; i<=7; i += 2) {
for (int m=0; m<=i+1; m++)
(void) new IntInt(i, m, irts.irt());
if (i <= 5)
(void) new IntVar(i, irts.irt());
}
}
}
};
Create c;
//@}
}
}}
// STATISTICS: test-int
|