Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
9.5 kB · 291 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Copyright: * Christian Schulte, 2008 * * 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 <gecode/minimodel.hh>
namespace Test { namespace Int {
/// %Tests for minimal modelling constraints (counting) namespace MiniModelCount {
/// Expand relation to abbreviation std::string expand(Gecode::IntRelType irt) { switch (irt) { case Gecode::IRT_EQ: return "Exactly"; case Gecode::IRT_LQ: return "AtMost"; case Gecode::IRT_GQ: return "AtLeast"; default: GECODE_NEVER; } GECODE_NEVER; return ""; }
/** * \defgroup TaskTestIntMiniModelCount Minimal modelling constraints (counting) * \ingroup TaskTestInt */ //@{ /// %Test number of equal integers equal to integer class IntInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntInt(Gecode::IntRelType irt0) : Test("MiniModel::"+expand(irt0)+"::Int::Int",4,-2,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int m = 0; for (int i=x.size(); i--; ) if (x[i] == 0) m++; return cmp(m,irt,2); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { switch (irt) { case Gecode::IRT_EQ: Gecode::exactly(home,x,0,2); break; case Gecode::IRT_LQ: Gecode::atmost(home,x,0,2); break; case Gecode::IRT_GQ: Gecode::atleast(home,x,0,2); break; default: GECODE_NEVER; } } };
/// %Test number of equal integers equal to integer variable class IntVar : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntVar(Gecode::IntRelType irt0) : Test("MiniModel::"+expand(irt0)+"::Int::Var",5,-2,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int m = 0; for (int i=0; i<4; i++) if (x[i] == 0) m++; return cmp(m,irt,x[4]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::IntVarArgs y(4); for (int i=0; i<4; i++) y[i]=x[i]; switch (irt) { case Gecode::IRT_EQ: Gecode::exactly(home,y,0,x[4]); break; case Gecode::IRT_LQ: Gecode::atmost(home,y,0,x[4]); break; case Gecode::IRT_GQ: Gecode::atleast(home,y,0,x[4]); break; default: GECODE_NEVER; } } };
/// %Test number of equal variables equal to integer variable class VarVar : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test VarVar(Gecode::IntRelType irt0) : Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int m = 0; for (int i=0; i<3; i++) if (x[i] == x[3]) m++; return cmp(m,irt,x[4]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::IntVarArgs y(3); for (int i=0; i<3; i++) y[i]=x[i]; switch (irt) { case Gecode::IRT_EQ: Gecode::exactly(home,y,x[3],x[4]); break; case Gecode::IRT_LQ: Gecode::atmost(home,y,x[3],x[4]); break; case Gecode::IRT_GQ: Gecode::atleast(home,y,x[3],x[4]); break; default: GECODE_NEVER; } } };
/// %Test number of equal variables equal to integer class VarInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test VarInt(Gecode::IntRelType irt0) : Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int m = 0; for (int i=0; i<3; i++) if (x[i] == x[3]) m++; return cmp(m,irt,2); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::IntVarArgs y(3); for (int i=0; i<3; i++) y[i]=x[i]; switch (irt) { case Gecode::IRT_EQ: Gecode::exactly(home,y,x[3],2); break; case Gecode::IRT_LQ: Gecode::atmost(home,y,x[3],2); break; case Gecode::IRT_GQ: Gecode::atleast(home,y,x[3],2); break; default: GECODE_NEVER; } } };
Gecode::IntArgs ints({1,0,3,2});
/// %Test number of several equal integers equal to integer class IntArrayInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntArrayInt(Gecode::IntRelType irt0) : Test("MiniModel::"+expand(irt0)+"::IntArray::Int",5,-2,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int m = 0; for (int i=0; i<4; i++) if (x[i] == ints[i]) m++; return cmp(m,irt,2); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::IntVarArgs y(4); for (int i=0; i<4; i++) y[i]=x[i]; switch (irt) { case Gecode::IRT_EQ: Gecode::exactly(home,y,ints,2); break; case Gecode::IRT_LQ: Gecode::atmost(home,y,ints,2); break; case Gecode::IRT_GQ: Gecode::atleast(home,y,ints,2); break; default: GECODE_NEVER; } } };
/// %Test number of several equal integers equal to integer variable class IntArrayVar : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntArrayVar(Gecode::IntRelType irt0) : Test("MiniModel::"+expand(irt0)+"::IntArray::Var",5,-2,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int m = 0; for (int i=0; i<4; i++) if (x[i] == ints[i]) m++; return cmp(m,irt,x[4]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::IntVarArgs y(4); for (int i=0; i<4; i++) y[i]=x[i]; switch (irt) { case Gecode::IRT_EQ: Gecode::exactly(home,y,ints,x[4]); break; case Gecode::IRT_LQ: Gecode::atmost(home,y,ints,x[4]); break; case Gecode::IRT_GQ: Gecode::atleast(home,y,ints,x[4]); break; default: GECODE_NEVER; } } };
/// Help class to create and register tests class Create { public: /// Perform creation and registration Create(void) { for (IntRelTypes irts; irts(); ++irts) if ((irts.irt() == Gecode::IRT_EQ) || (irts.irt() == Gecode::IRT_LQ) || (irts.irt() == Gecode::IRT_GQ)) { (void) new IntInt(irts.irt()); (void) new IntVar(irts.irt()); (void) new VarVar(irts.irt()); (void) new VarInt(irts.irt()); (void) new IntArrayInt(irts.irt()); (void) new IntArrayVar(irts.irt()); } } };
Create c; //@}
}
}}
// STATISTICS: test-minimodel