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

dfs.icc

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-02-22 02:44:55 +0100 (Fri, 22 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6273 $
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 namespace Gecode {
00039 
00040   namespace Search {
00041 
00042     /*
00043      * DFS engine
00044      *
00045      */
00046     forceinline
00047     DfsEngine::DfsEngine(unsigned int c_d0, unsigned int a_d0,
00048                          Stop* st, size_t sz)
00049       : EngineCtrl(st,sz), rcs(a_d0), cur(NULL), c_d(c_d0), d(0) {}
00050 
00051 
00052     forceinline void
00053     DfsEngine::init(Space* s) {
00054       cur = s;
00055     }
00056 
00057     forceinline void
00058     DfsEngine::reset(Space* s) {
00059       delete cur;
00060       rcs.reset();
00061       cur = s;
00062       d   = 0;
00063       EngineCtrl::reset(s);
00064     }
00065 
00066     forceinline void
00067     DfsEngine::reset(void) {
00068       delete cur;
00069       rcs.reset();
00070       cur = NULL;
00071       d   = 0;
00072       EngineCtrl::reset();
00073     }
00074 
00075     forceinline size_t
00076     DfsEngine::stacksize(void) const {
00077       return rcs.stacksize();
00078     }
00079 
00080     forceinline Space*
00081     DfsEngine::explore(void) {
00082       start();
00083       while (true) {
00084         while (cur) {
00085           if (stop(stacksize()))
00086             return NULL;
00087           switch (cur->status(propagate)) {
00088           case SS_FAILED:
00089             fail++;
00090             delete cur;
00091             cur = NULL;
00092             EngineCtrl::current(NULL);
00093             break;
00094           case SS_SOLVED:
00095             {
00096               Space* s = cur;
00097               cur = NULL;
00098               EngineCtrl::current(NULL);
00099               return s;
00100             }
00101           case SS_BRANCH:
00102             {
00103               Space* c;
00104               if ((d == 0) || (d >= c_d)) {
00105                 clone++;
00106                 c = cur->clone();
00107                 d = 1;
00108               } else {
00109                 c = NULL;
00110                 d++;
00111               }
00112               const BranchingDesc* desc = rcs.push(cur,c);
00113               EngineCtrl::push(c,desc);
00114               commit++;
00115               cur->commit(desc,0);
00116               break;
00117             }
00118           default: GECODE_NEVER;
00119           }
00120         }
00121         if (!rcs.next(*this))
00122           return NULL;
00123         cur = rcs.recompute<false>(d,*this);
00124         EngineCtrl::current(cur);
00125       }
00126       return NULL;
00127     }
00128 
00129     forceinline
00130     DfsEngine::~DfsEngine(void) {
00131       delete cur;
00132       rcs.reset();
00133     }
00134 
00135   }
00136 
00137   /*
00138    * Control for DFS search engine
00139    *
00140    */
00141 
00142   template <class T>
00143   forceinline
00144   DFS<T>::DFS(T* s, const Search::Options& o)
00145     : Search::DFS(s,o.c_d,o.a_d,o.stop,sizeof(T)) {}
00146 
00147   template <class T>
00148   forceinline T*
00149   DFS<T>::next(void) {
00150     return dynamic_cast<T*>(Search::DFS::next());
00151   }
00152 
00153 
00154 
00155   /*
00156    * DFS convenience
00157    *
00158    */
00159 
00160   template <class T>
00161   forceinline T*
00162   dfs(T* s, const Search::Options& o) {
00163     DFS<T> d(s,o);
00164     return dynamic_cast<T*>(d.next());
00165   }
00166 
00167 }
00168 
00169 // STATISTICS: search-any