exec.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 Exec {
class IntWait : public Test {
protected:
bool sf;
public:
IntWait(int n, bool sf0)
: Test("Wait::Int::"+str(n)+"::"+
(sf0 ? "std::function" : "funptr"),n,0,n,false), sf(sf0) {}
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] == x[j])
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
auto f = static_cast<std::function<void(Space&)>>
([](Space& home) { c(home); });
if (x.size() > 1) {
if (sf)
Gecode::wait(home, x, f);
else
Gecode::wait(home, x, &c);
} else {
if (sf)
Gecode::wait(home, x[0], f);
else
Gecode::wait(home, x[0], &c);
}
}
static void c(Gecode::Space& _home) {
TestSpace& home = static_cast<TestSpace&>(_home);
for (int i=0; i<home.x.size(); i++)
for (int j=i+1; j<home.x.size(); j++)
if (home.x[i].val() == home.x[j].val())
home.fail();
}
};
class BoolWait : public Test {
protected:
bool sf;
public:
BoolWait(int n, bool sf0)
: Test("Wait::Bool::"+str(n)+"::"+
(sf0 ? "std::function" : "funptr"),n,0,1,false), sf(sf0) {}
virtual bool solution(const Assignment& x) const {
int t=0;
for (int i=0; i<x.size(); i++)
t += x[i];
return t==2;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
BoolVarArgs b(x.size());
for (int i=b.size(); i--; )
b[i]=channel(home,x[i]);
auto f = static_cast<std::function<void(Space&)>>
([](Space& home) { c(home); });
if (b.size() > 1) {
if (sf)
Gecode::wait(home, b, f);
else
Gecode::wait(home, b, &c);
} else {
if (sf)
Gecode::wait(home, b[0], f);
else
Gecode::wait(home, b[0], &c);
}
}
static void c(Gecode::Space& _home) {
TestSpace& home = static_cast<TestSpace&>(_home);
int t=0;
for (int i=0; i<home.x.size(); i++)
t += home.x[i].val();
if (t!=2)
home.fail();
}
};
class When : public Test {
protected:
bool sf;
public:
When(bool sf0)
: Test(std::string("When::")+
(sf0 ? "std::function" : "funptr"),1,0,1,false), sf(sf0) {}
virtual bool solution(const Assignment& x) const {
return x[0]==0;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
if (sf) {
auto sft = static_cast<std::function<void(Space&)>>
([](Space& home) { t(home); });
auto sfe = static_cast<std::function<void(Space&)>>
([](Space& home) { e(home); });
when(home, channel(home, x[0]), sft, sfe);
} else {
when(home, channel(home, x[0]), &t, &e);
}
}
static void t(Gecode::Space& home) {
home.fail();
}
static void e(Gecode::Space& home) {
(void) home;
}
};
IntWait iw1t(1,true), iw2t(2,true), iw3t(3,true), iw4t(4,true);
IntWait iw1f(1,false), iw2f(2,false), iw3f(3,false), iw4f(4,false);
BoolWait bw1t(1,true), bw2t(2,true), bw3t(3,true), bw4t(4,true);
BoolWait bw1f(1,false), bw2f(2,false), bw3f(3,false), bw4f(4,false);
When whent(true);
When whenf(false);
}
}}
// STATISTICS: test-int
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::FloatVal::size
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
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::Exec::BoolWait
Simple test for wait (Boolean variables).
Definition exec.cpp:94
Test::Int::Exec::BoolWait::sf
bool sf
Whether to use std::function.
Definition exec.cpp:97
Test::Int::Exec::BoolWait::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post wait on x.
Definition exec.cpp:111
Test::Int::Exec::BoolWait::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition exec.cpp:104
Test::Int::Exec::BoolWait::c
static void c(Gecode::Space &_home)
Continuation to be executed.
Definition exec.cpp:131
Test::Int::Exec::BoolWait::BoolWait
BoolWait(int n, bool sf0)
Create and register test.
Definition exec.cpp:100
Test::Int::Exec::IntWait
Simple test for wait (integer variables).
Definition exec.cpp:49
Test::Int::Exec::IntWait::IntWait
IntWait(int n, bool sf0)
Create and register test.
Definition exec.cpp:55
Test::Int::Exec::IntWait::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post wait on x.
Definition exec.cpp:67
Test::Int::Exec::IntWait::sf
bool sf
Whether to use std::function.
Definition exec.cpp:52
Test::Int::Exec::IntWait::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition exec.cpp:59
Test::Int::Exec::IntWait::c
static void c(Gecode::Space &_home)
Continuation to be executed.
Definition exec.cpp:84
Test::Int::Exec::When
Simple test for when.
Definition exec.cpp:142
Test::Int::Exec::When::t
static void t(Gecode::Space &home)
Then-function to be executed.
Definition exec.cpp:169
Test::Int::Exec::When::sf
bool sf
Whether to use std::function.
Definition exec.cpp:145
Test::Int::Exec::When::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post when on x.
Definition exec.cpp:156
Test::Int::Exec::When::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition exec.cpp:152
Test::Int::Exec::When::When
When(bool sf0)
Create and register test.
Definition exec.cpp:148
Test::Int::Exec::When::e
static void e(Gecode::Space &home)
Else-function to be executed.
Definition exec.cpp:173
Test::Int::TestSpace
Space for executing tests.
Definition int.hh:149
Test::Int::TestSpace::x
Gecode::IntVarArray x
Variables to be tested.
Definition int.hh:154
Test::Int::Test::str
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Gecode::Space::fail
void fail(void)
Fail space.
Definition core.hpp:4030
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::when
void when(Home home, BoolVar x, std::function< void(Space &home)> t, std::function< void(Space &home)> e, IntPropLevel ipl=IPL_DEF)
Execute t (then) when x is assigned one, and e (else) otherwise.
Definition exec.cpp:70
Test::Int::Exec
Tests for synchronized execution
Definition exec.cpp:41
Test::Int::Exec::bw2f
BoolWait bw2f(2, false)
Test::Int::Exec::bw2t
BoolWait bw2t(2, true)
Test::Int::Exec::iw3f
IntWait iw3f(3, false)
Test::Int::Exec::iw2f
IntWait iw2f(2, false)
Test::Int::Exec::bw1f
BoolWait bw1f(1, false)
Test::Int::Exec::iw1f
IntWait iw1f(1, false)
Test::Int::Exec::iw1t
IntWait iw1t(1, true)
Test::Int::Exec::iw4f
IntWait iw4f(4, false)
Test::Int::Exec::bw4f
BoolWait bw4f(4, false)
Test::Int::Exec::iw3t
IntWait iw3t(3, true)
Test::Int::Exec::whenf
When whenf(false)
Test::Int::Exec::iw4t
IntWait iw4t(4, true)
Test::Int::Exec::iw2t
IntWait iw2t(2, true)
Test::Int::Exec::bw3t
BoolWait bw3t(3, true)
Test::Int::Exec::bw1t
BoolWait bw1t(1, true)
Test::Int::Exec::whent
When whent(true)
Test::Int::Exec::bw3f
BoolWait bw3f(3, false)
Test::Int::Exec::bw4t
BoolWait bw4t(4, true)
Test::Int
Testing finite domain integers.
Definition int.cpp:40
Test
General test support.
Definition afc.cpp:39
std
Definition flatzinc.cpp:52
int.hh