/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Contributing authors: * Guido Tack * * Copyright: * Christian Schulte, 2013 * Guido Tack, 2004 * * 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 #include #include "test/test.hh" namespace Test { /// Tests for search using no-goods namespace NoGoods { using namespace Gecode; /// A dummy function for branching void dummy(Space&) { } /// Example for testing integer no-goods class Queens : public Space { public: /// Number of queens (must be even) const static int n = 18; /// Position of queens on boards IntVarArray q; /// The actual problem Queens(IntValBranch ivb, bool assign, bool null) : q(*this,n,0,n-1) { distinct(*this, IntArgs::create(n,0,1), q, IPL_VAL); distinct(*this, IntArgs::create(n,0,-1), q, IPL_VAL); distinct(*this, q, IPL_VAL); if (assign) { IntVar x(*this,0,1); Gecode::assign(*this, x, INT_ASSIGN_MIN()); } { IntVarArgs q1(n/2), q2(n/2); for (int i=0; i= distance); if (assign) { IntVar x(*this,0,1); Gecode::assign(*this, x, INT_ASSIGN_MIN()); } { SetVarArgs x1(size/2), x2(size/2); for (int i=0; i class NoGoods : public Base { protected: /// How to branch ValBranch vb; /// Number of threads to use unsigned int t; /// Whether to also assign some variables bool a; /// Whether to also create branchers without no-good literals bool n; public: /// Map unsigned integer to string static std::string str(unsigned int i) { std::stringstream s; s << i; return s.str(); } /// Initialize test NoGoods(ValBranch vb0, unsigned int t0, bool a0, bool n0) : Base("NoGoods::"+Model::name()+"::"+Model::val(vb0)+"::"+str(t0)+ "::"+(a0 ? "+" : "-")+"::"+(n0 ? "+" : "-")), vb(vb0), t(t0), a(a0), n(n0) {} /// Run test virtual bool run(void) { Model* m = new Model(vb,a,n); // Solution without no-goods Model* s_plain = dfs(m); // Stop and re-start for a while to collect no-goods { Search::NodeStop ns(Model::nodeinc()); Search::Options o; o.stop = &ns; o.threads = t; o.nogoods_limit = 256U; Search::Engine* e = Search::dfsengine(m,o); while (true) { Model* s = static_cast(e->next()); delete s; if (!e->stopped()) break; // Add no-goods e->nogoods().post(*m); ns.limit(ns.limit()+Model::nodeinc()); } delete e; } // Compare whether the a or the same solution is found with no-goods Model* s_nogoods = dfs(m); bool ok = ((s_nogoods != nullptr) && ((t != 1) || s_plain->same(*s_nogoods))); delete m; delete s_nogoods; delete s_plain; return ok; } }; /// Help class to create and register tests class Create { public: /// Perform creation and registration Create(void) { bool a = false; do { bool n = false; do { for (unsigned int t = 1; t<=4; t++) { (void) new NoGoods(INT_VAL_MIN(),t,a,n); (void) new NoGoods(INT_VAL_MAX(),t,a,n); (void) new NoGoods(INT_VAL_SPLIT_MIN(),t,a,n); (void) new NoGoods(INT_VAL_SPLIT_MAX(),t,a,n); (void) new NoGoods(INT_VALUES_MIN(),t,a,n); (void) new NoGoods(INT_VALUES_MAX(),t,a,n); #ifdef GECODE_HAS_SET_VARS (void) new NoGoods(SET_VAL_MIN_INC(),t,a,n); (void) new NoGoods(SET_VAL_MIN_EXC(),t,a,n); (void) new NoGoods(SET_VAL_MAX_INC(),t,a,n); (void) new NoGoods(SET_VAL_MAX_EXC(),t,a,n); #endif } n = !n; } while (n); a = !a; } while (a); } }; Create c; } } // STATISTICS: test-search