this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Copyright: 7 * Guido Tack, 2012 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34 35#include <gecode/search/seq/rbs.hh> 36 37namespace Gecode { namespace Search { namespace Seq { 38 39 bool 40 RestartStop::stop(const Statistics& s, const Options& o) { 41 // Stop if the fail limit for the engine says so 42 if (s.fail > l) { 43 e_stopped = true; 44 m_stat.restart++; 45 return true; 46 } 47 // Stop if the stop object for the meta engine says so 48 if ((m_stop != nullptr) && m_stop->stop(m_stat+s,o)) { 49 e_stopped = false; 50 return true; 51 } 52 return false; 53 } 54 55 Space* 56 RBS::next(void) { 57 if (restart) { 58 restart = false; 59 sslr++; 60 NoGoods& ng = e->nogoods(); 61 // Reset number of no-goods found 62 ng.ng(0); 63 MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng); 64 bool r = master->master(mi); 65 stop->m_stat.nogood += ng.ng(); 66 if (master->status(stop->m_stat) == SS_FAILED) { 67 stop->update(e->statistics()); 68 delete master; 69 master = nullptr; 70 e->reset(nullptr); 71 return nullptr; 72 } else if (r) { 73 stop->update(e->statistics()); 74 Space* slave = master; 75 master = master->clone(); 76 complete = slave->slave(mi); 77 e->reset(slave); 78 sslr = 0; 79 stop->m_stat.restart++; 80 } 81 } 82 while (true) { 83 Space* n = e->next(); 84 if (n != nullptr) { 85 // The engine found a solution 86 restart = true; 87 delete last; 88 last = n->clone(); 89 return n; 90 } else if ( (!complete && !e->stopped()) || 91 (e->stopped() && stop->enginestopped()) ) { 92 // The engine must perform a true restart 93 // The number of the restart has been incremented in the stop object 94 sslr = 0; 95 NoGoods& ng = e->nogoods(); 96 ng.ng(0); 97 MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng); 98 (void) master->master(mi); 99 stop->m_stat.nogood += ng.ng(); 100 unsigned long long int nl = ++(*co); 101 stop->limit(e->statistics(),nl); 102 if (master->status(stop->m_stat) == SS_FAILED) 103 return nullptr; 104 Space* slave = master; 105 master = master->clone(); 106 complete = slave->slave(mi); 107 e->reset(slave); 108 } else { 109 return nullptr; 110 } 111 } 112 GECODE_NEVER; 113 return nullptr; 114 } 115 116 Search::Statistics 117 RBS::statistics(void) const { 118 return stop->metastatistics()+e->statistics(); 119 } 120 121 void 122 RBS::constrain(const Space& b) { 123 if (!best) 124 throw NoBest("RBS::constrain"); 125 if (last != nullptr) { 126 last->constrain(b); 127 if (last->status() == SS_FAILED) { 128 delete last; 129 } else { 130 return; 131 } 132 } 133 last = b.clone(); 134 master->constrain(b); 135 e->constrain(b); 136 } 137 138 bool 139 RBS::stopped(void) const { 140 /* 141 * What might happen during parallel search is that the 142 * engine has been stopped but the meta engine has not, so 143 * the meta engine does not perform a restart. However the 144 * invocation of next will do so and no restart will be 145 * missed. 146 */ 147 return e->stopped(); 148 } 149 150 RBS::~RBS(void) { 151 delete e; 152 delete master; 153 delete last; 154 delete co; 155 delete stop; 156 } 157 158}}} 159 160// STATISTICS: search-seq