/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 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/int.hh" #include namespace Test { namespace Int { /// %Tests for relation constraints namespace Rel { /** * \defgroup TaskTestIntRel Relation constraints * \ingroup TaskTestInt */ //@{ /// %Test for simple relation involving integer variables class IntVarXY : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntVarXY(Gecode::IntRelType irt0, int n, Gecode::IntPropLevel ipl) : Test("Rel::Int::Var::XY::"+str(irt0)+"::"+str(ipl)+"::"+str(n), n+1,-3,3,n==1,ipl), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { if (x.size() == 2) { return cmp(x[0],irt,x[1]); } else { return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]); } } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; if (x.size() == 2) { rel(home, x[0], irt, x[1], ipl); } else { IntVarArgs y(2); y[0]=x[0]; y[1]=x[1]; rel(home, y, irt, x[2], ipl); } } /// Post reified constraint on \a x for \a r virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, Gecode::Reify r) { assert(x.size() == 2); Gecode::rel(home, x[0], irt, x[1], r, ipl); } }; /// %Test for simple relation involving shared integer variables class IntVarXX : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntVarXX(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl) : Test("Rel::Int::Var::XX::"+str(irt0)+"::"+str(ipl), 1,-3,3,true,ipl), irt(irt0) { contest = ((irt != Gecode::IRT_LE) && (irt != Gecode::IRT_GR) && (irt != Gecode::IRT_NQ)) ? CTL_DOMAIN : CTL_NONE; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return cmp(x[0],irt,x[0]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::rel(home, x[0], irt, x[0], ipl); } /// Post reified constraint on \a x for \a r virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, Gecode::Reify r) { Gecode::rel(home, x[0], irt, x[0], r, ipl); } }; /// %Test for simple relation involving Boolean variables class BoolVarXY : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test BoolVarXY(Gecode::IntRelType irt0, int n) : Test("Rel::Bool::Var::XY::"+str(irt0)+"::"+str(n),n+1,0,1, n==1), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { if (x.size() == 2) { return cmp(x[0],irt,x[1]); } else { return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]); } } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; if (x.size() == 2) { rel(home, channel(home,x[0]), irt, channel(home,x[1])); } else { BoolVarArgs y(2); y[0]=channel(home,x[0]); y[1]=channel(home,x[1]); rel(home, y, irt, channel(home,x[2])); } } /// Post reified constraint on \a x for \a r virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, Gecode::Reify r) { assert(x.size() == 2); using namespace Gecode; rel(home, channel(home,x[0]), irt, channel(home,x[1]), r); } }; /// %Test for simple relation involving shared Boolean variables class BoolVarXX : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test BoolVarXX(Gecode::IntRelType irt0) : Test("Rel::Bool::Var::XX::"+str(irt0),1,0,1), irt(irt0) { contest = ((irt != Gecode::IRT_LE) && (irt != Gecode::IRT_GR) && (irt != Gecode::IRT_NQ)) ? CTL_DOMAIN : CTL_NONE; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return cmp(x[0],irt,x[0]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::BoolVar b = Gecode::channel(home,x[0]); Gecode::rel(home, b, irt, b); } }; /// %Test for simple relation involving integer variable and integer constant class IntInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; /// Integer constant int c; public: /// Create and register test IntInt(Gecode::IntRelType irt0, int n, int c0) : Test("Rel::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0), n,-3,3,n==1), irt(irt0), c(c0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { if (x.size() == 1) return cmp(x[0],irt,c); else return cmp(x[0],irt,c) && cmp(x[1],irt,c); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; if (x.size() == 1) rel(home, x[0], irt, c); else rel(home, x, irt, c); } /// Post reified constraint on \a x for \a r virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, Gecode::Reify r) { assert(x.size() == 1); Gecode::rel(home, x[0], irt, c, r); } }; /// %Test for simple relation involving Boolean variable and integer constant class BoolInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; /// Integer constant int c; public: /// Create and register test BoolInt(Gecode::IntRelType irt0, int n, int c0) : Test("Rel::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),n,0,1, n==1), irt(irt0), c(c0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { if (x.size() == 1) return cmp(x[0],irt,c); else return cmp(x[0],irt,c) && cmp(x[1],irt,c); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; if (x.size() == 1) { rel(home, channel(home,x[0]), irt, c); } else { BoolVarArgs y(2); y[0]=channel(home,x[0]); y[1]=channel(home,x[1]); rel(home, y, irt, c); } } /// Post reified constraint on \a x for \a r virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, Gecode::Reify r) { assert(x.size() == 1); using namespace Gecode; rel(home, channel(home,x[0]), irt, c, r); } }; /// %Test for sequence of relations between integer variables class IntSeq : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntSeq(int n, Gecode::IntRelType irt0, Gecode::IntPropLevel ipl) : Test("Rel::Int::Seq::"+str(n)+"::"+str(irt0)+"::"+str(ipl), n,-3,3,false,ipl), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { if (irt == Gecode::IRT_NQ) { if (x.size() < 2) return false; for (int i=0; i> 1; for (int i=0; i> 1; IntVarArgs y(n); IntVarArgs z(n); for (int i=0; i> 1; for (int i=0; i> 1; BoolVarArgs y(n); BoolVarArgs z(n); for (int i=0; i