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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Guido Tack, 2005
*
* 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 "test/set.hh"
#include "test/int.hh"
#include <gecode/minimodel.hh>
using namespace Gecode;
namespace Test { namespace Set {
/// %Tests for combined int/set constraints
namespace Int {
/**
* \defgroup TaskTestSetInt Combined integer/set constraints
* \ingroup TaskTestSet
*/
//@{
static const int d1r[4][2] = {
{-4,-3},{-1,-1},{1,1},{3,5}
};
static IntSet d1(d1r,4);
static IntSet d2(-1,3);
static IntSet d3(0,3);
static IntSet d4(0,4);
static IntSet ds_33(-3,3);
/// %Test for cardinality constraint
class Card : public SetTest {
public:
/// Create and register test
Card(const char* t)
: SetTest(t,1,ds_33,true,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
unsigned int s = 0;
for (CountableSetRanges xr(x.lub, x[0]);xr();++xr) s+= xr.width();
if (x.intval() < 0)
return false;
return s==(unsigned int)x.intval();
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::cardinality(home, x[0], y[0]);
}
/// Post reified constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
Reify r) {
Gecode::cardinality(home, x[0], y[0], r);
}
};
Card _card("Int::Card");
/// %Test for minimal element constraint
class Min : public SetTest {
public:
/// Create and register test
Min(const char* t)
: SetTest(t,1,ds_33,true,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetRanges xr0(x.lub, x[0]);
return xr0() && xr0.min()==x.intval();
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::min(home, x[0], y[0]);
}
/// Post reified constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
Reify r) {
Gecode::min(home, x[0], y[0], r);
}
};
Min _min("Int::Min");
/// %Test for negated minimal element constraint
class NotMin : public SetTest {
public:
/// Create and register test
NotMin(const char* t)
: SetTest(t,1,ds_33,false,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetRanges xr0(x.lub, x[0]);
return !(xr0() && xr0.min()==x.intval());
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::notMin(home, x[0], y[0]);
}
};
NotMin _notmin("Int::NotMin");
/// %Test for maximal element constraint
class Max : public SetTest {
public:
/// Create and register test
Max(const char* t)
: SetTest(t,1,ds_33,true,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetRanges xr0(x.lub, x[0]);
IntSet x0(xr0);
return x0.ranges() > 0 && x0.max()==x.intval();
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::max(home, x[0], y[0]);
}
/// Post reified constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
Reify r) {
Gecode::max(home, x[0], y[0], r);
}
};
Max _max("Int::Max");
/// %Test for negated maximal element constraint
class NotMax : public SetTest {
public:
/// Create and register test
NotMax(const char* t)
: SetTest(t,1,ds_33,false,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetRanges xr0(x.lub, x[0]);
IntSet x0(xr0);
return !(x0.ranges() > 0 && x0.max()==x.intval());
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::notMax(home, x[0], y[0]);
}
};
NotMax _notmax("Int::NotMax");
/// %Test for element constraint
class Elem : public SetTest {
public:
/// Create and register test
Elem(const char* t)
: SetTest(t,1,ds_33,true,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
for (CountableSetValues xr(x.lub, x[0]);xr();++xr)
if (xr.val()==x.intval())
return true;
return false;
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::rel(home, x[0], SRT_SUP, y[0]);
}
/// Post reified constraint on \a x for \a b
virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
Reify r) {
Gecode::rel(home, x[0], SRT_SUP, y[0], r);
}
};
Elem _elem("Int::Elem");
/// %Test for negated element constraint
class NoElem : public SetTest {
public:
/// Create and register test
NoElem(const char* t)
: SetTest(t,1,ds_33,false,1) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
for (CountableSetValues xr(x.lub, x[0]);xr();++xr)
if (xr.val()==x.intval())
return false;
return true;
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::rel(home, x[0], SRT_DISJ, y[0]);
}
};
NoElem _noelem("Int::NoElem");
/// %Test for relation constraint
class Rel : public SetTest {
private:
/// The set relation type
Gecode::SetRelType srt;
/// Whether relation is swapped
bool swapped;
public:
/// Create and register test
Rel(Gecode::SetRelType srt0, bool s)
: SetTest("Int::Rel::"+str(srt0)+(s ? "::s" : ""),
1,ds_33,true,1),
srt(srt0), swapped(s) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetRanges xr(x.lub, x[0]);
IntSet is(x.intval(), x.intval());
IntSetRanges dr(is);
Gecode::SetRelType rsrt = srt;
if (swapped) {
switch (srt) {
case SRT_SUB: rsrt = SRT_SUP; break;
case SRT_SUP: rsrt = SRT_SUB; break;
default: break;
}
}
switch (rsrt) {
case SRT_EQ: return Iter::Ranges::equal(xr, dr);
case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
case SRT_SUB: return Iter::Ranges::subset(xr, dr);
case SRT_SUP: return Iter::Ranges::subset(dr, xr);
case SRT_DISJ:
{
Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
inter(xr, dr);
return !inter();
}
case SRT_CMPL:
{
Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
return Iter::Ranges::equal(xr,drc);
}
default: GECODE_NEVER;
}
return false;
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
if (!swapped)
Gecode::rel(home, x[0], srt, y[0]);
else
Gecode::rel(home, y[0], srt, x[0]);
}
/// Post reified constraint on \a x for \a r
virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
Reify r) {
if (!swapped)
Gecode::rel(home, x[0], srt, y[0], r);
else
Gecode::rel(home, y[0], srt, x[0], r);
}
};
Rel _rel_eq(Gecode::SRT_EQ,false);
Rel _rel_nq(Gecode::SRT_NQ,false);
Rel _rel_sub(Gecode::SRT_SUB,false);
Rel _rel_sup(Gecode::SRT_SUP,false);
Rel _rel_disj(Gecode::SRT_DISJ,false);
Rel _rel_cmpl(Gecode::SRT_CMPL,false);
Rel _rel_eqs(Gecode::SRT_EQ,true);
Rel _rel_nqs(Gecode::SRT_NQ,true);
Rel _rel_subs(Gecode::SRT_SUB,true);
Rel _rel_sups(Gecode::SRT_SUP,true);
Rel _rel_disjs(Gecode::SRT_DISJ,true);
Rel _rel_cmpls(Gecode::SRT_CMPL,true);
/// %Test for integer relation constraint
class IntRel : public SetTest {
private:
/// The integer relation type
Gecode::IntRelType irt;
/// Whether the arguments are swapped
bool swapped;
public:
/// Create and register test
IntRel(Gecode::IntRelType irt0, bool s)
: SetTest("Int::IntRel::"+Test::Int::Test::str(irt0)+
(s ? "::s" : ""),1,ds_33,true,1),
irt(irt0), swapped(s) {
testsubsumed = false;
}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetValues xr(x.lub, x[0]);
if (!xr())
return false;
for (; xr(); ++xr)
switch (irt) {
case Gecode::IRT_EQ:
if (xr.val() != x.intval()) return false;
break;
case Gecode::IRT_NQ:
if (xr.val() == x.intval()) return false;
break;
case Gecode::IRT_GR:
if (!swapped && xr.val() <= x.intval()) return false;
if (swapped && xr.val() >= x.intval()) return false;
break;
case Gecode::IRT_GQ:
if (!swapped && xr.val() < x.intval()) return false;
if (swapped && xr.val() > x.intval()) return false;
break;
case Gecode::IRT_LE:
if (!swapped && xr.val() >= x.intval()) return false;
if (swapped && xr.val() <= x.intval()) return false;
break;
case Gecode::IRT_LQ:
if (!swapped && xr.val() > x.intval()) return false;
if (swapped && xr.val() < x.intval()) return false;
break;
default:
GECODE_NEVER;
return false;
}
return true;
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
if (!swapped)
Gecode::rel(home, x[0], irt, y[0]);
else
Gecode::rel(home, y[0], irt, x[0]);
}
/// Post reified constraint on \a x for \a r
virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
Reify r) {
assert((x.size() == 1) && (y.size() == 1));
if ((r.mode() != Gecode::RM_EQV) || (_rand(2) != 0)) {
if (!swapped)
Gecode::rel(home, x[0], irt, y[0], r);
else
Gecode::rel(home, y[0], irt, x[0], r);
} else if (swapped) {
switch (irt) {
case Gecode::IRT_EQ:
Gecode::rel(home, (y[0] == x[0]) == r.var()); break;
case Gecode::IRT_NQ:
Gecode::rel(home, (y[0] != x[0]) == r.var()); break;
case Gecode::IRT_LE:
Gecode::rel(home, (y[0] < x[0]) == r.var()); break;
case Gecode::IRT_LQ:
Gecode::rel(home, (y[0] <= x[0]) == r.var()); break;
case Gecode::IRT_GR:
Gecode::rel(home, (y[0] > x[0]) == r.var()); break;
case Gecode::IRT_GQ:
Gecode::rel(home, (y[0] >= x[0]) == r.var()); break;
default: GECODE_NEVER;
}
} else {
switch (irt) {
case Gecode::IRT_EQ:
Gecode::rel(home, (x[0] == y[0]) == r.var()); break;
case Gecode::IRT_NQ:
Gecode::rel(home, (x[0] != y[0]) == r.var()); break;
case Gecode::IRT_LE:
Gecode::rel(home, (x[0] < y[0]) == r.var()); break;
case Gecode::IRT_LQ:
Gecode::rel(home, (x[0] <= y[0]) == r.var()); break;
case Gecode::IRT_GR:
Gecode::rel(home, (x[0] > y[0]) == r.var()); break;
case Gecode::IRT_GQ:
Gecode::rel(home, (x[0] >= y[0]) == r.var()); break;
default: GECODE_NEVER;
}
}
}
};
IntRel _intrel_eq(Gecode::IRT_EQ,false);
IntRel _intrel_nq(Gecode::IRT_NQ,false);
IntRel _intrel_gr(Gecode::IRT_GR,false);
IntRel _intrel_gq(Gecode::IRT_GQ,false);
IntRel _intrel_le(Gecode::IRT_LE,false);
IntRel _intrel_lq(Gecode::IRT_LQ,false);
IntRel _intrel_eqs(Gecode::IRT_EQ,true);
IntRel _intrel_nqs(Gecode::IRT_NQ,true);
IntRel _intrel_grs(Gecode::IRT_GR,true);
IntRel _intrel_gqs(Gecode::IRT_GQ,true);
IntRel _intrel_les(Gecode::IRT_LE,true);
IntRel _intrel_lqs(Gecode::IRT_LQ,true);
template<class I>
int weightI(const IntArgs& elements,
const IntArgs& weights,
I& iter) {
int sum = 0;
int i = 0;
for (Iter::Ranges::ToValues<I> v(iter); v(); ++v) {
// Skip all elements below the current
while (elements[i]<v.val()) i++;
assert(elements[i] == v.val());
sum += weights[i];
}
return sum;
}
/// %Test for set weight constraint
class Weights : public SetTest {
public:
IntArgs elements;
IntArgs weights;
int minWeight;
int maxWeight;
/// Create and register test
Weights(const char* t, IntArgs& el, IntArgs& w,
int min = -10000, int max = 10000)
: SetTest(t,1,ds_33,false,1),
elements(el), weights(w), minWeight(min), maxWeight(max) {}
/// %Test whether \a x is solution
virtual bool solution(const SetAssignment& x) const {
CountableSetRanges x0(x.lub, x[0]);
return x.intval()==weightI(elements,weights,x0) &&
x.intval() >= minWeight && x.intval() <= maxWeight;
}
/// Post constraint on \a x
virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
Gecode::rel(home, minWeight <= y[0]);
Gecode::rel(home, maxWeight >= y[0]);
Gecode::weights(home, elements, weights, x[0], y[0]);
}
};
const int el1v[] = {-3,-2,-1,0,1,2,3};
IntArgs el1(7,el1v);
const int w1v[] = {1,-2,1,1,1,6,1};
IntArgs w1(7,w1v);
Weights _weights1("Int::Weights::1", el1, w1);
const int w2v[] = {-1,-1,-1,10,-1,-1,-1};
IntArgs w2(7,w2v);
Weights _weights2("Int::Weights::2", el1, w2);
Weights _weights3("Int::Weights::3", el1, w2, 3);
const int w4v[] = {1,1,0,0,0,0,0};
IntArgs w4(7,w4v);
Weights _weights4("Int::Weights::4", el1, w4);
}}}
// STATISTICS: test-set
|