assign.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>
*
* Contributing authors:
* Vincent Barichard <Vincent.Barichard@univ-angers.fr>
*
* Copyright:
* Christian Schulte, 2008
* Vincent Barichard, 2012
*
* 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/assign.hh"
#include <gecode/search.hh>
namespace Test { namespace Assign {
class IntTestSpace : public Gecode::Space {
public:
Gecode::IntVarArray x;
IntTestSpace(int n, Gecode::IntSet& d)
: x(*this, n, d) {}
IntTestSpace(IntTestSpace& s)
: Gecode::Space(s) {
x.update(*this, s.x);
}
virtual Gecode::Space* copy(void) {
return new IntTestSpace(*this);
}
};
class BoolTestSpace : public Gecode::Space {
public:
Gecode::BoolVarArray x;
BoolTestSpace(int n)
: x(*this, n, 0, 1) {}
BoolTestSpace(BoolTestSpace& s)
: Gecode::Space(s) {
x.update(*this, s.x);
}
virtual Gecode::Space* copy(void) {
return new BoolTestSpace(*this);
}
};
#ifdef GECODE_HAS_SET_VARS
class SetTestSpace : public Gecode::Space {
public:
Gecode::SetVarArray x;
SetTestSpace(int n, const Gecode::IntSet& d)
: x(*this, n, Gecode::IntSet::empty, d) {}
SetTestSpace(SetTestSpace& s)
: Gecode::Space(s) {
x.update(*this, s.x);
}
virtual Gecode::Space* copy(void) {
return new SetTestSpace(*this);
}
};
#endif
#ifdef GECODE_HAS_FLOAT_VARS
class FloatTestSpace : public Gecode::Space {
public:
Gecode::FloatVarArray x;
FloatTestSpace(int n, const Gecode::FloatVal& d)
: x(*this, n, d.min(), d.max()) {}
FloatTestSpace(FloatTestSpace& s)
: Gecode::Space(s) {
x.update(*this, s.x);
}
virtual Gecode::Space* copy(void) {
return new FloatTestSpace(*this);
}
};
#endif
const char* int_assign_name[] = {
"INT_ASSIGN_MIN",
"INT_ASSIGN_MED",
"INT_ASSIGN_MAX",
"INT_ASSIGN_RND",
"INT_ASSIGN"
};
const int n_int_assign =
sizeof(int_assign_name)/sizeof(const char*);
int int_val(const Gecode::Space&, Gecode::IntVar x, int) {
return x.min();
}
const char* bool_assign_name[] = {
"BOOL_ASSIGN_MIN",
"BOOL_ASSIGN_MAX",
"BOOL_ASSIGN_RND",
"BOOL_ASSIGN"
};
const int n_bool_assign =
sizeof(bool_assign_name)/sizeof(const char*);
int bool_val(const Gecode::Space&, Gecode::BoolVar x, int) {
return x.min();
}
IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d)
: Base("Int::Assign::"+s), arity(a), dom(d) {
}
bool
IntTest::run(void) {
using namespace Gecode;
IntTestSpace* root = new IntTestSpace(arity,dom);
post(*root, root->x);
(void) root->status();
for (int val = 0; val<n_int_assign; val++) {
IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone());
Gecode::Search::Options o;
o.a_d = Base::rand(10);
o.c_d = Base::rand(10);
Rnd r(1);
IntAssign ia;
switch (val) {
case 0: ia = INT_ASSIGN_MIN(); break;
case 1: ia = INT_ASSIGN_MED(); break;
case 2: ia = INT_ASSIGN_MAX(); break;
case 3: ia = INT_ASSIGN_RND(r); break;
case 4: ia = INT_ASSIGN(&int_val); break;
default: GECODE_NEVER;
}
assign(*clone, clone->x, ia);
Gecode::DFS<IntTestSpace> e_s(clone, o);
delete clone;
// Find number of solutions
int solutions = 0;
while (Space* s = e_s.next()) {
delete s; solutions++;
}
if (solutions != 1) {
std::cout << "FAILURE" << std::endl
<< "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
<< "\t" << int_assign_name[val] << std::endl;
delete root;
return false;
}
}
delete root;
return true;
}
BoolTest::BoolTest(const std::string& s, int a)
: Base("Bool::Assign::"+s), arity(a) {
}
bool
BoolTest::run(void) {
using namespace Gecode;
BoolTestSpace* root = new BoolTestSpace(arity);
post(*root, root->x);
(void) root->status();
for (int val = n_bool_assign; val--; ) {
BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone());
Gecode::Search::Options o;
o.a_d = Base::rand(10);
o.c_d = Base::rand(10);
Rnd r(1);
BoolAssign ia;
switch (val) {
case 0: ia = BOOL_ASSIGN_MIN(); break;
case 1: ia = BOOL_ASSIGN_MAX(); break;
case 2: ia = BOOL_ASSIGN_RND(r); break;
case 3: ia = BOOL_ASSIGN(&bool_val); break;
default: GECODE_NEVER;
}
assign(*clone, clone->x, ia);
Gecode::DFS<BoolTestSpace> e_s(clone, o);
delete clone;
// Find number of solutions
int solutions = 0;
while (Space* s = e_s.next()) {
delete s; solutions++;
}
if (solutions != 1) {
std::cout << "FAILURE" << std::endl
<< "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
<< "\t" << int_assign_name[val] << std::endl;
delete root;
return false;
}
}
delete root;
return true;
}
#ifdef GECODE_HAS_SET_VARS
const char* set_assign_name[] = {
"SET_ASSIGN_MIN_INC",
"SET_ASSIGN_MIN_EXC",
"SET_ASSIGN_MED_INC",
"SET_ASSIGN_MED_EXC",
"SET_ASSIGN_MAX_INC",
"SET_ASSIGN_MAX_EXC",
"SET_ASSIGN_RND_INC",
"SET_ASSIGN_RND_EXC",
"SET_ASSIGN"
};
const int n_set_assign =
sizeof(set_assign_name)/sizeof(const char*);
int set_val(const Gecode::Space&, Gecode::SetVar x, int) {
Gecode::SetVarUnknownRanges r(x);
return r.min();
}
SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d)
: Base("Set::Assign::"+s), arity(a), dom(d) {
}
bool
SetTest::run(void) {
using namespace Gecode;
SetTestSpace* root = new SetTestSpace(arity,dom);
post(*root, root->x);
(void) root->status();
for (int val = n_set_assign; val--; ) {
SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone());
Gecode::Search::Options o;
o.a_d = Base::rand(10);
o.c_d = Base::rand(10);
Rnd r(1);
SetAssign sa;
switch (val) {
case 0: sa = SET_ASSIGN_MIN_INC(); break;
case 1: sa = SET_ASSIGN_MIN_EXC(); break;
case 2: sa = SET_ASSIGN_MED_INC(); break;
case 3: sa = SET_ASSIGN_MED_EXC(); break;
case 4: sa = SET_ASSIGN_MAX_INC(); break;
case 5: sa = SET_ASSIGN_MAX_EXC(); break;
case 6: sa = SET_ASSIGN_RND_INC(r); break;
case 7: sa = SET_ASSIGN_RND_EXC(r); break;
case 8: sa = SET_ASSIGN(&set_val); break;
default: GECODE_NEVER;
}
assign(*clone, clone->x, sa);
Gecode::DFS<SetTestSpace> e_s(clone, o);
delete clone;
// Find number of solutions
int solutions = 0;
while (Space* s = e_s.next()) {
delete s; solutions++;
}
if (solutions != 1) {
std::cout << "FAILURE" << std::endl
<< "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
<< "\t" << set_assign_name[val] << std::endl;
delete root;
return false;
}
}
delete root;
return true;
}
#endif
#ifdef GECODE_HAS_FLOAT_VARS
const char* float_assign_name[] = {
"FLOAT_ASSIGN_MIN",
"FLOAT_ASSIGN_MAX",
"FLOAT_ASSIGN_RND",
"FLOAT_ASSIGN"
};
const int n_float_assign =
sizeof(float_assign_name)/sizeof(const char*);
Gecode::FloatNumBranch float_val(const Gecode::Space&,
Gecode::FloatVar x, int) {
Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true;
return nl;
}
FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d)
: Base("Float::Assign::"+s), arity(a), dom(d) {
}
bool
FloatTest::run(void) {
using namespace Gecode;
FloatTestSpace* root = new FloatTestSpace(arity,dom);
post(*root, root->x);
(void) root->status();
for (int val = n_float_assign; val--; ) {
FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone());
Gecode::Search::Options o;
o.a_d = Base::rand(10);
o.c_d = Base::rand(10);
Rnd r(1);
FloatAssign fa;
switch (val) {
case 0: fa = FLOAT_ASSIGN_MIN(); break;
case 1: fa = FLOAT_ASSIGN_MAX(); break;
case 2: fa = FLOAT_ASSIGN_RND(r); break;
case 3: fa = FLOAT_ASSIGN(&float_val); break;
default: GECODE_NEVER;
}
assign(*clone, clone->x, fa);
Gecode::DFS<FloatTestSpace> e_s(clone, o);
delete clone;
// Find number of solutions
int solutions = 0;
while (Space* s = e_s.next()) {
delete s; solutions++;
}
if (solutions != 1) {
std::cout << "FAILURE" << std::endl
<< "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl
<< "\t" << float_assign_name[val] << std::endl;
delete root;
return false;
}
}
delete root;
return true;
}
#endif
}}
// STATISTICS: test-branch
assign.hh
Gecode::BoolVarArray
Boolean variable array.
Definition int.hh:808
Gecode::BoolVar
Boolean integer variables.
Definition int.hh:512
Gecode::DFS
Depth-first search engine.
Definition search.hh:1036
Gecode::FloatAssign
Which values to select for assignment.
Definition float.hh:1874
Gecode::FloatNumBranch
Value description class for branching.
Definition float.hh:1462
Gecode::FloatNumBranch::l
bool l
Whether to try the lower or upper half first.
Definition float.hh:1467
Gecode::FloatNumBranch::n
FloatNum n
The middle value for branching.
Definition float.hh:1465
Gecode::FloatVal
Float value type.
Definition float.hh:334
Gecode::FloatVarArray
Float variable array.
Definition float.hh:1030
Gecode::FloatVar
Float variables.
Definition float.hh:870
Gecode::IntAssign
Which values to select for assignment.
Definition int.hh:4971
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::Rnd
Random number generator.
Definition rnd.hpp:42
Gecode::Search::Options
Search engine options
Definition search.hh:746
Gecode::Search::Options::c_d
unsigned int c_d
Create a clone after every c_d commits (commit distance).
Definition search.hh:753
Gecode::Search::Options::a_d
unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance).
Definition search.hh:755
Gecode::SetAssign
Which value to select for assignment.
Definition set.hh:1517
Gecode::SetVarArray
Set variable array
Definition set.hh:570
Gecode::SetVarUnknownRanges
Iterator for the unknown ranges of a set variable.
Definition set.hh:334
Gecode::SetVar
Set variables
Definition set.hh:127
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Test::Assign::BoolTestSpace::bool_assign_name
const char * bool_assign_name[]
Names for integer assignments.
Definition assign.cpp:156
Test::Assign::BoolTestSpace::BoolTestSpace
BoolTestSpace(int n)
Initialize test space.
Definition assign.cpp:69
Test::Assign::BoolTestSpace::copy
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:77
Test::Assign::BoolTestSpace::n_bool_assign
const int n_bool_assign
Number of integer value selections.
Definition assign.cpp:163
Test::Assign::BoolTestSpace::x
Gecode::BoolVarArray x
Variables to be tested.
Definition assign.cpp:67
Test::Assign::BoolTestSpace::BoolTestSpace
BoolTestSpace(BoolTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:72
Test::Assign::BoolTestSpace::bool_val
int bool_val(const Gecode::Space &, Gecode::BoolVar x, int)
Test function for branch value function.
Definition assign.cpp:166
Test::Assign::BoolTest::run
virtual bool run(void)
Perform test.
Test::Assign::BoolTest::BoolTest
BoolTest(const std::string &s, int a)
Construct and register test.
Test::Assign::BoolTest::arity
int arity
Number of variables.
Definition assign.hh:83
Test::Assign::BoolTest::post
virtual void post(Gecode::Space &home, Gecode::BoolVarArray &x)=0
Post assignment on variables x.
Test::Assign::FloatTestSpace::float_val
Gecode::FloatNumBranch float_val(const Gecode::Space &, Gecode::FloatVar x, int)
Test function for branch value function.
Definition assign.cpp:370
Test::Assign::FloatTestSpace::x
Gecode::FloatVarArray x
Variables to be tested.
Definition assign.cpp:111
Test::Assign::FloatTestSpace::n_float_assign
const int n_float_assign
Number of float value selections.
Definition assign.cpp:367
Test::Assign::FloatTestSpace::copy
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:121
Test::Assign::FloatTestSpace::FloatTestSpace
FloatTestSpace(FloatTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:116
Test::Assign::FloatTestSpace::float_assign_name
const char * float_assign_name[]
Names for float assignments.
Definition assign.cpp:360
Test::Assign::FloatTestSpace::FloatTestSpace
FloatTestSpace(int n, const Gecode::FloatVal &d)
Initialize test space.
Definition assign.cpp:113
Test::Assign::FloatTest::FloatTest
FloatTest(const std::string &s, int a, const Gecode::FloatVal &d)
Construct and register test.
Test::Assign::FloatTest::post
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)=0
Post assignment on variables x.
Test::Assign::FloatTest::arity
int arity
Number of variables.
Definition assign.hh:125
Test::Assign::FloatTest::run
virtual bool run(void)
Perform test.
Test::Assign::IntTestSpace::copy
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:58
Test::Assign::IntTestSpace::IntTestSpace
IntTestSpace(int n, Gecode::IntSet &d)
Initialize test space.
Definition assign.cpp:50
Test::Assign::IntTestSpace::IntTestSpace
IntTestSpace(IntTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:53
Test::Assign::IntTestSpace::int_assign_name
const char * int_assign_name[]
Names for integer assignments.
Definition assign.cpp:134
Test::Assign::IntTestSpace::int_val
int int_val(const Gecode::Space &, Gecode::IntVar x, int)
Test function for branch value function.
Definition assign.cpp:145
Test::Assign::IntTestSpace::x
Gecode::IntVarArray x
Variables to be tested.
Definition assign.cpp:48
Test::Assign::IntTestSpace::n_int_assign
const int n_int_assign
Number of integer value selections.
Definition assign.cpp:142
Test::Assign::IntTest::run
virtual bool run(void)
Perform test.
Test::Assign::IntTest::IntTest
IntTest(const std::string &s, int a, const Gecode::IntSet &d)
Construct and register test.
Test::Assign::IntTest::arity
int arity
Number of variables.
Definition assign.hh:64
Test::Assign::IntTest::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)=0
Post assignment on variables x.
Test::Assign::SetTestSpace::n_set_assign
const int n_set_assign
Number of set value selections.
Definition assign.cpp:287
Test::Assign::SetTestSpace::SetTestSpace
SetTestSpace(int n, const Gecode::IntSet &d)
Initialize test space.
Definition assign.cpp:90
Test::Assign::SetTestSpace::set_val
int set_val(const Gecode::Space &, Gecode::SetVar x, int)
Test function for branch value function.
Definition assign.cpp:290
Test::Assign::SetTestSpace::x
Gecode::SetVarArray x
Variables to be tested.
Definition assign.cpp:88
Test::Assign::SetTestSpace::SetTestSpace
SetTestSpace(SetTestSpace &s)
Constructor for cloning s.
Definition assign.cpp:93
Test::Assign::SetTestSpace::copy
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition assign.cpp:98
Test::Assign::SetTestSpace::set_assign_name
const char * set_assign_name[]
Names for integer assignments.
Definition assign.cpp:275
Test::Assign::SetTest::arity
int arity
Number of variables.
Definition assign.hh:102
Test::Assign::SetTest::run
virtual bool run(void)
Perform test.
Test::Assign::SetTest::post
virtual void post(Gecode::Space &home, Gecode::SetVarArray &x)=0
Post assignment on variables x.
Test::Assign::SetTest::SetTest
SetTest(const std::string &s, int a, const Gecode::IntSet &d)
Construct and register test.
Test::Base
Base class for all tests to be run
Definition test.hh:103
Test::Base::rand
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Gecode::assign
void assign(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatAssign vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Assign all x with variable selection vars and value selection vals.
Definition branch.cpp:111
Gecode::Space::Space
Space(void)
Default constructor.
Definition core.cpp:115
Gecode::Search::Config::clone
const bool clone
Whether engines create a clone when being initialized.
Definition search.hh:108
Gecode
Gecode toplevel namespace
Gecode::INT_ASSIGN
IntAssign INT_ASSIGN(IntBranchVal v, IntBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c.
Definition assign.hpp:75
Gecode::INT_ASSIGN_RND
IntAssign INT_ASSIGN_RND(Rnd r)
Select random value.
Definition assign.hpp:70
Gecode::BOOL_ASSIGN_MAX
BoolAssign BOOL_ASSIGN_MAX(void)
Select largest value.
Definition assign.hpp:105
Gecode::SET_ASSIGN_RND_EXC
SetAssign SET_ASSIGN_RND_EXC(Rnd r)
Exclude random element.
Definition assign.hpp:90
Gecode::SET_ASSIGN_MED_EXC
SetAssign SET_ASSIGN_MED_EXC(void)
Exclude median element (rounding downwards).
Definition assign.hpp:70
Gecode::SET_ASSIGN_MED_INC
SetAssign SET_ASSIGN_MED_INC(void)
Include median element (rounding downwards).
Definition assign.hpp:65
Gecode::dom
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
Gecode::FLOAT_ASSIGN_MAX
FloatAssign FLOAT_ASSIGN_MAX(void)
Select median value of the upper part.
Definition assign.hpp:60
Gecode::SET_ASSIGN_MAX_INC
SetAssign SET_ASSIGN_MAX_INC(void)
Include largest element.
Definition assign.hpp:75
Gecode::BOOL_ASSIGN_RND
BoolAssign BOOL_ASSIGN_RND(Rnd r)
Select random value.
Definition assign.hpp:110
Gecode::SET_ASSIGN_RND_INC
SetAssign SET_ASSIGN_RND_INC(Rnd r)
Include random element.
Definition assign.hpp:85
Gecode::INT_ASSIGN_MED
IntAssign INT_ASSIGN_MED(void)
Select greatest value not greater than the median.
Definition assign.hpp:60
Gecode::BOOL_ASSIGN
BoolAssign BOOL_ASSIGN(BoolBranchVal v, BoolBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c.
Definition assign.hpp:115
Gecode::INT_ASSIGN_MIN
IntAssign INT_ASSIGN_MIN(void)
Select smallest value.
Definition assign.hpp:55
Gecode::SET_ASSIGN_MIN_EXC
SetAssign SET_ASSIGN_MIN_EXC(void)
Exclude smallest element.
Definition assign.hpp:60
Gecode::SET_ASSIGN_MIN_INC
SetAssign SET_ASSIGN_MIN_INC(void)
Include smallest element.
Definition assign.hpp:55
Gecode::SET_ASSIGN
SetAssign SET_ASSIGN(SetBranchVal v, SetBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c.
Definition assign.hpp:95
Gecode::SET_ASSIGN_MAX_EXC
SetAssign SET_ASSIGN_MAX_EXC(void)
Exclude largest element.
Definition assign.hpp:80
Gecode::FLOAT_ASSIGN_RND
FloatAssign FLOAT_ASSIGN_RND(Rnd r)
Select median value of a randomly chosen part.
Definition assign.hpp:65
Gecode::BOOL_ASSIGN_MIN
BoolAssign BOOL_ASSIGN_MIN(void)
Select smallest value.
Definition assign.hpp:100
Gecode::INT_ASSIGN_MAX
IntAssign INT_ASSIGN_MAX(void)
Select largest value.
Definition assign.hpp:65
Gecode::FLOAT_ASSIGN_MIN
FloatAssign FLOAT_ASSIGN_MIN(void)
Select median value of the lower part.
Definition assign.hpp:55
Gecode::FLOAT_ASSIGN
FloatAssign FLOAT_ASSIGN(FloatBranchVal v, FloatBranchCommit c=nullptr)
Definition assign.hpp:70
Test::Assign
Tests for assignments.
Definition assign.cpp:42
Test::Branch::solutions
int solutions(TestSpace *c, Gecode::Search::Options &o, int maxNbSol=-1)
Find number of solutions.
Definition branch.cpp:412
Test::Float::Arithmetic::a
Gecode::FloatVal a(-8, 5)
Test
General test support.
Definition afc.cpp:39
Test::r
Region r
Definition region.cpp:65
search.hh
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56