Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
38 kB · 1151 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * Mikael Lagerkvist <lagerkvist@gecode.org> * * Copyright: * Christian Schulte, 2005 * Mikael Lagerkvist, 2005 * * 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 <algorithm>
namespace Test { namespace Int {
/* * Complete assignments * */ void CpltAssignment::next(Gecode::Support::RandomGenerator&) { int i = n-1; while (true) { ++dsv[i]; if (dsv[i]() || (i == 0)) return; dsv[i--].init(d); } }
/* * Random assignments * */ void RandomAssignment::next(Gecode::Support::RandomGenerator& rand) { for (int i = this->n; i--; ) this->vals[i]= this->randval(rand); this->a--; }
void RandomMixAssignment::next(Gecode::Support::RandomGenerator& rand) { for (int i=n-_n1; i--; ) vals[i] = randval(d, rand); for (int i=_n1; i--; ) vals[n-_n1+i] = randval(_d1, rand); a--; }
}}
std::ostream&operator<<(std::ostream& os, const Test::Int::Assignment& a) { int n = a.size(); os << "{"; for (int i=0; i<n; i++) os << a[i] << ((i!=n-1) ? "," : "}"); return os;}
namespace Test { namespace Int {
TestSpace::TestSpace(int n, Gecode::IntSet& d0, Test* t) : d(d0), x(*this,n,Gecode::Int::Limits::min,Gecode::Int::Limits::max), test(t), reified(false) { Gecode::IntVarArgs _x(*this,n,d); 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::IntSet& d0, Test* t, Gecode::ReifyMode rm) : d(d0), x(*this,n,Gecode::Int::Limits::min,Gecode::Int::Limits::max), test(t), reified(true) { Gecode::IntVarArgs _x(*this,n,d); 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,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), 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); }
bool TestSpace::assigned(void) const { for (int i=x.size(); i--; ) if (!x[i].assigned()) 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; } }
int TestSpace::rndvar(Gecode::Support::RandomGenerator& rand) { assert(!assigned()); // Select variable to be pruned int i = static_cast<int>(rand(static_cast<unsigned int>(x.size()))); while (x[i].assigned()) { i = (i+1) % x.size(); } return i; }
void TestSpace::rndrel(const Assignment& a, int i, Gecode::IntRelType& irt, int& v, Gecode::Support::RandomGenerator& rand) { using namespace Gecode; // Select mode for pruning irt = IRT_EQ; // Means do nothing! switch (rand(3)) { case 0: if (a[i] < x[i].max()) { v=a[i]+1+ static_cast<int>(rand(static_cast<unsigned int>(x[i].max()-a[i]))); assert((v > a[i]) && (v <= x[i].max())); irt = IRT_LE; } break; case 1: if (a[i] > x[i].min()) { v=x[i].min()+ static_cast<int>(rand(static_cast<unsigned int>(a[i]-x[i].min()))); assert((v < a[i]) && (v >= x[i].min())); irt = IRT_GR; } break; default: { Gecode::Int::ViewRanges<Gecode::Int::IntView> it(x[i]); unsigned int skip = rand(static_cast<unsigned int>(x[i].size()-1)); while (true) { if (it.width() > skip) { v = it.min() + static_cast<int>(skip); if (v == a[i]) { if (it.width() == 1) { ++it; v = it.min(); } else if (v < it.max()) { ++v; } else { --v; } } break; } skip -= it.width(); ++it; } irt = IRT_NQ; break; } } }
void TestSpace::rel(int i, Gecode::IntRelType irt, int n) { if (opt.log) { olog << ind(4) << "x[" << i << "] "; switch (irt) { case Gecode::IRT_EQ: olog << "="; break; case Gecode::IRT_NQ: olog << "!="; break; case Gecode::IRT_LQ: olog << "<="; break; case Gecode::IRT_LE: olog << "<"; break; case Gecode::IRT_GQ: olog << ">="; break; case Gecode::IRT_GR: olog << ">"; break; } olog << " " << n << std::endl; } Gecode::rel(*this, x[i], irt, 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, bool skip, Gecode::Support::RandomGenerator& rand) { using namespace Gecode; int i = skip ? static_cast<int>(rand(static_cast<unsigned int>(a.size()))) : -1; for (int j=a.size(); j--; ) if (i != j) { rel(j, IRT_EQ, a[j]); if (Base::fixpoint(rand) && failed()) return; } }
void TestSpace::bound(Gecode::Support::RandomGenerator& rand) { using namespace Gecode; int i = rndvar(rand); bool min = rand(2); rel(i, IRT_EQ, min ? x[i].min() : x[i].max()); }
void TestSpace::prune(int i, bool bounds_only, Gecode::Support::RandomGenerator& rand) { using namespace Gecode; // Prune values if (bounds_only) { if (rand(2) && !x[i].assigned()) { int v=x[i].min()+1+ static_cast<int>(rand(static_cast<unsigned int>(x[i].max()-x[i].min()))); assert((v > x[i].min()) && (v <= x[i].max())); rel(i, Gecode::IRT_LE, v); } if (rand(2) && !x[i].assigned()) { int v=x[i].min()+ static_cast<int>(rand(static_cast<unsigned int>(x[i].max()-x[i].min()))); assert((v < x[i].max()) && (v >= x[i].min())); rel(i, Gecode::IRT_GR, v); } } else { for (int vals = static_cast<int>(rand(static_cast<unsigned int>(x[i].size()-1))+1); vals--; ) { int v; Gecode::Int::ViewRanges<Gecode::Int::IntView> it(x[i]); unsigned int skip = rand(x[i].size()-1); while (true) { if (it.width() > skip) { v = it.min() + static_cast<int>(skip); break; } skip -= it.width(); ++it; } rel(i, IRT_NQ, v); } } }
void TestSpace::prune(Gecode::Support::RandomGenerator& rand) { prune(rndvar(rand), false, rand); }
bool TestSpace::prune(const Assignment& a, bool testfix, Gecode::Support::RandomGenerator& rand) { using namespace Gecode; // Select variable to be pruned int i = rndvar(rand); // Select mode for pruning IntRelType irt; int v; rndrel(a, i, irt, v, rand); if (irt != IRT_EQ) rel(i, irt, v); if (Base::fixpoint(rand)) { 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()) { if (opt.log) olog << ind(4) << "Copy failed after posting" << std::endl; delete c; return false; } for (int j=x.size(); j--; ) if (x[j].size() != c->x[j].size()) { if (opt.log) olog << ind(4) << "Different domain size" << std::endl; delete c; return false; } if (reified && (r.var().size() != c->r.var().size())) { if (opt.log) olog << ind(4) << "Different control variable" << std::endl; delete c; return false; } if (opt.log) olog << ind(3) << "Finished testing fixpoint on copy" << std::endl; delete c; } return true; }
void TestSpace::enable(void) { Gecode::PropagatorGroup::all.enable(*this); }
void TestSpace::disable(void) { Gecode::PropagatorGroup::all.disable(*this); (void) status(); }
bool TestSpace::disabled(const Assignment& a, TestSpace& c, bool testfix, Gecode::Support::RandomGenerator& rand) { using namespace Gecode; // Disable propagators c.disable(); // Select variable to be pruned int i = rndvar(rand); // Select mode for pruning IntRelType irt; int v; rndrel(a, i, irt, v, rand); if (irt != IRT_EQ) { rel(i, irt, v); c.rel(i, irt, v); } // Enable propagators c.enable(); if (!testfix) return true; if (failed()) { if (!c.failed()) { if (opt.log) olog << ind(3) << "No failure on disabled copy" << std::endl; return false; } return true; } if (c.failed()) { if (opt.log) olog << ind(3) << "Failure on disabled copy" << std::endl; return false; } for (int j=x.size(); j--; ) { if (x[j].size() != c.x[j].size()) { if (opt.log) olog << ind(4) << "Different domain size" << std::endl; return false; } if (reified && (r.var().size() != c.r.var().size())) { if (opt.log) olog << ind(4) << "Different control variable" << std::endl; return false; } } return true; }
unsigned int TestSpace::propagators(void) { return Gecode::PropagatorGroup::all.size(*this); }
const Gecode::IntPropLevel IntPropLevels::ipls[] = {Gecode::IPL_DOM,Gecode::IPL_BND,Gecode::IPL_VAL};
const Gecode::IntPropLevel IntPropBasicAdvanced::ipls[] = {Gecode::IPL_BASIC_ADVANCED,Gecode::IPL_ADVANCED,Gecode::IPL_BASIC};
const Gecode::IntRelType IntRelTypes::irts[] = {Gecode::IRT_EQ,Gecode::IRT_NQ,Gecode::IRT_LQ, Gecode::IRT_LE,Gecode::IRT_GQ,Gecode::IRT_GR};
const Gecode::BoolOpType BoolOpTypes::bots[] = {Gecode::BOT_AND,Gecode::BOT_OR,Gecode::BOT_IMP, Gecode::BOT_EQV,Gecode::BOT_XOR};
Assignment* Test::assignment(void) const { return new CpltAssignment(arity,dom); }
/// Check the test result and handle failed test#define CHECK_TEST(T,M) \do { \if (opt.log) \ olog << ind(3) << "Check: " << (M) << std::endl; \if (!(T)) { \ problem = (M); delete s; goto failed; \} \} while (false)
/// Start new test#define START_TEST(T) \do { \ if (opt.log) { \ olog.str(""); \ olog << ind(2) << "Testing: " << (T) << std::endl; \ } \ test = (T); \} while (false)
bool Test::ignore(const Assignment&) const { return false; }
void Test::post(Gecode::Space&, Gecode::IntVarArray&, 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,this); post(*search_s,search_s->x); branch(*search_s,search_s->x,INT_VAR_NONE(),INT_VAL_MIN()); Search::Options search_o; search_o.threads = 1; DFS<TestSpace> e_s(search_s,search_o); delete search_s;
while (a.has_more()) { bool sol = solution(a); if (opt.log) { olog << ind(1) << "Assignment: " << a << (sol ? " (solution)" : " (no solution)") << std::endl; }
START_TEST("Assignment (after posting)"); { TestSpace* s = new TestSpace(arity,dom,this); TestSpace* sc = nullptr; s->post(); switch (_rand(2)) { case 0: if (opt.log) olog << ind(3) << "No copy" << std::endl; sc = s; s = nullptr; 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 = nullptr; } break; default: assert(false); } sc->assign(a, false, _rand); if (sol) { CHECK_TEST(!sc->failed(), "Failed on solution"); CHECK_TEST(sc->propagators()==0, "No subsumption"); } else { CHECK_TEST(sc->failed(), "Solved on non-solution"); } delete s; delete sc; } START_TEST("Partial assignment (after posting)"); { TestSpace* s = new TestSpace(arity,dom,this); s->post(); s->assign(a, true, _rand); (void) s->failed(); s->assign(a, false, _rand); if (sol) { CHECK_TEST(!s->failed(), "Failed on solution"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Solved on non-solution"); } delete s; } START_TEST("Assignment (after posting, disable)"); { TestSpace* s = new TestSpace(arity,dom,this); s->post(); s->disable(); s->assign(a, false, _rand); s->enable(); if (sol) { CHECK_TEST(!s->failed(), "Failed on solution"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Solved on non-solution"); } delete s; } START_TEST("Partial assignment (after posting, disable)"); { TestSpace* s = new TestSpace(arity,dom,this); s->post(); s->assign(a, true, _rand); s->disable(); (void) s->failed(); s->assign(a, false, _rand); s->enable(); if (sol) { CHECK_TEST(!s->failed(), "Failed on solution"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Solved on non-solution"); } delete s; } START_TEST("Assignment (before posting)"); { TestSpace* s = new TestSpace(arity,dom,this); s->assign(a, false, _rand); s->post(); if (sol) { CHECK_TEST(!s->failed(), "Failed on solution"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Solved on non-solution"); } delete s; } START_TEST("Partial assignment (before posting)"); { TestSpace* s = new TestSpace(arity,dom,this); s->assign(a, true, _rand); s->post(); (void) s->failed(); s->assign(a, false, _rand); if (sol) { CHECK_TEST(!s->failed(), "Failed on solution"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Solved on non-solution"); } delete s; } START_TEST("Prune"); { TestSpace* s = new TestSpace(arity,dom,this); s->post(); while (!s->failed() && !s->assigned()) if (!s->prune(a, testfix, _rand)) { problem = "No fixpoint"; delete s; goto failed; } s->assign(a, false, _rand); if (sol) { CHECK_TEST(!s->failed(), "Failed on solution"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Solved on non-solution"); } delete s; } START_TEST("Prune (disable)"); { TestSpace* s = new TestSpace(arity,dom,this); TestSpace* c = static_cast<TestSpace*>(s->clone()); s->post(); c->post(); while (!s->failed() && !s->assigned()) if (!s->disabled(a, *c, testfix, _rand)) { problem = "Different result after re-enable"; delete s; delete c; goto failed; } if (testfix && (s->failed() != c->failed())) { problem = "Different failure after re-enable"; delete s; delete c; goto failed; } delete s; delete c; } if (!ignore(a)) { if (eqv()) { { START_TEST("Assignment reified (rewrite after post, <=>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->post(); s->rel(sol); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); delete s; } { START_TEST("Assignment reified (rewrite failure, <=>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->post(); s->rel(!sol); s->assign(a, false, _rand); CHECK_TEST(s->failed(), "Not failed"); delete s; } { START_TEST("Assignment reified (immediate rewrite, <=>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->rel(sol); s->post(); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); delete s; } { START_TEST("Assignment reified (immediate failure, <=>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->rel(!sol); s->post(); s->assign(a, false, _rand); CHECK_TEST(s->failed(), "Not failed"); delete s; } { START_TEST("Assignment reified (before posting, <=>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->assign(a, false, _rand); s->post(); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); if (sol) { CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Assignment reified (after posting, <=>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->post(); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); if (sol) { CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Assignment reified (after posting, <=>, disable)"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->post(); s->disable(); s->assign(a, false, _rand); s->enable(); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); if (sol) { CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Prune reified, <=>"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); s->post(); while (!s->failed() && (!s->assigned() || !s->r.var().assigned())) if (!s->prune(a, testfix, _rand)) { problem = "No fixpoint"; delete s; goto failed; } CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); if (sol) { CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Prune reified, <=>, disable"); TestSpace* s = new TestSpace(arity,dom,this,RM_EQV); TestSpace* c = static_cast<TestSpace*>(s->clone()); s->post(); c->post(); while (!s->failed() && (!s->assigned() || !s->r.var().assigned())) if (!s->disabled(a, *c, testfix, _rand)) { problem = "No fixpoint"; delete s; delete c; goto failed; } CHECK_TEST(!c->failed(), "Failed"); CHECK_TEST(c->propagators()==0, "No subsumption"); CHECK_TEST(c->r.var().assigned(), "Control variable unassigned"); if (sol) { CHECK_TEST(c->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(c->r.var().val()==0, "One on non-solution"); } delete s; delete c; } }
if (imp()) { { START_TEST("Assignment reified (rewrite after post, =>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->post(); s->rel(sol); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); delete s; } { START_TEST("Assignment reified (rewrite failure, =>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->post(); s->rel(!sol); s->assign(a, false, _rand); if (sol) { CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Not failed"); } delete s; } { START_TEST("Assignment reified (immediate rewrite, =>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->rel(sol); s->post(); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); delete s; } { START_TEST("Assignment reified (immediate failure, =>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->rel(!sol); s->post(); s->assign(a, false, _rand); if (sol) { CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } else { CHECK_TEST(s->failed(), "Not failed"); } delete s; } { START_TEST("Assignment reified (before posting, =>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->assign(a, false, _rand); s->post(); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } else { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Assignment reified (after posting, =>)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->post(); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } else { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Assignment reified (after posting, =>, disable)"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->post(); s->disable(); s->assign(a, false, _rand); s->enable(); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } else { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Prune reified, =>"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); s->post(); while (!s->failed() && (!s->assigned() || (!sol && !s->r.var().assigned()))) if (!s->prune(a, testfix, _rand)) { problem = "No fixpoint"; delete s; goto failed; } CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } else { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==0, "One on non-solution"); } delete s; } { START_TEST("Prune reified, =>, disable"); TestSpace* s = new TestSpace(arity,dom,this,RM_IMP); TestSpace* c = static_cast<TestSpace*>(s->clone()); s->post(); c->post(); while (!s->failed() && (!s->assigned() || (!sol && !s->r.var().assigned()))) if (!s->disabled(a, *c, testfix, _rand)) { problem = "No fixpoint"; delete s; delete c; goto failed; } CHECK_TEST(!c->failed(), "Failed"); CHECK_TEST(c->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(!c->r.var().assigned(), "Control variable assigned"); } else { CHECK_TEST(c->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(c->r.var().val()==0, "One on non-solution"); } delete s; delete c; } }
if (pmi()) { { START_TEST("Assignment reified (rewrite after post, <=)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->post(); s->rel(sol); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); delete s; } { START_TEST("Assignment reified (rewrite failure, <=)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->post(); s->rel(!sol); s->assign(a, false, _rand); if (sol) { CHECK_TEST(s->failed(), "Not failed"); } else { CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } delete s; } { START_TEST("Assignment reified (immediate rewrite, <=)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->rel(sol); s->post(); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); delete s; } { START_TEST("Assignment reified (immediate failure, <=)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->rel(!sol); s->post(); s->assign(a, false, _rand); if (sol) { CHECK_TEST(s->failed(), "Not failed"); } else { CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } delete s; } { START_TEST("Assignment reified (before posting, <=)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->assign(a, false, _rand); s->post(); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } delete s; } { START_TEST("Assignment reified (after posting, <=)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->post(); s->assign(a, false, _rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } delete s; } { START_TEST("Assignment reified (after posting, <=, disable)"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->post(); s->disable(); s->assign(a, false, _rand); s->enable(); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } delete s; } { START_TEST("Prune reified, <="); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); s->post(); while (!s->failed() && (!s->assigned() || (sol && !s->r.var().assigned()))) if (!s->prune(a, testfix, _rand)) { problem = "No fixpoint"; delete s; goto failed; } CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(s->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(s->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(!s->r.var().assigned(), "Control variable assigned"); } delete s; } { START_TEST("Prune reified, <=, disable"); TestSpace* s = new TestSpace(arity,dom,this,RM_PMI); TestSpace* c = static_cast<TestSpace*>(s->clone()); s->post(); c->post(); while (!s->failed() && (!s->assigned() || (sol && !s->r.var().assigned()))) if (!s->disabled(a, *c, testfix, _rand)) { problem = "No fixpoint"; delete s; delete c; goto failed; } CHECK_TEST(!c->failed(), "Failed"); CHECK_TEST(c->propagators()==0, "No subsumption"); if (sol) { CHECK_TEST(c->r.var().assigned(), "Control variable unassigned"); CHECK_TEST(c->r.var().val()==1, "Zero on solution"); } else { CHECK_TEST(!c->r.var().assigned(), "Control variable assigned"); } delete s; delete c; } } }
if (testsearch) { if (sol) { START_TEST("Search"); TestSpace* s = e_s.next(); CHECK_TEST(s != nullptr, "Solutions exhausted"); CHECK_TEST(s->propagators()==0, "No subsumption"); for (int i=a.size(); i--; ) { CHECK_TEST(s->x[i].assigned(), "Unassigned variable"); CHECK_TEST(a[i] == s->x[i].val(), "Wrong value in solution"); } delete s; } }
a.next(_rand); }
if (testsearch) { test = "Search"; if (e_s.next() != nullptr) { problem = "Excess solutions"; goto failed; } }
switch (contest) { case CTL_NONE: break; case CTL_DOMAIN: { START_TEST("Full domain consistency"); TestSpace* s = new TestSpace(arity,dom,this); s->post(); if (!s->failed()) { while (!s->failed() && !s->assigned()) s->prune(_rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } delete s; // Fall-through -- domain implies bounds(d) and bounds(z) } case CTL_BOUNDS_D: { START_TEST("Bounds(D)-consistency"); TestSpace* s = new TestSpace(arity,dom,this); s->post(); for (int i = s->x.size(); i--; ) s->prune(i, false, _rand); if (!s->failed()) { while (!s->failed() && !s->assigned()) s->bound(_rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } delete s; // Fall-through -- bounds(d) implies bounds(z) } case CTL_BOUNDS_Z: { START_TEST("Bounds(Z)-consistency"); TestSpace* s = new TestSpace(arity,dom,this); s->post(); for (int i = s->x.size(); i--; ) s->prune(i, true, _rand); if (!s->failed()) { while (!s->failed() && !s->assigned()) s->bound(_rand); CHECK_TEST(!s->failed(), "Failed"); CHECK_TEST(s->propagators()==0, "No subsumption"); } delete s; break; } }
delete ap; return true;
failed: if (opt.log) olog << "FAILURE" << std::endl << ind(1) << "Test: " << test << std::endl << ind(1) << "Problem: " << problem << std::endl; if (a.has_more() && opt.log) olog << ind(1) << "Assignment: " << a << std::endl; delete ap;
return false; }
}}
#undef START_TEST#undef CHECK_TEST
// STATISTICS: test-int