/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2005 * * Last modified: * $Date: 2010-04-08 20:35:31 +1000 (Thu, 08 Apr 2010) $ by $Author: schulte $ * $Revision: 10684 $ * * 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 #include namespace Test { namespace Int { /// %Tests for element constraints namespace Element { /** * \defgroup TaskTestIntElement Element constraints * \ingroup TaskTestInt */ //@{ /// %Test for element with integer array and integer variables class IntIntVar : public Test { protected: /// Array of integers Gecode::IntArgs c; public: /// Create and register test IntIntVar(const std::string& s, const Gecode::IntArgs& c0, int min, int max) : Test("Element::Int::Int::Var::"+s,2,min,max), c(c0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[0]>= 0) && (x[0]= 0) && (x[0]= 0) && (x[0]= 0) && (x[0]= 0) && (x[0]= 0) && (x[0]= 0) && (x[0]= 0) && (x[0]1)) return false; return ((x[0]>= 0) && (x[0]=0) && (x[1]<=1)); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVarArgs c(x.size()-2); for (int i=0; i1)) return false; return ((x[0]>= 0) && (x[0] 2) || (x[1] > 1)) return false; Matrix m(tm,3,2); return m(x[0],x[1]) == x[2]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] using namespace Gecode; Matrix m(tm,3,2); element(home, m, x[0], x[1], x[2]); } }; /// %Test for matrix element with integer array and integer variable class MatrixIntIntVarXX : public Test { protected: /// Array for test matrix Gecode::IntArgs tm; public: /// Create and register test MatrixIntIntVarXX(void) : Test("Element::Matrix::Int::IntVar::XX",2,0,3,false), tm(4, 0,1,2,3) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[0], result: x[1] using namespace Gecode; if (x[0] > 1) return false; Matrix m(tm,2,2); return m(x[0],x[0]) == x[1]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[0], result: x[1] using namespace Gecode; Matrix m(tm,2,2); element(home, m, x[0], x[0], x[1]); } }; /// %Test for matrix element with integer array and Boolean variable class MatrixIntBoolVarXY : public Test { protected: /// Array for test matrix Gecode::IntArgs tm; public: /// Create and register test MatrixIntBoolVarXY(void) : Test("Element::Matrix::Int::BoolVar::XY",3,0,3,false), tm(4, 0,1,1,0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] using namespace Gecode; if ((x[0] > 1) || (x[1] > 1)) return false; Matrix m(tm,2,2); return m(x[0],x[1]) == x[2]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] using namespace Gecode; Matrix m(tm,2,2); element(home, m, x[0], x[1], channel(home,x[2])); } }; /// %Test for matrix element with integer array and Boolean variable class MatrixIntBoolVarXX : public Test { protected: /// Array for test matrix Gecode::IntArgs tm; public: /// Create and register test MatrixIntBoolVarXX(void) : Test("Element::Matrix::Int::BoolVar::XX",2,0,3,false), tm(4, 0,1,1,0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[0], result: x[1] using namespace Gecode; if (x[0] > 1) return false; Matrix m(tm,2,2); return m(x[0],x[0]) == x[1]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[0], result: x[1] using namespace Gecode; Matrix m(tm,2,2); element(home, m, x[0], x[0], channel(home,x[1])); } }; /// %Test for matrix element with integer variable array and integer variable class MatrixIntVarIntVarXY : public Test { public: /// Create and register test MatrixIntVarIntVarXY(void) : Test("Element::Matrix::IntVar::IntVar::XY",3+4,0,3,false) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] // remaining: matrix using namespace Gecode; if ((x[0] > 1) || (x[1] > 1)) return false; IntArgs tm(4); tm[0]=x[3]; tm[1]=x[4]; tm[2]=x[5]; tm[3]=x[6]; Matrix m(tm,2,2); return m(x[0],x[1]) == x[2]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] using namespace Gecode; IntVarArgs tm(4); tm[0]=x[3]; tm[1]=x[4]; tm[2]=x[5]; tm[3]=x[6]; Matrix m(tm,2,2); element(home, m, x[0], x[1], x[2]); } }; /// %Test for matrix element with integer variable array and integer variable class MatrixIntVarIntVarXX : public Test { public: /// Create and register test MatrixIntVarIntVarXX(void) : Test("Element::Matrix::IntVar::IntVar::XX",2+4,0,3,false) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[0], result: x[1] // remaining: matrix using namespace Gecode; if (x[0] > 1) return false; IntArgs tm(4); tm[0]=x[2]; tm[1]=x[3]; tm[2]=x[4]; tm[3]=x[5]; Matrix m(tm,2,2); return m(x[0],x[0]) == x[1]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[1], result: x[1] using namespace Gecode; IntVarArgs tm(4); tm[0]=x[2]; tm[1]=x[3]; tm[2]=x[4]; tm[3]=x[5]; Matrix m(tm,2,2); element(home, m, x[0], x[0], x[1]); } }; /// %Test for matrix element with Boolean variable array and Boolean variable class MatrixBoolVarBoolVarXY : public Test { public: /// Create and register test MatrixBoolVarBoolVarXY(void) : Test("Element::Matrix::BoolVar::BoolVar::XY",3+4,0,1,false) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] // remaining: matrix using namespace Gecode; IntArgs tm(4); tm[0]=x[3]; tm[1]=x[4]; tm[2]=x[5]; tm[3]=x[6]; Matrix m(tm,2,2); return m(x[0],x[1]) == x[2]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[1], result: x[2] using namespace Gecode; BoolVarArgs tm(4); tm[0]=channel(home,x[3]); tm[1]=channel(home,x[4]); tm[2]=channel(home,x[5]); tm[3]=channel(home,x[6]); Matrix m(tm,2,2); element(home, m, x[0], x[1], channel(home,x[2])); } }; /// %Test for matrix element with Boolean variable array and Boolean variable class MatrixBoolVarBoolVarXX : public Test { public: /// Create and register test MatrixBoolVarBoolVarXX(void) : Test("Element::Matrix::BoolVar::BoolVar::XX",2+4,0,1,false) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { // x-coordinate: x[0], y-coordinate: x[0], result: x[1] // remaining: matrix using namespace Gecode; IntArgs tm(4); tm[0]=x[2]; tm[1]=x[3]; tm[2]=x[4]; tm[3]=x[5]; Matrix m(tm,2,2); return m(x[0],x[0]) == x[1]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { // x-coordinate: x[0], y-coordinate: x[1], result: x[1] using namespace Gecode; BoolVarArgs tm(4); tm[0]=channel(home,x[2]); tm[1]=channel(home,x[3]); tm[2]=channel(home,x[4]); tm[3]=channel(home,x[5]); Matrix m(tm,2,2); element(home, m, x[0], x[0], channel(home,x[1])); } }; /// Help class to create and register tests class Create { public: /// %Test size-dependent optimizations void optimized(int idx, int val) { Gecode::IntArgs c(idx); for (int i=0; i