Generated on Fri Mar 20 15:56:19 2015 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: 2015-03-19 14:02:56 +0100 (Thu, 19 Mar 2015) $ by $Author: schulte $
00011  *     $Revision: 14468 $
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.hh>
00040 
00041 namespace Gecode { namespace Search { namespace Meta {
00042 
00043   /*
00044    * Stopping for meta search engines
00045    *
00046    */
00047 
00048   bool 
00049   RestartStop::stop(const Statistics& s, const Options& o) {
00050     // Stop if the fail stop object for the engine says so
00051     if (e_stop->stop(s,o)) {
00052       e_stopped = true;
00053       m_stat.restart++;
00054       return true;
00055     }
00056     // Stop if the stop object for the meta engine says so
00057     if ((m_stop != NULL) && m_stop->stop(m_stat+s,o)) {
00058       e_stopped = false;
00059       return true;
00060     }
00061     return false;
00062   }
00063 
00064 
00065   Space*
00066   RBS::next(void) {
00067     if (restart) {
00068       restart = false;
00069       sslr++;
00070       NoGoods& ng = e->nogoods();
00071       // Reset number of no-goods found
00072       ng.ng(0);
00073       CRI cri(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
00074       bool r = master->master(cri);
00075       stop->m_stat.nogood += ng.ng();
00076       if (master->status(stop->m_stat) == SS_FAILED) {
00077         stop->update(e->statistics());
00078         delete master;
00079         master = NULL;
00080         e->reset(NULL);
00081         return NULL;
00082       } else if (r) {
00083         stop->update(e->statistics());
00084         Space* slave = master;
00085         master = master->clone(shared);
00086         complete = slave->slave(cri);
00087         e->reset(slave);
00088         sslr = 0;
00089         stop->m_stat.restart++;
00090       }
00091     }
00092     while (true) {
00093       Space* n = e->next();
00094       if (n != NULL) {
00095         // The engine found a solution
00096         restart = true;
00097         delete last;
00098         last = n->clone();
00099         return n;
00100       } else if ( (!complete && !e->stopped()) ||
00101                   (e->stopped() && stop->enginestopped()) ) {
00102         // The engine must perform a true restart
00103         // The number of the restart has been incremented in the stop object
00104         sslr = 0;
00105         NoGoods& ng = e->nogoods();
00106         ng.ng(0);
00107         CRI cri(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
00108         (void) master->master(cri);
00109         stop->m_stat.nogood += ng.ng();
00110         long unsigned int nl = ++(*co);
00111         stop->limit(e->statistics(),nl);
00112         if (master->status(stop->m_stat) == SS_FAILED)
00113           return NULL;
00114         Space* slave = master;
00115         master = master->clone(shared);
00116         complete = slave->slave(cri);
00117         e->reset(slave);
00118       } else {
00119         return NULL;
00120       }
00121     }
00122     GECODE_NEVER;
00123     return NULL;
00124   }
00125   
00126   Search::Statistics
00127   RBS::statistics(void) const {
00128     return stop->metastatistics()+e->statistics();
00129   }
00130   
00131   bool
00132   RBS::stopped(void) const {
00133     /*
00134      * What might happen during parallel search is that the
00135      * engine has been stopped but the meta engine has not, so
00136      * the meta engine does not perform a restart. However the
00137      * invocation of next will do so and no restart will be
00138      * missed.
00139      */
00140     return e->stopped(); 
00141   }
00142   
00143   RBS::~RBS(void) {
00144     // Deleting e also deletes stop
00145     delete e;
00146     delete master;
00147     delete last;
00148     delete co;
00149   }
00150 
00151 }}}
00152 
00153 // STATISTICS: search-meta