float.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>
* Mikael Lagerkvist <lagerkvist@gecode.org>
* Vincent Barichard <Vincent.Barichard@univ-angers.fr>
*
* Copyright:
* Christian Schulte, 2005
* Mikael Lagerkvist, 2005
* 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/float.hh"
#include <algorithm>
#include <iomanip>
namespace Test { namespace Float {
/*
* Complete assignments
*
*/
void
CpltAssignment::operator++(void) {
using namespace Gecode;
int i = n-1;
while (true) {
FloatNum ns = dsv[i].min() + step;
dsv[i] = FloatVal(ns,nextafter(ns,ns+1));
if ((dsv[i].max() < d.max()) || (i == 0))
return;
dsv[i--] = FloatVal(d.min(),nextafter(d.min(),d.max()));
}
}
/*
* Extended assignments
*
*/
void
ExtAssignment::operator++(void) {
using namespace Gecode;
assert(n > 1);
int i = n-2;
while (true) {
FloatNum ns = dsv[i].min() + step;
dsv[i] = FloatVal(ns,nextafter(ns,ns+1));
if ((dsv[i].max() < d.max()) || (i == 0)) {
if (curPb->extendAssignement(*this)) return;
if ((dsv[i].max() >= d.max()) && (i == 0)) return;
continue;
}
dsv[i--] = FloatVal(d.min(),nextafter(d.min(),d.max()));
}
}
/*
* Random assignments
*
*/
void
RandomAssignment::operator++(void) {
for (int i = n; i--; )
vals[i]=randval();
a--;
}
}}
std::ostream&
operator<<(std::ostream& os, const Test::Float::Assignment& a) {
int n = a.size();
os << "{";
for (int i=0; i<n; i++)
os << "[" << a[i].min() << "," << a[i].max() << "]" << ((i!=n-1) ? "," : "}");
return os;
}
namespace Test { namespace Float {
Gecode::FloatNum randFValDown(Gecode::FloatNum l, Gecode::FloatNum u) {
using namespace Gecode;
using namespace Gecode::Float;
Rounding r;
return
r.add_down(
l,
r.mul_down(
r.div_down(
Base::rand(static_cast<unsigned int>(Int::Limits::max)),
static_cast<FloatNum>(Int::Limits::max)
),
r.sub_down(u,l)
)
);
}
Gecode::FloatNum randFValUp(Gecode::FloatNum l, Gecode::FloatNum u) {
using namespace Gecode;
using namespace Gecode::Float;
Rounding r;
return
r.sub_up(
u,
r.mul_down(
r.div_down(
Base::rand(static_cast<unsigned int>(Int::Limits::max)),
static_cast<FloatNum>(Int::Limits::max)
),
r.sub_down(u,l)
)
);
}
TestSpace::TestSpace(int n, Gecode::FloatVal& d0, Gecode::FloatNum s,
Test* t)
: d(d0), step(s),
x(*this,n,Gecode::Float::Limits::min,Gecode::Float::Limits::max),
test(t), reified(false) {
Gecode::FloatVarArgs _x(*this,n,d.min(),d.max());
if (x.size() == 1)
Gecode::dom(*this,x[0],_x[0]);
else
Gecode::dom(*this,x,_x);
Gecode::BoolVar b(*this,0,1);
r = Gecode::Reify(b,Gecode::RM_EQV);
if (opt.log)
olog << ind(2) << "Initial: x[]=" << x
<< std::endl;
}
TestSpace::TestSpace(int n, Gecode::FloatVal& d0, Gecode::FloatNum s,
Test* t, Gecode::ReifyMode rm)
: d(d0), step(s), x(*this,n,d.min(),d.max()), test(t), reified(true) {
Gecode::BoolVar b(*this,0,1);
r = Gecode::Reify(b,rm);
if (opt.log)
olog << ind(2) << "Initial: x[]=" << x
<< " b=" << r.var()
<< std::endl;
}
TestSpace::TestSpace(TestSpace& s)
: Gecode::Space(s), d(s.d), step(s.step), test(s.test), reified(s.reified) {
x.update(*this, s.x);
Gecode::BoolVar b;
Gecode::BoolVar sr(s.r.var());
b.update(*this, sr);
r.var(b); r.mode(s.r.mode());
}
Gecode::Space*
TestSpace::copy(void) {
return new TestSpace(*this);
}
void
TestSpace::enable(void) {
Gecode::PropagatorGroup::all.enable(*this);
}
void
TestSpace::disable(void) {
Gecode::PropagatorGroup::all.disable(*this);
(void) status();
}
void
TestSpace::dropUntil(const Assignment& a) {
for (int i = x.size(); i--; )
Gecode::rel(*this, x[i], Gecode::FRT_GQ, a[i].min());
}
bool
TestSpace::assigned(void) const {
for (int i=x.size(); i--; )
if (!x[i].assigned())
return false;
return true;
}
bool
TestSpace::matchAssignment(const Assignment& a) const {
for (int i=x.size(); i--; )
if ((x[i].min() < a[i].min()) && (x[i].max() > a[i].max()))
return false;
return true;
}
void
TestSpace::post(void) {
if (reified){
test->post(*this,x,r);
if (opt.log)
olog << ind(3) << "Posting reified propagator" << std::endl;
} else {
test->post(*this,x);
if (opt.log)
olog << ind(3) << "Posting propagator" << std::endl;
}
}
bool
TestSpace::failed(void) {
if (opt.log) {
olog << ind(3) << "Fixpoint: " << x;
bool f=(status() == Gecode::SS_FAILED);
olog << std::endl << ind(3) << " --> " << x << std::endl;
return f;
} else {
return status() == Gecode::SS_FAILED;
}
}
void
TestSpace::rel(int i, Gecode::FloatRelType frt, Gecode::FloatVal n) {
if (opt.log) {
olog << ind(4) << "x[" << i << "] ";
switch (frt) {
case Gecode::FRT_EQ: olog << "="; break;
case Gecode::FRT_NQ: olog << "!="; break;
case Gecode::FRT_LQ: olog << "<="; break;
case Gecode::FRT_LE: olog << "<"; break;
case Gecode::FRT_GQ: olog << ">="; break;
case Gecode::FRT_GR: olog << ">"; break;
}
olog << " [" << n.min() << "," << n.max() << "]" << std::endl;
}
Gecode::rel(*this, x[i], frt, n);
}
void
TestSpace::rel(bool sol) {
int n = sol ? 1 : 0;
assert(reified);
if (opt.log)
olog << ind(4) << "b = " << n << std::endl;
Gecode::rel(*this, r.var(), Gecode::IRT_EQ, n);
}
void
TestSpace::assign(const Assignment& a, MaybeType& sol, bool skip) {
using namespace Gecode;
int i = skip ? static_cast<int>(Base::rand(a.size())) : -1;
for (int j=a.size(); j--; )
if (i != j) {
if ((x[j].min() == a[j].max()) || (x[j].max() == a[j].min()))
{
sol = MT_MAYBE;
return;
}
rel(j, FRT_EQ, a[j]);
if (Base::fixpoint() && failed())
return;
}
}
void
TestSpace::bound(void) {
using namespace Gecode;
// Select variable to be assigned
int i = Base::rand(x.size());
while (x[i].assigned()) {
i = (i+1) % x.size();
}
bool min = Base::rand(2);
if (min)
rel(i, FRT_LQ, nextafter(x[i].min(), x[i].max()));
else
rel(i, FRT_GQ, nextafter(x[i].max(), x[i].min()));
}
Gecode::FloatNum
TestSpace::cut(int* cutDirections) {
using namespace Gecode;
using namespace Gecode::Float;
// Select variable to be cut
int i = 0;
while (x[i].assigned()) i++;
for (int j=x.size(); j--; ) {
if (!x[j].assigned() && (x[j].size() > x[i].size())) i = j;
}
Rounding r;
if (cutDirections[i]) {
FloatNum m = r.div_up(r.add_up(x[i].min(),x[i].max()),2);
FloatNum n = nextafter(x[i].min(), x[i].max());
if (m > n)
rel(i, FRT_LQ, m);
else
rel(i, FRT_LQ, n);
} else {
FloatNum m = r.div_down(r.add_down(x[i].min(),x[i].max()),2);
FloatNum n = nextafter(x[i].max(), x[i].min());
if (m < n)
rel(i, FRT_GQ, m);
else
rel(i, FRT_GQ, n);
}
return x[i].size();
}
void
TestSpace::prune(int i) {
using namespace Gecode;
// Prune values
if (Base::rand(2) && !x[i].assigned()) {
Gecode::FloatNum v=randFValUp(x[i].min(),x[i].max());
assert((v >= x[i].min()) && (v <= x[i].max()));
rel(i, Gecode::FRT_LQ, v);
}
if (Base::rand(2) && !x[i].assigned()) {
Gecode::FloatNum v=randFValDown(x[i].min(),x[i].max());
assert((v <= x[i].max()) && (v >= x[i].min()));
rel(i, Gecode::FRT_GQ, v);
}
}
void
TestSpace::prune(void) {
using namespace Gecode;
// Select variable to be pruned
int i = Base::rand(x.size());
while (x[i].assigned()) {
i = (i+1) % x.size();
}
prune(i);
}
bool
TestSpace::prune(const Assignment& a, bool testfix) {
// Select variable to be pruned
int i = Base::rand(x.size());
while (x[i].assigned())
i = (i+1) % x.size();
// Select mode for pruning
switch (Base::rand(2)) {
case 0:
if (a[i].max() < x[i].max()) {
Gecode::FloatNum v=randFValDown(a[i].max(),x[i].max());
if (v==x[i].max())
v = a[i].max();
assert((v >= a[i].max()) && (v <= x[i].max()));
rel(i, Gecode::FRT_LQ, v);
}
break;
case 1:
if (a[i].min() > x[i].min()) {
Gecode::FloatNum v=randFValUp(x[i].min(),a[i].min());
if (v==x[i].min())
v = a[i].min();
assert((v <= a[i].min()) && (v >= x[i].min()));
rel(i, Gecode::FRT_GQ, v);
}
break;
}
if (Base::fixpoint()) {
if (failed() || !testfix)
return true;
TestSpace* c = static_cast<TestSpace*>(clone());
if (opt.log)
olog << ind(3) << "Testing fixpoint on copy" << std::endl;
c->post();
if (c->failed()) {
delete c; return false;
}
for (int i=x.size(); i--; )
if (x[i].size() != c->x[i].size()) {
delete c; return false;
}
if (reified && (r.var().size() != c->r.var().size())) {
delete c; return false;
}
if (opt.log)
olog << ind(3) << "Finished testing fixpoint on copy" << std::endl;
delete c;
}
return true;
}
unsigned int
TestSpace::propagators(void) {
return Gecode::PropagatorGroup::all.size(*this);
}
const Gecode::FloatRelType FloatRelTypes::frts[] =
{Gecode::FRT_EQ,Gecode::FRT_NQ,Gecode::FRT_LQ,Gecode::FRT_LE,
Gecode::FRT_GQ,Gecode::FRT_GR};
Assignment*
Test::assignment(void) const {
switch (assigmentType) {
case CPLT_ASSIGNMENT:
return new CpltAssignment(arity,dom,step);
case RANDOM_ASSIGNMENT:
return new RandomAssignment(arity,dom,step);
case EXTEND_ASSIGNMENT:
return new ExtAssignment(arity,dom,step,this);
default :
GECODE_NEVER;
}
return NULL; // Avoid compiler warnings
}
bool
Test::extendAssignement(Assignment&) const {
GECODE_NEVER;
return false;
}
bool
Test::subsumed(const TestSpace& ts) const {
if (!testsubsumed) return true;
if (const_cast<TestSpace&>(ts).propagators() == 0) return true;
if (assigmentType == EXTEND_ASSIGNMENT) return true;
return false;
}
#define CHECK_TEST(T,M) \
if (opt.log) \
olog << ind(3) << "Check: " << (M) << std::endl; \
if (!(T)) { \
problem = (M); delete s; goto failed; \
}
#define START_TEST(T) \
if (opt.log) { \
olog.str(""); \
olog << ind(2) << "Testing: " << (T) << std::endl; \
} \
test = (T);
bool
Test::ignore(const Assignment&) const {
return false;
}
void
Test::post(Gecode::Space&, Gecode::FloatVarArray&,
Gecode::Reify) {}
bool
Test::run(void) {
using namespace Gecode;
const char* test = "NONE";
const char* problem = "NONE";
// Set up assignments
Assignment* ap = assignment();
Assignment& a = *ap;
// Set up space for all solution search
TestSpace* search_s = new TestSpace(arity,dom,step,this);
post(*search_s,search_s->x);
branch(*search_s,search_s->x,FLOAT_VAR_NONE(),FLOAT_VAL_SPLIT_MIN());
Search::Options search_o;
search_o.threads = 1;
DFS<TestSpace> * e_s = new DFS<TestSpace>(search_s,search_o);
while (a()) {
MaybeType sol = solution(a);
if (opt.log) {
olog << ind(1) << "Assignment: " << a;
switch (sol) {
case MT_TRUE: olog << " (solution)"; break;
case MT_FALSE: olog << " (no solution)"; break;
case MT_MAYBE: olog << " (maybe)"; break;
}
olog << std::endl;
}
START_TEST("Assignment (after posting)");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
TestSpace* sc = NULL;
s->post();
switch (Base::rand(2)) {
case 0:
if (opt.log)
olog << ind(3) << "No copy" << std::endl;
sc = s;
s = NULL;
break;
case 1:
if (opt.log)
olog << ind(3) << "Copy" << std::endl;
if (s->status() != SS_FAILED) {
sc = static_cast<TestSpace*>(s->clone());
} else {
sc = s; s = NULL;
}
break;
default: assert(false);
}
sc->assign(a,sol);
if (sol == MT_TRUE) {
CHECK_TEST(!sc->failed(), "Failed on solution");
CHECK_TEST(subsumed(*sc), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(sc->failed(), "Solved on non-solution");
}
delete s; delete sc;
}
START_TEST("Partial assignment (after posting)");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
s->post();
s->assign(a,sol,true);
(void) s->failed();
s->assign(a,sol);
if (sol == MT_TRUE) {
CHECK_TEST(!s->failed(), "Failed on solution");
CHECK_TEST(subsumed(*s), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->failed(), "Solved on non-solution");
}
delete s;
}
START_TEST("Assignment (after posting, disable)");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
s->post();
s->disable();
s->enable();
s->assign(a,sol);
if (sol == MT_TRUE) {
CHECK_TEST(!s->failed(), "Failed on solution");
CHECK_TEST(subsumed(*s), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->failed(), "Solved on non-solution");
}
delete s;
}
START_TEST("Partial assignment (after posting, disable)");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
s->post();
s->disable();
s->enable();
s->assign(a,sol,true);
(void) s->failed();
s->assign(a,sol);
if (sol == MT_TRUE) {
CHECK_TEST(!s->failed(), "Failed on solution");
CHECK_TEST(subsumed(*s), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->failed(), "Solved on non-solution");
}
delete s;
}
START_TEST("Assignment (before posting)");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
s->assign(a,sol);
s->post();
if (sol == MT_TRUE) {
CHECK_TEST(!s->failed(), "Failed on solution");
CHECK_TEST(subsumed(*s), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->failed(), "Solved on non-solution");
}
delete s;
}
START_TEST("Partial assignment (before posting)");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
s->assign(a,sol,true);
s->post();
(void) s->failed();
s->assign(a,sol);
if (sol == MT_TRUE) {
CHECK_TEST(!s->failed(), "Failed on solution");
CHECK_TEST(subsumed(*s), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->failed(), "Solved on non-solution");
}
delete s;
}
START_TEST("Prune");
{
TestSpace* s = new TestSpace(arity,dom,step,this);
s->post();
while (!s->failed() && !s->assigned() && !s->matchAssignment(a))
if (!s->prune(a,testfix)) {
problem = "No fixpoint";
delete s;
goto failed;
}
s->assign(a,sol);
if (sol == MT_TRUE) {
CHECK_TEST(!s->failed(), "Failed on solution");
CHECK_TEST(subsumed(*s), "No subsumption");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->failed(), "Solved on non-solution");
}
delete s;
}
if (reified && !ignore(a) && (sol != MT_MAYBE)) {
if (eqv()) {
START_TEST("Assignment reified (rewrite after post, <=>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_EQV);
s->post();
s->rel(sol == MT_TRUE);
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
delete s;
}
if (imp()) {
START_TEST("Assignment reified (rewrite after post, =>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_IMP);
s->post();
s->rel(sol == MT_TRUE);
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
delete s;
}
if (pmi()) {
START_TEST("Assignment reified (rewrite after post, <=)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_PMI);
s->post();
s->rel(sol == MT_TRUE);
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
delete s;
}
if (eqv()) {
START_TEST("Assignment reified (immediate rewrite, <=>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_EQV);
s->rel(sol == MT_TRUE);
s->post();
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
delete s;
}
if (imp()) {
START_TEST("Assignment reified (immediate rewrite, =>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_IMP);
s->rel(sol == MT_TRUE);
s->post();
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
delete s;
}
if (pmi()) {
START_TEST("Assignment reified (immediate rewrite, <=)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_PMI);
s->rel(sol == MT_TRUE);
s->post();
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
delete s;
}
if (eqv()) {
START_TEST("Assignment reified (before posting, <=>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_EQV);
s->assign(a,sol);
s->post();
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (s->r.var().assigned()) {
if (sol == MT_TRUE) {
CHECK_TEST(s->r.var().val()==1, "Zero on solution");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->r.var().val()==0, "One on non-solution");
}
}
delete s;
}
if (imp()) {
START_TEST("Assignment reified (before posting, =>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_IMP);
s->assign(a,sol);
s->post();
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (sol == MT_TRUE) {
CHECK_TEST(!s->r.var().assigned(), "Control variable assigned");
} else if ((sol = MT_FALSE) && s->r.var().assigned()) {
CHECK_TEST(s->r.var().val()==0, "One on non-solution");
}
delete s;
}
if (pmi()) {
START_TEST("Assignment reified (before posting, <=)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_PMI);
s->assign(a,sol);
s->post();
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (sol == MT_TRUE) {
if (s->r.var().assigned()) {
CHECK_TEST(s->r.var().val()==1, "Zero on solution");
}
} else if (sol == MT_FALSE) {
CHECK_TEST(!s->r.var().assigned(), "Control variable assigned");
}
delete s;
}
if (eqv()) {
START_TEST("Assignment reified (after posting, <=>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_EQV);
s->post();
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (s->r.var().assigned()) {
if (sol == MT_TRUE) {
CHECK_TEST(s->r.var().val()==1, "Zero on solution");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->r.var().val()==0, "One on non-solution");
}
}
delete s;
}
if (imp()) {
START_TEST("Assignment reified (after posting, =>)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_IMP);
s->post();
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (sol == MT_TRUE) {
CHECK_TEST(!s->r.var().assigned(), "Control variable assigned");
} else if (s->r.var().assigned()) {
CHECK_TEST(s->r.var().val()==0, "One on non-solution");
}
delete s;
}
if (pmi()) {
START_TEST("Assignment reified (after posting, <=)");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_PMI);
s->post();
s->assign(a,sol);
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (sol == MT_TRUE) {
if (s->r.var().assigned()) {
CHECK_TEST(s->r.var().val()==1, "Zero on solution");
}
} else if (sol == MT_FALSE) {
CHECK_TEST(!s->r.var().assigned(), "Control variable assigned");
}
delete s;
}
if (eqv()) {
START_TEST("Prune reified, <=>");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_EQV);
s->post();
while (!s->failed() && !s->matchAssignment(a) &&
(!s->assigned() || !s->r.var().assigned()))
if (!s->prune(a,testfix)) {
problem = "No fixpoint";
delete s;
goto failed;
}
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (s->r.var().assigned()) {
if (sol == MT_TRUE) {
CHECK_TEST(s->r.var().val()==1, "Zero on solution");
} else if (sol == MT_FALSE) {
CHECK_TEST(s->r.var().val()==0, "One on non-solution");
}
}
delete s;
}
if (imp()) {
START_TEST("Prune reified, =>");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_IMP);
s->post();
while (!s->failed() && !s->matchAssignment(a) &&
(!s->assigned() || ((sol == MT_FALSE) &&
!s->r.var().assigned())))
if (!s->prune(a,testfix)) {
problem = "No fixpoint";
delete s;
goto failed;
}
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if (sol == MT_TRUE) {
CHECK_TEST(!s->r.var().assigned(), "Control variable assigned");
} else if ((sol == MT_FALSE) && s->r.var().assigned()) {
CHECK_TEST(s->r.var().val()==0, "One on non-solution");
}
delete s;
}
if (pmi()) {
START_TEST("Prune reified, <=");
TestSpace* s = new TestSpace(arity,dom,step,this,RM_PMI);
s->post();
while (!s->failed() && !s->matchAssignment(a) &&
(!s->assigned() || ((sol == MT_TRUE) &&
!s->r.var().assigned())))
if (!s->prune(a,testfix)) {
problem = "No fixpoint";
delete s;
goto failed;
}
CHECK_TEST(!s->failed(), "Failed");
CHECK_TEST(subsumed(*s), "No subsumption");
if ((sol == MT_TRUE) && s->r.var().assigned()) {
CHECK_TEST(s->r.var().val()==1, "Zero on solution");
} else if (sol == MT_FALSE) {
CHECK_TEST(!s->r.var().assigned(), "Control variable assigned");
}
delete s;
}
}
if (testsearch) {
if (sol == MT_TRUE) {
START_TEST("Search");
if (!search_s->failed()) {
TestSpace* ss = static_cast<TestSpace*>(search_s->clone());
ss->dropUntil(a);
delete e_s;
e_s = new DFS<TestSpace>(ss,search_o);
delete ss;
}
TestSpace* s = e_s->next();
CHECK_TEST(s != NULL, "Solutions exhausted");
CHECK_TEST(subsumed(*s), "No subsumption");
for (int i=a.size(); i--; ) {
CHECK_TEST(s->x[i].assigned(), "Unassigned variable");
CHECK_TEST(Gecode::Float::overlap(a[i], s->x[i].val()), "Wrong value in solution");
}
delete s;
}
}
++a;
}
if (testsearch) {
test = "Search";
if (!search_s->failed()) {
TestSpace* ss = static_cast<TestSpace*>(search_s->clone());
ss->dropUntil(a);
delete e_s;
e_s = new DFS<TestSpace>(ss,search_o);
delete ss;
}
if (e_s->next() != NULL) {
problem = "Excess solutions";
goto failed;
}
}
delete ap;
delete search_s;
delete e_s;
return true;
failed:
if (opt.log)
olog << "FAILURE" << std::endl
<< ind(1) << "Test: " << test << std::endl
<< ind(1) << "Problem: " << problem << std::endl;
if (a() && opt.log)
olog << ind(1) << "Assignment: " << a << std::endl;
delete ap;
delete search_s;
delete e_s;
return false;
}
}}
#undef START_TEST
#undef CHECK_TEST
// STATISTICS: test-float
Gecode::BoolVar
Boolean integer variables.
Definition int.hh:512
Gecode::BoolVar::val
int val(void) const
Return assigned value.
Definition bool.hpp:57
Gecode::DFS
Depth-first search engine.
Definition search.hh:1036
Gecode::FloatVal
Float value type.
Definition float.hh:334
Gecode::FloatVal::max
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:386
Gecode::FloatVal::min
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:398
Gecode::FloatVal::size
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
Gecode::FloatVarArgs
Passing float variables.
Definition float.hh:979
Gecode::FloatVarArray
Float variable array.
Definition float.hh:1030
Gecode::Float::Rounding
Floating point rounding policy.
Definition float.hh:154
Gecode::Options
Options for scripts
Definition driver.hh:366
Gecode::Options::threads
void threads(double n)
Set number of parallel threads.
Definition options.hpp:292
Gecode::PropagatorGroup::all
static PropagatorGroup all
Group of all propagators.
Definition core.hpp:789
Gecode::Reify
Reification specification.
Definition int.hh:876
Gecode::Reify::var
BoolVar var(void) const
Return Boolean control variable.
Definition reify.hpp:48
Gecode::Reify::mode
ReifyMode mode(void) const
Return reification mode.
Definition reify.hpp:56
Gecode::Search::Base::next
virtual T * next(void)
Return next solution (NULL, if none exists or search has been stopped).
Definition base.hpp:46
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Space::c
struct Gecode::Space::@055132133326276162005044145100211202071356247106::@155123175027073262103111264343315000271204104107 c
Data available only during copying.
Gecode::VarArray::assigned
bool assigned(void) const
Test if all variables are assigned.
Definition array.hpp:1026
Gecode::VarImpVar::assigned
bool assigned(void) const
Test whether view is assigned.
Definition var.hpp:111
Test::Base::rand
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Test::Base::fixpoint
static bool fixpoint(void)
Throw a coin whether to compute a fixpoint.
Definition test.hpp:66
Test::Float::Assignment
Base class for assignments
Definition float.hh:80
Test::Float::Assignment::n
int n
Number of variables.
Definition float.hh:82
Test::Float::Assignment::d
Gecode::FloatVal d
Domain for each variable.
Definition float.hh:83
Test::Float::CpltAssignment
Generate all assignments.
Definition float.hh:102
Test::Float::CpltAssignment::operator++
virtual void operator++(void)
Move to next assignment.
Definition float.cpp:50
Test::Float::CpltAssignment::step
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:105
Test::Float::CpltAssignment::dsv
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:104
Test::Float::ExtAssignment
Generate all assignments except the last variable and complete it to get a solution.
Definition float.hh:122
Test::Float::ExtAssignment::dsv
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:125
Test::Float::ExtAssignment::operator++
virtual void operator++(void)
Move to next assignment.
Definition float.cpp:67
Test::Float::ExtAssignment::step
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:126
Test::Float::ExtAssignment::curPb
const Test * curPb
Current problem used to complete assignment.
Definition float.hh:124
Test::Float::RandomAssignment
Generate random selection of assignments.
Definition float.hh:144
Test::Float::RandomAssignment::randval
Gecode::FloatNum randval(void)
Definition float.hpp:112
Test::Float::RandomAssignment::vals
Gecode::FloatVal * vals
The current values for the variables.
Definition float.hh:146
Test::Float::RandomAssignment::operator++
virtual void operator++(void)
Move to next assignment.
Definition float.cpp:89
Test::Float::RandomAssignment::a
int a
How many assigments still to be generated Generate new value according to domain.
Definition float.hh:147
Test::Float::TestSpace
Space for executing tests.
Definition float.hh:166
Test::Float::TestSpace::x
Gecode::FloatVarArray x
Variables to be tested.
Definition float.hh:173
Test::Float::TestSpace::prune
void prune(void)
Prune some random values for some random variable.
Definition float.cpp:348
Test::Float::TestSpace::cut
Gecode::FloatNum cut(int *cutDirections)
Cut the bigger variable to an half sized interval. It returns the new size of the cut interval....
Definition float.cpp:303
Test::Float::TestSpace::matchAssignment
bool matchAssignment(const Assignment &a) const
Test whether all variables match assignment a.
Definition float.cpp:211
Test::Float::TestSpace::r
Gecode::Reify r
Reification information.
Definition float.hh:175
Test::Float::TestSpace::bound
void bound(void)
Assing a random variable to a random bound.
Definition float.cpp:288
Test::Float::TestSpace::propagators
unsigned int propagators(void)
Return the number of propagators.
Definition float.cpp:410
Test::Float::TestSpace::prune
void prune(int i)
Prune some random values from variable i.
Definition float.cpp:332
Test::Float::TestSpace::disable
void disable(void)
Disable propagators in space and compute fixpoint (make all idle).
Definition float.cpp:191
Test::Float::TestSpace::d
Gecode::FloatVal d
Initial domain.
Definition float.hh:169
Test::Float::TestSpace::post
void post(void)
Post propagator.
Definition float.cpp:219
Test::Float::TestSpace::TestSpace
TestSpace(int n, Gecode::FloatVal &d, Gecode::FloatNum s, Test *t)
Create test space.
Definition float.cpp:143
Test::Float::TestSpace::test
Test * test
The test currently run.
Definition float.hh:177
Test::Float::TestSpace::reified
bool reified
Whether the test is for a reified propagator.
Definition float.hh:179
Test::Float::TestSpace::enable
void enable(void)
Enable propagators in space.
Definition float.cpp:186
Test::Float::TestSpace::failed
bool failed(void)
Compute a fixpoint and check for failure.
Definition float.cpp:232
Test::Float::TestSpace::assigned
bool assigned(void) const
Test whether all variables are assigned.
Definition float.cpp:203
Test::Float::TestSpace::dropUntil
virtual void dropUntil(const Assignment &a)
Add constraints to skip solutions to the a assignment.
Definition float.cpp:197
Test::Float::TestSpace::step
Gecode::FloatNum step
Step for going to next solution.
Definition float.hh:171
Test::Float::TestSpace::rel
void rel(int i, Gecode::FloatRelType frt, Gecode::FloatVal n)
Perform integer tell operation on x[i].
Definition float.cpp:244
Test::Float::TestSpace::assign
void assign(const Assignment &a, MaybeType &sol, bool skip=false)
Assign all (or all but one, if skip is true) variables to values in a If assignment of a variable is ...
Definition float.cpp:270
Test::Float::TestSpace::copy
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition float.cpp:181
Test::Float::Test::testsubsumed
bool testsubsumed
Whether to test for subsumption.
Definition float.hh:263
Test::Float::Test::extendAssignement
virtual bool extendAssignement(Assignment &a) const
Complete the current assignment to get a feasible one (which satisfies all constraint)....
Definition float.cpp:435
Test::Float::Test::testfix
bool testfix
Whether to perform fixpoint test.
Definition float.hh:261
Test::Float::Test::eqv
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition float.hpp:160
Test::Float::Test::arity
int arity
Number of variables.
Definition float.hh:247
Test::Float::Test::post
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)=0
Post constraint.
Test::Float::Test::run
virtual bool run(void)
Perform test.
Definition float.cpp:474
Test::Float::Test::step
Gecode::FloatNum step
Step for going to next solution.
Definition float.hh:251
Test::Float::Test::assignment
virtual Assignment * assignment(void) const
Create assignment.
Definition float.cpp:420
Test::Float::Test::pmi
bool pmi(void) const
Test whether reverse implication as reification mode is supported.
Definition float.hpp:168
Test::Float::Test::subsumed
bool subsumed(const TestSpace &ts) const
Test if ts is subsumed or not (i.e. if there is no more propagator unless the assignment is an extend...
Definition float.cpp:441
Test::Float::Test::reified
bool reified
Does the constraint also exist as reified constraint.
Definition float.hh:255
Test::Float::Test::testsearch
bool testsearch
Whether to perform search test.
Definition float.hh:259
Test::Float::Test::ignore
virtual bool ignore(const Assignment &a) const
Whether to ignore assignment for reification.
Definition float.cpp:465
Test::Float::Test::solution
virtual MaybeType solution(const Assignment &) const =0
Check for solution.
Test::Float::Test::assigmentType
AssignmentType assigmentType
Gives the type of assignment to use.
Definition float.hh:253
Test::Float::Test::dom
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:249
Test::Float::Test::imp
bool imp(void) const
Test whether implication as reification mode is supported.
Definition float.hpp:164
Test::ind
Simple class for describing identation.
Definition test.hh:66
Gecode::branch
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::FloatNum
double FloatNum
Floating point number base type.
Definition float.hh:106
Gecode::FloatRelType
FloatRelType
Relation types for floats.
Definition float.hh:1068
Gecode::FRT_EQ
@ FRT_EQ
Equality ( ).
Definition float.hh:1069
Gecode::FRT_GR
@ FRT_GR
Greater ( ).
Definition float.hh:1074
Gecode::FRT_NQ
@ FRT_NQ
Disequality ( ).
Definition float.hh:1070
Gecode::FRT_LE
@ FRT_LE
Less ( ).
Definition float.hh:1072
Gecode::FRT_LQ
@ FRT_LQ
Less or equal ( ).
Definition float.hh:1071
Gecode::FRT_GQ
@ FRT_GQ
Greater or equal ( ).
Definition float.hh:1073
Gecode::ReifyMode
ReifyMode
Mode for reification.
Definition int.hh:848
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::RM_IMP
@ RM_IMP
Implication for reification.
Definition int.hh:862
Gecode::RM_PMI
@ RM_PMI
Inverse implication for reification.
Definition int.hh:869
Gecode::RM_EQV
@ RM_EQV
Equivalence for reification (default).
Definition int.hh:855
Gecode::Space::Space
Space(void)
Default constructor.
Definition core.cpp:115
Gecode::Space::status
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition core.cpp:252
Gecode::Space::clone
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition core.hpp:3224
Gecode::SS_FAILED
@ SS_FAILED
Space is failed
Definition core.hpp:1682
Gecode::Float
Floating point numbers.
Gecode::Float::overlap
bool overlap(const FloatVal &x, const FloatVal &y)
Definition val.hpp:498
Gecode::Int::Limits
Numerical limits for integer variables.
Definition int.hh:114
Gecode::Int::Limits::max
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode
Gecode toplevel namespace
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::dom
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
Gecode::FLOAT_VAR_NONE
FloatVarBranch FLOAT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:97
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
Gecode::FLOAT_VAL_SPLIT_MIN
FloatValBranch FLOAT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition val.hpp:55
Test::Float
Testing domain floats.
Definition float.cpp:43
Test::Float::RANDOM_ASSIGNMENT
@ RANDOM_ASSIGNMENT
Definition float.hh:63
Test::Float::EXTEND_ASSIGNMENT
@ EXTEND_ASSIGNMENT
Definition float.hh:64
Test::Float::CPLT_ASSIGNMENT
@ CPLT_ASSIGNMENT
Definition float.hh:62
Test::Float::randFValDown
Gecode::FloatNum randFValDown(Gecode::FloatNum l, Gecode::FloatNum u)
Definition float.cpp:108
Test::Float::MaybeType
MaybeType
Type for comparisons and solutions.
Definition float.hh:51
Test::Float::MT_MAYBE
@ MT_MAYBE
Definition float.hh:54
Test::Float::MT_FALSE
@ MT_FALSE
Definition float.hh:52
Test::Float::MT_TRUE
@ MT_TRUE
Definition float.hh:53
Test::Float::randFValUp
Gecode::FloatNum randFValUp(Gecode::FloatNum l, Gecode::FloatNum u)
Definition float.cpp:125
Test
General test support.
Definition afc.cpp:39
Test::olog
std::ostringstream olog
Stream used for logging.
Definition test.cpp:53
Test::r
Region r
Definition region.cpp:65
Test::opt
Options opt
The options.
Definition test.cpp:97
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56
START_TEST
#define START_TEST(T)
Start new test.
Definition array.cpp:48
CHECK_TEST
#define CHECK_TEST(T, M)
Check the test result and handle failed test.
Definition array.cpp:40
operator<<
std::ostream & operator<<(std::ostream &os, const Test::Float::Assignment &a)
Definition float.cpp:98
float.hh