Generated on Tue Apr 18 10:22:10 2017 for Gecode by doxygen 1.6.3

rbs.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2012
00008  *
00009  *  Last modified:
00010  *     $Date: 2017-03-28 16:18:06 +0200 (Tue, 28 Mar 2017) $ by $Author: schulte $
00011  *     $Revision: 15620 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 
00039 #include <gecode/search/meta/rbs.hh>
00040 
00041 namespace Gecode { namespace Search { namespace Meta {
00042 
00043   bool
00044   RestartStop::stop(const Statistics& s, const Options& o) {
00045     // Stop if the fail limit for the engine says so
00046     if (s.fail > l) {
00047       e_stopped = true;
00048       m_stat.restart++;
00049       return true;
00050     }
00051     // Stop if the stop object for the meta engine says so
00052     if ((m_stop != NULL) && m_stop->stop(m_stat+s,o)) {
00053       e_stopped = false;
00054       return true;
00055     }
00056     return false;
00057   }
00058 
00059 
00060   Space*
00061   RBS::next(void) {
00062     if (restart) {
00063       restart = false;
00064       sslr++;
00065       NoGoods& ng = e->nogoods();
00066       // Reset number of no-goods found
00067       ng.ng(0);
00068       MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
00069       bool r = master->master(mi);
00070       stop->m_stat.nogood += ng.ng();
00071       if (master->status(stop->m_stat) == SS_FAILED) {
00072         stop->update(e->statistics());
00073         delete master;
00074         master = NULL;
00075         e->reset(NULL);
00076         return NULL;
00077       } else if (r) {
00078         stop->update(e->statistics());
00079         Space* slave = master;
00080         master = master->clone(shared_data,shared_info);
00081         complete = slave->slave(mi);
00082         e->reset(slave);
00083         sslr = 0;
00084         stop->m_stat.restart++;
00085       }
00086     }
00087     while (true) {
00088       Space* n = e->next();
00089       if (n != NULL) {
00090         // The engine found a solution
00091         restart = true;
00092         delete last;
00093         last = n->clone(shared_data);
00094         return n;
00095       } else if ( (!complete && !e->stopped()) ||
00096                   (e->stopped() && stop->enginestopped()) ) {
00097         // The engine must perform a true restart
00098         // The number of the restart has been incremented in the stop object
00099         sslr = 0;
00100         NoGoods& ng = e->nogoods();
00101         ng.ng(0);
00102         MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
00103         (void) master->master(mi);
00104         stop->m_stat.nogood += ng.ng();
00105         long unsigned int nl = ++(*co);
00106         stop->limit(e->statistics(),nl);
00107         if (master->status(stop->m_stat) == SS_FAILED)
00108           return NULL;
00109         Space* slave = master;
00110         master = master->clone(shared_data,shared_info);
00111         complete = slave->slave(mi);
00112         e->reset(slave);
00113       } else {
00114         return NULL;
00115       }
00116     }
00117     GECODE_NEVER;
00118     return NULL;
00119   }
00120 
00121   Search::Statistics
00122   RBS::statistics(void) const {
00123     return stop->metastatistics()+e->statistics();
00124   }
00125 
00126   void
00127   RBS::constrain(const Space& b) {
00128     if (!best)
00129       throw NoBest("RBS::constrain");
00130     if (last != NULL) {
00131       last->constrain(b);
00132       if (last->status() == SS_FAILED) {
00133         delete last;
00134       } else {
00135         return;
00136       }
00137     }
00138     last = b.clone(shared_data);
00139     master->constrain(b);
00140     e->constrain(b);
00141   }
00142 
00143   bool
00144   RBS::stopped(void) const {
00145     /*
00146      * What might happen during parallel search is that the
00147      * engine has been stopped but the meta engine has not, so
00148      * the meta engine does not perform a restart. However the
00149      * invocation of next will do so and no restart will be
00150      * missed.
00151      */
00152     return e->stopped();
00153   }
00154 
00155   RBS::~RBS(void) {
00156     delete e;
00157     delete master;
00158     delete last;
00159     delete co;
00160     delete stop;
00161   }
00162 
00163 }}}
00164 
00165 // STATISTICS: search-meta