Generated on Fri Mar 20 15:56:19 2015 for Gecode by doxygen 1.6.3

bab.hh

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Contributing authors:
00007  *     Guido Tack <tack@gecode.org>
00008  *
00009  *  Copyright:
00010  *     Christian Schulte, 2004
00011  *     Guido Tack, 2004
00012  *
00013  *  Last modified:
00014  *     $Date: 2015-03-20 15:37:34 +0100 (Fri, 20 Mar 2015) $ by $Author: schulte $
00015  *     $Revision: 14471 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 #ifndef __GECODE_SEARCH_SEQUENTIAL_BAB_HH__
00043 #define __GECODE_SEARCH_SEQUENTIAL_BAB_HH__
00044 
00045 #include <gecode/search.hh>
00046 #include <gecode/search/support.hh>
00047 #include <gecode/search/worker.hh>
00048 #include <gecode/search/sequential/path.hh>
00049 
00050 namespace Gecode { namespace Search { namespace Sequential {
00051 
00053   class BAB : public Worker {
00054   private:
00056     Options opt;
00058     Path path;
00060     Space* cur;
00062     unsigned int d;
00064     int mark;
00066     Space* best;
00067   public:
00069     BAB(Space* s, const Options& o);
00071     Space* next(void);
00073     Statistics statistics(void) const;
00075     void reset(Space* s);
00077     NoGoods& nogoods(void);
00079     ~BAB(void);
00080   };
00081 
00082   forceinline 
00083   BAB::BAB(Space* s, const Options& o)
00084     : opt(o), path(opt.nogoods_limit), d(0), mark(0), best(NULL) {
00085     if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
00086       fail++;
00087       cur = NULL;
00088       if (!o.clone)
00089         delete s;
00090     } else {
00091       cur = snapshot(s,opt);
00092     }
00093   }
00094 
00095   forceinline Space*
00096   BAB::next(void) {
00097     /*
00098      * The engine maintains the following invariant:
00099      *  - If the current space (cur) is not NULL, the path always points
00100      *    to exactly that space.
00101      *  - If the current space (cur) is NULL, the path always points
00102      *    to the next space (if there is any).
00103      *
00104      * This invariant is needed so that no-goods can be extracted properly
00105      * when the engine is stopped or has found a solution.
00106      *
00107      * An additional invariant maintained by the engine is:
00108      *   For all nodes stored at a depth less than mark, there
00109      *   is no guarantee of betterness. For those above the mark,
00110      *   betterness is guaranteed.
00111      *
00112      */
00113     start();
00114     while (true) {
00115       if (stop(opt))
00116         return NULL;
00117       // Recompute and add constraint if necessary
00118       while (cur == NULL) {
00119         if (path.empty())
00120           return NULL;
00121         cur = path.recompute(d,opt.a_d,*this,*best,mark);
00122         if (cur != NULL)
00123           break;
00124         path.next();
00125       }
00126       node++;
00127       switch (cur->status(*this)) {
00128       case SS_FAILED:
00129         fail++;
00130         delete cur;
00131         cur = NULL;
00132         path.next();
00133         break;
00134       case SS_SOLVED:
00135         // Deletes all pending branchers
00136         (void) cur->choice();
00137         delete best;
00138         best = cur;
00139         cur = NULL;
00140         path.next();
00141         mark = path.entries();
00142         return best->clone();
00143       case SS_BRANCH:
00144         {
00145           Space* c;
00146           if ((d == 0) || (d >= opt.c_d)) {
00147             c = cur->clone();
00148             d = 1;
00149           } else {
00150             c = NULL;
00151             d++;
00152           }
00153           const Choice* ch = path.push(*this,cur,c);
00154           cur->commit(*ch,0);
00155           break;
00156         }
00157       default:
00158         GECODE_NEVER;
00159       }
00160     }
00161     GECODE_NEVER;
00162     return NULL;
00163   }
00164 
00165   forceinline Statistics
00166   BAB::statistics(void) const {
00167     return *this;
00168   }
00169 
00170   forceinline void
00171   BAB::reset(Space* s) { 
00172     delete best;
00173     best = NULL;
00174     path.reset();
00175     d = 0;
00176     mark = 0;
00177     delete cur;
00178     if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
00179       delete s;
00180       cur = NULL;
00181     } else {
00182       cur = s;
00183     }
00184     Worker::reset();
00185   }
00186 
00187   forceinline NoGoods&
00188   BAB::nogoods(void) {
00189     return path;
00190   }
00191 
00192   forceinline 
00193   BAB::~BAB(void) {
00194     path.reset();
00195     delete best;
00196     delete cur;
00197   }
00198 
00199 }}}
00200 
00201 #endif
00202 
00203 // STATISTICS: search-sequential