Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Copyright: * Christian Schulte, 2011 * * 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/int.hh"
namespace Test { namespace Int {
/// %Tests for number of values constraints namespace NValues {
/** * \defgroup TaskTestIntNValues Number of values constraints * \ingroup TaskTestInt */ //@{ /// %Test number of values of integer variables equal to integer class IntInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; /// Number of values int m; public: /// Create and register test IntInt(int n, int m0, Gecode::IntRelType irt0) : Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0), n,0,n), irt(irt0), m(m0) { testfix = false; if (arity > 5) testsearch = false; } /// Create and register initial assignment virtual Assignment* assignment(void) const { if (arity > 5) return new RandomAssignment(arity, dom, 500, _rand); else return new CpltAssignment(arity,dom); } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int n = x.size(); bool* v = new bool[n+1]; for (int i=n+1; i--; ) v[i] = false; int k = 0; for (int i=n; i--; ) if (!v[x[i]]) { k++; v[x[i]] = true; } delete [] v; return cmp(k,irt,m); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { Gecode::nvalues(home, x, irt, m); } };
/// %Test number of values of integer variables equal to integer variable class IntVar : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test IntVar(int n, Gecode::IntRelType irt0) : Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n), irt(irt0) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int n = x.size() - 1; bool* v = new bool[n+1]; for (int i=n+1; i--; ) v[i] = false; int k = 0; for (int i=n; i--; ) if (!v[x[i]]) { k++; v[x[i]] = true; } delete [] v; return cmp(k,irt,x[n]); } /// Post constraint on \a xy virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) { int n = xy.size() - 1; Gecode::IntVarArgs x(n); for (int i=n; i--; ) x[i] = xy[i]; Gecode::nvalues(home, x, irt, xy[n]); } };
/// %Test number of values of Boolean variables equal to integer class BoolInt : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; /// Number of values int m; public: /// Create and register test BoolInt(int n, int m0, Gecode::IntRelType irt0) : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0), n,0,2), irt(irt0), m(m0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int n = x.size(); for (int i=n; i--; ) if (x[i] > 1) return false; bool* v = new bool[n+1]; for (int i=n+1; i--; ) v[i] = false; int k = 0; for (int i=n; i--; ) if (!v[x[i]]) { k++; v[x[i]] = true; } delete [] v; return cmp(k,irt,m); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; BoolVarArgs y(x.size()); for (int i=x.size(); i--; ) y[i] = channel(home, x[i]); nvalues(home, y, irt, m); } };
/// %Test number of values of Boolean variables equal to integer variable class BoolVar : public Test { protected: /// Integer relation type to propagate Gecode::IntRelType irt; public: /// Create and register test BoolVar(int n, Gecode::IntRelType irt0) : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2), irt(irt0) {} /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { int n = x.size() - 1; for (int i=n; i--; ) if (x[i] > 1) return false; bool* v = new bool[n+1]; for (int i=n+1; i--; ) v[i] = false; int k = 0; for (int i=n; i--; ) if (!v[x[i]]) { k++; v[x[i]] = true; } delete [] v; return cmp(k,irt,x[n]); } /// Post constraint on \a xy virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) { using namespace Gecode; int n = xy.size() - 1; BoolVarArgs x(n); for (int i=n; i--; ) x[i] = channel(home, xy[i]); nvalues(home, x, irt, xy[n]); } };
/// Help class to create and register tests class Create { public: /// Perform creation and registration Create(void) { for (IntRelTypes irts; irts(); ++irts) { for (int i=1; i<=7; i += 3) { for (int m=0; m<=3; m++) (void) new BoolInt(i, m, irts.irt()); (void) new BoolVar(i, irts.irt()); } for (int i=1; i<=7; i += 2) { for (int m=0; m<=i+1; m++) (void) new IntInt(i, m, irts.irt()); if (i <= 5) (void) new IntVar(i, irts.irt()); } } } };
Create c; //@}
}}}
// STATISTICS: test-int