Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
13 kB · 383 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384/* -*- 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 <gecode/minimodel.hh>
#include "test/set.hh"
using namespace Gecode;
namespace Test { namespace Set {
/// %Tests for set element constraints namespace Element {
/** * \defgroup TaskTestSetElement Element constraints * \ingroup TaskTestSet */ //@{
static IntSet ds_12(-1,2); static IntSet ds_13(-1,3);
/// %Test for %ElementUnion constraint class ElementUnion : public SetTest { public: /// Create and register test ElementUnion(const char* t) : SetTest(t,5,ds_12,false) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { int selected = 0; for (CountableSetValues sel2(x.lub, x[3]); sel2(); ++sel2, selected++) {} CountableSetValues x4v(x.lub, x[4]); if (selected==0) return !x4v(); CountableSetRanges* sel = new CountableSetRanges[selected]; CountableSetValues selector(x.lub, x[3]); for (int i=selected; i--;++selector) { if (selector.val()>=3 || selector.val()<0) { delete[] sel; return false; } sel[i].init(x.lub, x[selector.val()]); }
bool ret; { Region r; Iter::Ranges::NaryUnion u(r, sel, selected);
CountableSetRanges z(x.lub, x[4]); ret = Iter::Ranges::equal(u, z); } delete[] sel; return ret; } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray&) { SetVarArgs xs(x.size()-2); for (int i=x.size()-2; i--;) xs[i]=x[i]; Gecode::element(home, SOT_UNION, xs, x[x.size()-2], x[x.size()-1]); } }; ElementUnion _elementunion("Element::Union");
/// %Test for %ElementUnionConst constraint class ElementUnionConst : public SetTest { private: const IntSet i0; const IntSet i1; const IntSet i2; public: /// Create and register test ElementUnionConst(const char* t) : SetTest(t,2,ds_13,false), i0(-3,-3), i1(-1,1), i2(0,2) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { int selected = 0; for (CountableSetValues sel2(x.lub, x[0]); sel2(); ++sel2, selected++) {} CountableSetValues x4v(x.lub, x[1]); if (selected==0) return !x4v(); IntSet iss[] = {i0, i1, i2}; IntSetRanges* sel = new IntSetRanges[selected]; CountableSetValues selector(x.lub, x[0]); for (int i=selected; i--;++selector) { if (selector.val()>=3 || selector.val()<0) { delete[] sel; return false; } sel[i].init(iss[selector.val()]); }
bool ret; { Region r; Iter::Ranges::NaryUnion u(r, sel, selected);
CountableSetRanges z(x.lub, x[1]); ret = Iter::Ranges::equal(u, z); } delete[] sel; return ret; } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray&) { IntSetArgs xs(3); xs[0] = i0; xs[1] = i1; xs[2] = i2; Gecode::element(home, SOT_UNION, xs, x[0], x[1]); } }; ElementUnionConst _elementunionconst("Element::UnionConst");
/// %Test for %ElementInter constraint class ElementInter : public SetTest { public: /// Create and register test ElementInter(const char* t) : SetTest(t,5,ds_12,false) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { int selected = 0; for (CountableSetValues sel2(x.lub, x[3]); sel2(); ++sel2, selected++) {} CountableSetRanges x4r(x.lub, x[4]); if (selected==0) return Iter::Ranges::size(x4r)==Gecode::Set::Limits::card; CountableSetRanges* sel = new CountableSetRanges[selected]; CountableSetValues selector(x.lub, x[3]); for (int i=selected; i--;++selector) { if (selector.val()>=3 || selector.val()<0) { delete[] sel; return false; } sel[i].init(x.lub, x[selector.val()]); } bool ret; { Region r; Iter::Ranges::NaryInter u(r, sel, selected);
CountableSetRanges z(x.lub, x[4]); ret = Iter::Ranges::equal(u, z); } delete [] sel; return ret; } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray&) { SetVarArgs xs(x.size()-2); for (int i=x.size()-2; i--;) xs[i]=x[i]; Gecode::element(home, SOT_INTER, xs, x[x.size()-2], x[x.size()-1]); } }; ElementInter _elementinter("Element::Inter");
/// %Test for %ElementInter constraint class ElementInterIn : public SetTest { public: /// Create and register test ElementInterIn(const char* t) : SetTest(t,5,ds_12,false) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { int selected = 0; for (CountableSetValues sel2(x.lub, x[3]); sel2(); ++sel2, selected++) {} CountableSetRanges x4r(x.lub, x[4]); if (selected==0) return Iter::Ranges::size(x4r)==4; CountableSetRanges* sel = new CountableSetRanges[selected]; CountableSetValues selector(x.lub, x[3]); for (int i=selected; i--;++selector) { if (selector.val()>=3 || selector.val()<0) { delete[] sel; return false; } sel[i].init(x.lub, x[selector.val()]); } bool ret; { Region r; Iter::Ranges::NaryInter u(r,sel, selected);
CountableSetRanges z(x.lub, x[4]); ret = Iter::Ranges::equal(u, z); } delete [] sel; return ret; } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray&) { SetVarArgs xs(x.size()-2); for (int i=x.size()-2; i--;) xs[i]=x[i]; Gecode::element(home, SOT_INTER, xs, x[x.size()-2], x[x.size()-1], ds_12); } }; ElementInterIn _elementinterin("Element::InterIn");
/// %Test for %ElementDisjoint constraint class ElementDisjoint : public SetTest { public: /// Create and register test ElementDisjoint(const char* t) : SetTest(t,5,ds_12,false) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { int selected = 0; for (CountableSetValues sel2(x.lub, x[3]); sel2(); ++sel2, selected++) { if (sel2.val() < 0) return false; } CountableSetValues x4v(x.lub, x[4]); if (selected == 0) return !x4v(); CountableSetRanges* sel = new CountableSetRanges[selected]; CountableSetValues selector(x.lub, x[3]); unsigned int cardsum = 0; for (int i=selected; i--;++selector) { if (selector.val()>=3 || selector.val()<0) { delete[] sel; return false; } sel[i].init(x.lub, x[selector.val()]); CountableSetRanges xicard(x.lub, x[selector.val()]); cardsum += Iter::Ranges::size(xicard); }
bool ret; { Region r; Iter::Ranges::NaryUnion u(r, sel, selected); ret = Iter::Ranges::size(u) == cardsum; u.reset(); CountableSetRanges z(x.lub, x[4]); ret &= Iter::Ranges::equal(u, z); } delete[] sel; return ret; } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray&) { SetVarArgs xs(x.size()-2); for (int i=x.size()-2; i--;) xs[i]=x[i]; Gecode::element(home, SOT_DUNION, xs, x[x.size()-2], x[x.size()-1]); } }; ElementDisjoint _elementdisjoint("Element::Disjoint");
/// %Test for %ElementSet constraint class ElementSet : public SetTest { public: /// Create and register test ElementSet(const char* t) : SetTest(t,4,ds_12,false,true) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { if (x.intval() < 0 || x.intval() > 2) return false; CountableSetRanges z(x.lub, x[3]); CountableSetRanges y(x.lub, x[x.intval()]); return Iter::Ranges::equal(y, z); } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray& y) { SetVarArgs xs(x.size()-1); for (int i=x.size()-1; i--;) xs[i]=x[i]; Gecode::element(home, xs, y[0], x[x.size()-1]); } }; ElementSet _elementset("Element::Set");
/// %Test for %ElementSetConst constraint class ElementSetConst : public SetTest { private: const IntSet i0; const IntSet i1; const IntSet i2; public: /// Create and register test ElementSetConst(const char* t) : SetTest(t,1,ds_13,false,true), i0(-3,-3), i1(-1,1), i2(0,2) {} /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { if (x.intval() < 0 || x.intval() > 2) return false; CountableSetRanges xr(x.lub, x[0]); IntSet iss[] = {i0, i1, i2}; IntSetRanges isr(iss[x.intval()]); return Iter::Ranges::equal(xr, isr); } /// Post constraint on \a x virtual void post(Space& home, SetVarArray& x, IntVarArray& y) { IntSetArgs xs(3); xs[0] = i0; xs[1] = i1; xs[2] = i2; Gecode::element(home, xs, y[0], x[0]); } }; ElementSetConst _elementsetconst("Element::SetConst");
/// %Test for matrix element with integer set array and set variable class MatrixIntSet : public SetTest { protected: /// Array for test matrix Gecode::IntSetArgs tm; public: /// Create and register test MatrixIntSet(void) : SetTest("Element::Matrix::IntSet",1,IntSet(0,3),false,2), tm(4) { tm[0]=IntSet(0,0); tm[1]=IntSet(1,1); tm[2]=IntSet(2,2); tm[3]=IntSet(3,3); } /// %Test whether \a x is solution virtual bool solution(const SetAssignment& x) const { // Get integer assignment const Int::Assignment& y = x.ints(); // x-coordinate: y[0], y-coordinate: y[1], result: x[0] using namespace Gecode; if ((y[0] > 1) || (y[1] > 1)) return false; Matrix<IntSetArgs> m(tm,2,2); IntSetRanges a(m(y[0],y[1])); CountableSetRanges b(x.lub, x[0]); return Iter::Ranges::equal(a,b); } /// Post constraint on \a x and \a y virtual void post(Gecode::Space& home, Gecode::SetVarArray& x, Gecode::IntVarArray& y) { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] using namespace Gecode; Matrix<IntSetArgs> m(tm,2,2); element(home, m, y[0], y[1], x[0]); } };
MatrixIntSet _emis;
//@}
}}}
// STATISTICS: test-set