Generated on Mon Aug 25 11:35:40 2008 for Gecode by doxygen 1.5.6

bab.cc

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  *  Copyright:
00007  *     Christian Schulte, 2003
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-19 13:19:23 +0100 (Sat, 19 Jan 2008) $ by $Author: schulte $
00011  *     $Revision: 5916 $
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 #include "gecode/search.hh"
00039 
00040 namespace Gecode { namespace Search {
00041 
00042   /*
00043    * The invariant maintained by the engine is:
00044    *   For all nodes stored at a depth less than mark, there
00045    *   is no guarantee of betterness. For those above the mark,
00046    *   betterness is guaranteed.
00047    *
00048    * The engine maintains the path on the stack for the current
00049    * node to be explored.
00050    *
00051    */
00052 
00053   BabEngine::ExploreStatus
00054   BabEngine::explore(Space*& s1, Space*& s2) {
00055     start();
00056     /*
00057      * Upon entry, cur can be either NULL or set to the initial
00058      * space. For the initial case, es is also ES_CONSTRAIN.
00059      *
00060      * Otherwise (that is, cur == NULL), the actions depend on
00061      * es. In case es is ES_CONSTRAIN, a space on stack has been
00062      * constrained. Whether this is succesful recomputation finds
00063      * out. In any case, the stack is not allowed to be moved to
00064      * the next node.
00065      *
00066      * In case es is ES_SOLUTION, the stack must be moved to the next
00067      * node and recomputation is to be performed.
00068      */
00069     while (true) {
00070       assert((es == ES_SOLUTION) || (cur == NULL));
00071       if (stop(stacksize())) {
00072         s1 = NULL;
00073         return ES_SOLUTION;
00074       }
00075       if (cur == NULL) {
00076         if (es == ES_CONSTRAIN) {
00077           es = ES_SOLUTION;
00078           goto same;
00079         }
00080         do {
00081           if (!rcs.next(*this)) {
00082             s1 = NULL;
00083             return ES_SOLUTION;
00084           }
00085           {
00086             int l = rcs.lc(s1);
00087             if (l < mark) {
00088               es = ES_CONSTRAIN;
00089               mark = l;
00090               s2 = best;
00091               return ES_CONSTRAIN;
00092             }
00093           }
00094         same:
00095           cur = rcs.recompute<true>(d,*this);
00096         } while (cur == NULL);
00097         EngineCtrl::current(cur);
00098       }
00099       switch (cur->status(propagate)) {
00100       case SS_FAILED:
00101         fail++;
00102         delete cur;
00103         cur = NULL;
00104         EngineCtrl::current(NULL);
00105         break;
00106       case SS_SOLVED:
00107         delete best;
00108         best = cur;
00109         mark = rcs.entries();
00110         s1 = best->clone();
00111         clone++;
00112         cur = NULL;
00113         EngineCtrl::current(NULL);
00114         return ES_SOLUTION;
00115       case SS_BRANCH:
00116         {
00117           Space* c;
00118           if ((d == 0) || (d >= c_d)) {
00119             c = cur->clone();
00120             clone++;
00121             d = 1;
00122           } else {
00123             c = NULL;
00124             d++;
00125           }
00126           const BranchingDesc* desc = rcs.push(cur,c);
00127           EngineCtrl::push(c,desc);
00128           cur->commit(desc,0);
00129           commit++;
00130           break;
00131         }
00132       default:
00133         GECODE_NEVER;
00134       }
00135     }
00136     return ES_SOLUTION;
00137   }
00138 
00139 
00140 
00141 
00142   BAB::BAB(Space* s, unsigned int c_d, unsigned int a_d, Stop* st, size_t sz)
00143     : e(c_d,a_d,st,sz) {
00144     unsigned long int p = 0;
00145     Space* c = (s->status(p) == SS_FAILED) ? NULL : s->clone(true);
00146     e.init(c);
00147     e.propagate += p;
00148     e.current(s);
00149     e.current(NULL);
00150     e.current(c);
00151     if (c == NULL)
00152       e.fail += 1;
00153   }
00154 
00155   bool
00156   BAB::stopped(void) const {
00157     return e.stopped();
00158   }
00159 
00160   Statistics
00161   BAB::statistics(void) const {
00162     Statistics s = e;
00163     s.memory += e.stacksize();
00164     return s;
00165   }
00166 
00167 }}
00168 
00169 // STATISTICS: search-any