sequence.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* David Rijsman <David.Rijsman@quintiq.com>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* David Rijsman, 2009
* 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>
#include <climits>
namespace Test { namespace Int {
namespace Sequence {
class SequenceTest : public Test {
protected:
Gecode::IntSet s;
int q,l,u;
public:
SequenceTest(const std::string& s,
const Gecode::IntSet& s0, int q0, int l0, int u0,
int size, int min, int max)
: Test("Sequence::"+s,size,min,max), s(s0), q(q0), l(l0), u(u0) {
}
virtual bool solution(const Assignment& x) const {
for (int i=0; i< (x.size() - q + 1); i++ ) {
int total = 0;
for (int j=i; j < i + q; j++ ) {
if (s.in(x[j]))
total++;
if (total > u)
return false;
}
if ( total < l )
return false;
}
return true;
}
};
class SequenceBoolTest : public SequenceTest {
public:
SequenceBoolTest(const std::string& s, const Gecode::IntSet& s0,
int q0, int l0, int u0, int size)
: SequenceTest("Bool::"+s,s0,q0,l0,u0,size,0,1) {
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::BoolVarArgs c(x.size());
for (int i=0; i<x.size(); i++) {
c[i]=Gecode::channel(home,x[i]);
}
Gecode::sequence(home,c,s,q,l,u);
}
};
class SequenceIntTest : public SequenceTest {
public:
SequenceIntTest(const std::string& s, const Gecode::IntSet& s0,
int q0, int l0, int u0, int size, int min, int max)
: SequenceTest("Int::"+s,s0,q0,l0,u0,size,min,max) {
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::sequence(home,x,s,q,l,u);
}
};
class Create {
public:
Create(void) {
using namespace Gecode;
IntSet a(0,0);
IntSet b(1,1);
IntSet c(2,2);
IntSet d(0,1);
IntArgs ie({0,2});
IntSet e(ie);
(void) new SequenceBoolTest("A",a,3,2,2,6);
(void) new SequenceBoolTest("B",b,3,2,2,6);
(void) new SequenceBoolTest("C",b,6,2,2,6);
(void) new SequenceBoolTest("D",b,6,0,0,6);
(void) new SequenceBoolTest("E",b,6,6,6,6);
(void) new SequenceIntTest ("A",c,3,2,2,6,2,3);
(void) new SequenceIntTest ("B",c,3,2,2,6,2,4);
(void) new SequenceIntTest ("C",b,3,2,2,6,1,3);
(void) new SequenceIntTest ("D",c,3,0,0,3,1,3);
(void) new SequenceIntTest ("E",c,3,3,3,3,1,3);
(void) new SequenceIntTest ("F",c,3,2,2,10,2,3);
(void) new SequenceIntTest ("G",d,3,2,2,6,0,3);
(void) new SequenceIntTest ("H",d,3,2,2,6,0,4);
(void) new SequenceIntTest ("I",d,3,2,2,6,1,3);
(void) new SequenceIntTest ("J",e,3,0,0,6,0,3);
(void) new SequenceIntTest ("K",e,3,3,3,6,0,3);
(void) new SequenceIntTest ("L",e,3,2,2,6,0,3);
}
};
Create c;
}
}}
// STATISTICS: test-int
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::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::Space
Computation spaces.
Definition core.hpp:1742
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::Sequence::Create
Help class to create and register tests.
Definition sequence.cpp:120
Test::Int::Sequence::Create::Create
Create(void)
Perform creation and registration.
Definition sequence.cpp:124
Test::Int::Sequence::SequenceBoolTest
Test for sequence with boolean variables
Definition sequence.cpp:84
Test::Int::Sequence::SequenceBoolTest::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition sequence.cpp:93
Test::Int::Sequence::SequenceBoolTest::SequenceBoolTest
SequenceBoolTest(const std::string &s, const Gecode::IntSet &s0, int q0, int l0, int u0, int size)
Create and register test.
Definition sequence.cpp:87
Test::Int::Sequence::SequenceIntTest
Test for sequence with boolean variables
Definition sequence.cpp:105
Test::Int::Sequence::SequenceIntTest::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition sequence.cpp:114
Test::Int::Sequence::SequenceIntTest::SequenceIntTest
SequenceIntTest(const std::string &s, const Gecode::IntSet &s0, int q0, int l0, int u0, int size, int min, int max)
Create and register test.
Definition sequence.cpp:108
Test::Int::Sequence::SequenceTest::solution
virtual bool solution(const Assignment &x) const
Test whether x is solutionin
Definition sequence.cpp:66
Test::Int::Sequence::SequenceTest::s
Gecode::IntSet s
Definition sequence.cpp:56
Test::Int::Sequence::SequenceTest::l
int l
Definition sequence.cpp:57
Test::Int::Sequence::SequenceTest::u
int u
Definition sequence.cpp:57
Test::Int::Sequence::SequenceTest::SequenceTest
SequenceTest(const std::string &s, const Gecode::IntSet &s0, int q0, int l0, int u0, int size, int min, int max)
Create and register test.
Definition sequence.cpp:60
Test::Int::Sequence::SequenceTest::q
int q
Definition sequence.cpp:57
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::channel
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
Gecode::sequence
void sequence(Home home, const IntVarArgs &x, const IntSet &s, int q, int l, int u, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition sequence.cpp:47
Test::Int::Sequence
Tests for sequence constraints
Definition sequence.cpp:46
Test::Int::Sequence::c
Create c
Definition sequence.cpp:158
Test::Int
Testing finite domain integers.
Definition int.cpp:40
Test
General test support.
Definition afc.cpp:39
int.hh