unary.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
*
* 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 {
namespace Unary {}
}}
namespace Test { namespace Int { namespace Unary {
class ManFixPUnary : public Test {
protected:
Gecode::IntArgs p;
static int st(const Gecode::IntArgs& p) {
int t = 0;
for (int i=p.size(); i--; )
t += p[i];
return t;
}
public:
ManFixPUnary(const Gecode::IntArgs& p0, int o, Gecode::IntPropLevel ipl0)
: Test("Unary::Man::Fix::"+str(o)+"::"+str(p0)+"::"+str(ipl0),
p0.size(),o,o+st(p0),false,ipl0),
p(p0) {
testsearch = false;
contest = CTL_NONE;
}
virtual Assignment* assignment(void) const {
return new RandomAssignment(arity,dom,500);
}
virtual bool solution(const Assignment& x) const {
for (int i=0; i<x.size(); i++)
for (int j=i+1; j<x.size(); j++)
if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::unary(home, x, p, ipl);
}
};
class OptFixPUnary : public Test {
protected:
Gecode::IntArgs p;
int l;
static int st(const Gecode::IntArgs& p) {
int t = 0;
for (int i=p.size(); i--; )
t += p[i];
return t;
}
public:
OptFixPUnary(const Gecode::IntArgs& p0, int o, Gecode::IntPropLevel ipl0)
: Test("Unary::Opt::Fix::"+str(o)+"::"+str(p0)+"::"+str(ipl0),
2*p0.size(),o,o+st(p0),false,ipl0), p(p0), l(o+st(p)/2) {
testsearch = false;
contest = CTL_NONE;
}
virtual Assignment* assignment(void) const {
return new RandomAssignment(arity,dom,500);
}
virtual bool solution(const Assignment& x) const {
int n = x.size() / 2;
for (int i=0; i<n; i++)
if (x[n+i] > l)
for (int j=i+1; j<n; j++)
if(x[n+j] > l)
if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
int n=x.size() / 2;
Gecode::IntVarArgs s(n);
Gecode::BoolVarArgs m(n);
for (int i=0; i<n; i++) {
s[i]=x[i];
m[i]=Gecode::expr(home, (x[n+i] > l));
}
Gecode::unary(home, s, p, m, ipl);
}
};
class ManFlexUnary : public Test {
protected:
int _minP;
int _maxP;
int off;
public:
ManFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
: Test("Unary::Man::Flex::"+str(o)+"::"+str(n)+"::"
+str(minP)+"::"+str(maxP)+"::"+str(ipl0),
2*n,0,n*maxP,false,ipl0), _minP(minP), _maxP(maxP), off(o) {
testsearch = false;
testfix = false;
contest = CTL_NONE;
}
virtual Assignment* assignment(void) const {
return new RandomMixAssignment(arity/2,dom,arity/2,
Gecode::IntSet(_minP,_maxP),500);
}
virtual bool solution(const Assignment& x) const {
int n = x.size()/2;
for (int i=0; i<n; i++)
for (int j=i+1; j<n; j++)
if ((x[i]+x[n+i] > x[j]) && (x[j]+x[n+j] > x[i]))
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs s(x.size()/2);
Gecode::IntVarArgs px(x.slice(x.size()/2));
Gecode::IntVarArgs e(home,x.size()/2,
Gecode::Int::Limits::min,
Gecode::Int::Limits::max);
for (int i=s.size(); i--;) {
s[i] = expr(home, off+x[i]);
rel(home, s[i]+px[i] == e[i]);
rel(home, _minP <= px[i]);
rel(home, _maxP >= px[i]);
}
Gecode::unary(home, s, px, e, ipl);
}
};
class OptFlexUnary : public Test {
protected:
int _minP;
int _maxP;
int off;
int l;
static int st(const Gecode::IntArgs& p) {
int t = 0;
for (int i=p.size(); i--; )
t += p[i];
return t;
}
public:
OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
: Test("Unary::Opt::Flex::"+str(o)+"::"+str(n)+"::"
+str(minP)+"::"+str(maxP)+"::"+str(ipl0),
3*n,0,n*maxP,false,ipl0), _minP(minP), _maxP(maxP), off(o),
l(n*maxP/2) {
testsearch = false;
testfix = false;
contest = CTL_NONE;
}
virtual Assignment* assignment(void) const {
return new RandomMixAssignment(2*(arity/3),dom,arity/3,
Gecode::IntSet(_minP,_maxP),500);
}
virtual bool solution(const Assignment& x) const {
int n = x.size() / 3;
for (int i=0; i<n; i++)
if (x[n+i] > l)
for (int j=i+1; j<n; j++)
if(x[n+j] > l)
if ((x[i]+x[2*n+i] > x[j]) && (x[j]+x[2*n+j] > x[i]))
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
int n=x.size() / 3;
Gecode::IntVarArgs s(n);
Gecode::IntVarArgs px(n);
Gecode::IntVarArgs e(home,n,
Gecode::Int::Limits::min,
Gecode::Int::Limits::max);
for (int i=n; i--;) {
s[i] = expr(home, off+x[i]);
px[i] = x[2*n+i];
rel(home, s[i]+px[i] == e[i]);
rel(home, _minP <= px[i]);
rel(home, _maxP >= px[i]);
}
Gecode::BoolVarArgs m(n);
for (int i=0; i<n; i++)
m[i]=Gecode::expr(home, (x[n+i] > l));
Gecode::unary(home, s, px, e, m, ipl);
}
};
class Create {
public:
Create(void) {
using namespace Gecode;
IntArgs p1({2,2,2,2});
IntArgs p10({2,2,0,2,2});
IntArgs p2({4,3,3,5});
IntArgs p20({4,0,3,3,0,5});
IntArgs p3({4,2,9,3,7,5});
IntArgs p30({4,0,2,9,3,7,5,0});
for (IntPropBasicAdvanced ipba; ipba(); ++ipba) {
(void) new ManFixPUnary(p1,0,ipba.ipl());
(void) new ManFixPUnary(p1,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFixPUnary(p1,0,ipba.ipl());
(void) new OptFixPUnary(p1,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(4,0,2,0,ipba.ipl());
(void) new ManFlexUnary(4,0,2,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(4,1,3,0,ipba.ipl());
(void) new ManFlexUnary(4,1,3,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFlexUnary(4,0,2,0,ipba.ipl());
(void) new OptFlexUnary(4,0,2,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFixPUnary(p10,0,ipba.ipl());
(void) new ManFixPUnary(p10,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFixPUnary(p10,0,ipba.ipl());
(void) new OptFixPUnary(p10,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(5,0,2,0,ipba.ipl());
(void) new ManFlexUnary(5,0,2,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFlexUnary(5,0,2,0,ipba.ipl());
(void) new OptFlexUnary(5,0,2,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFixPUnary(p2,0,ipba.ipl());
(void) new ManFixPUnary(p2,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFixPUnary(p2,0,ipba.ipl());
(void) new OptFixPUnary(p2,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(4,3,5,0,ipba.ipl());
(void) new ManFlexUnary(4,3,5,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFlexUnary(4,3,5,0,ipba.ipl());
(void) new OptFlexUnary(4,3,5,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFixPUnary(p20,0,ipba.ipl());
(void) new ManFixPUnary(p20,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFixPUnary(p20,0,ipba.ipl());
(void) new OptFixPUnary(p20,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(6,0,5,0,ipba.ipl());
(void) new ManFlexUnary(6,0,5,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFlexUnary(6,0,5,0,ipba.ipl());
(void) new OptFlexUnary(6,0,5,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFixPUnary(p3,0,ipba.ipl());
(void) new ManFixPUnary(p3,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFixPUnary(p3,0,ipba.ipl());
(void) new OptFixPUnary(p3,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(6,2,7,0,ipba.ipl());
(void) new ManFlexUnary(6,2,7,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFlexUnary(6,2,7,0,ipba.ipl());
(void) new OptFlexUnary(6,2,7,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFixPUnary(p30,0,ipba.ipl());
(void) new ManFixPUnary(p30,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFixPUnary(p30,0,ipba.ipl());
(void) new OptFixPUnary(p30,Gecode::Int::Limits::min,ipba.ipl());
(void) new ManFlexUnary(8,0,9,0,ipba.ipl());
(void) new ManFlexUnary(8,0,9,Gecode::Int::Limits::min,ipba.ipl());
(void) new OptFlexUnary(8,0,9,0,ipba.ipl());
(void) new OptFlexUnary(8,0,9,Gecode::Int::Limits::min,ipba.ipl());
}
}
};
Create c;
}}}
// STATISTICS: test-int
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::VarArray::slice
ArrayTraits< VarArgArray< Var > >::ArgsType slice(int start, int inc=1, int n=-1)
Definition array.hpp:946
Gecode::VarArray::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:926
Test::Int::Assignment
Base class for assignments
Definition int.hh:59
Test::Int::Assignment::size
int size(void) const
Return number of variables.
Definition int.hpp:46
Test::Int::IntPropBasicAdvanced
Iterator for basic and advanced integer propagation levels.
Definition int.hh:350
Test::Int::RandomAssignment
Generate random selection of assignments.
Definition int.hh:96
Test::Int::RandomMixAssignment
Generate random selection of assignments.
Definition int.hh:116
Test::Int::Test::testsearch
bool testsearch
Whether to perform search test.
Definition int.hh:238
Test::Int::Test::testfix
bool testfix
Whether to perform fixpoint test.
Definition int.hh:240
Test::Int::Test::ipl
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:234
Test::Int::Test::arity
int arity
Number of variables.
Definition int.hh:226
Test::Int::Test::str
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Test::Int::Test::dom
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
Test::Int::Test::contest
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:236
Test::Int::Unary::Create
Help class to create and register tests.
Definition unary.cpp:258
Test::Int::Unary::Create::Create
Create(void)
Perform creation and registration.
Definition unary.cpp:261
Test::Int::Unary::ManFixPUnary
Test for unary constraint
Definition unary.cpp:51
Test::Int::Unary::ManFixPUnary::p
Gecode::IntArgs p
The processing times.
Definition unary.cpp:54
Test::Int::Unary::ManFixPUnary::ManFixPUnary
ManFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:64
Test::Int::Unary::ManFixPUnary::assignment
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:72
Test::Int::Unary::ManFixPUnary::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:84
Test::Int::Unary::ManFixPUnary::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:76
Test::Int::Unary::ManFixPUnary::st
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition unary.cpp:56
Test::Int::Unary::ManFlexUnary
Test for unary constraint
Definition unary.cpp:140
Test::Int::Unary::ManFlexUnary::off
int off
Offset for start times.
Definition unary.cpp:147
Test::Int::Unary::ManFlexUnary::_minP
int _minP
Minimum processing time.
Definition unary.cpp:143
Test::Int::Unary::ManFlexUnary::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:173
Test::Int::Unary::ManFlexUnary::assignment
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:159
Test::Int::Unary::ManFlexUnary::_maxP
int _maxP
Maximum processing time.
Definition unary.cpp:145
Test::Int::Unary::ManFlexUnary::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:164
Test::Int::Unary::ManFlexUnary::ManFlexUnary
ManFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:150
Test::Int::Unary::OptFixPUnary
Test for unary constraint with optional tasks
Definition unary.cpp:90
Test::Int::Unary::OptFixPUnary::assignment
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:112
Test::Int::Unary::OptFixPUnary::OptFixPUnary
OptFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:105
Test::Int::Unary::OptFixPUnary::p
Gecode::IntArgs p
The processing times.
Definition unary.cpp:93
Test::Int::Unary::OptFixPUnary::st
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition unary.cpp:97
Test::Int::Unary::OptFixPUnary::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:116
Test::Int::Unary::OptFixPUnary::l
int l
Threshold for taking a task as optional.
Definition unary.cpp:95
Test::Int::Unary::OptFixPUnary::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:127
Test::Int::Unary::OptFlexUnary
Test for unary constraint with optional tasks
Definition unary.cpp:190
Test::Int::Unary::OptFlexUnary::_minP
int _minP
Minimum processing time.
Definition unary.cpp:193
Test::Int::Unary::OptFlexUnary::st
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition unary.cpp:201
Test::Int::Unary::OptFlexUnary::off
int off
Offset for start times.
Definition unary.cpp:197
Test::Int::Unary::OptFlexUnary::l
int l
Threshold for taking a task as optional.
Definition unary.cpp:199
Test::Int::Unary::OptFlexUnary::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:235
Test::Int::Unary::OptFlexUnary::assignment
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:219
Test::Int::Unary::OptFlexUnary::OptFlexUnary
OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:209
Test::Int::Unary::OptFlexUnary::_maxP
int _maxP
Maximum processing time.
Definition unary.cpp:195
Test::Int::Unary::OptFlexUnary::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:224
Gecode::IntPropLevel
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
minimodel.hh
Gecode::Int::Limits::min
const int min
Smallest allowed integer value.
Definition int.hh:118
Gecode::Int::Limits::max
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode
Gecode toplevel namespace
Gecode::expr
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
Gecode::unary
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntPropLevel ipl=IPL_DEF)
Post propagators for scheduling tasks on unary resources.
Definition unary.cpp:44
Test::Int::Unary
Tests for unary scheduling constraints
Definition unary.cpp:40
Test::Int::Unary::c
Create c
Definition unary.cpp:330
Test::Int
Testing finite domain integers.
Definition int.cpp:40
Test::Int::CTL_NONE
@ CTL_NONE
No consistency-test.
Definition int.hh:140
Test
General test support.
Definition afc.cpp:39
int.hh