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

dfs.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  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2015-03-20 15:37:34 +0100 (Fri, 20 Mar 2015) $ by $Author: schulte $
00011  *     $Revision: 14471 $
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 #ifndef __GECODE_SEARCH_SEQUENTIAL_DFS_HH__
00039 #define __GECODE_SEARCH_SEQUENTIAL_DFS_HH__
00040 
00041 #include <gecode/search.hh>
00042 #include <gecode/search/support.hh>
00043 #include <gecode/search/worker.hh>
00044 #include <gecode/search/sequential/path.hh>
00045 
00046 namespace Gecode { namespace Search { namespace Sequential {
00047 
00049   class DFS : public Worker {
00050   private:
00052     Options opt;
00054     Path path;
00056     Space* cur;
00058     unsigned int d;
00059   public:
00061     DFS(Space* s, const Options& o);
00063     Space* next(void);
00065     Statistics statistics(void) const;
00067     void reset(Space* s);
00069     NoGoods& nogoods(void);
00071     ~DFS(void);
00072   };
00073 
00074   forceinline 
00075   DFS::DFS(Space* s, const Options& o)
00076     : opt(o), path(opt.nogoods_limit), d(0) {
00077     if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
00078       fail++;
00079       cur = NULL;
00080       if (!opt.clone)
00081         delete s;
00082     } else {
00083       cur = snapshot(s,opt);
00084     }
00085   }
00086 
00087   forceinline void
00088   DFS::reset(Space* s) {
00089     delete cur;
00090     path.reset();
00091     d = 0;
00092     if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
00093       delete s;
00094       cur = NULL;
00095     } else {
00096       cur = s;
00097     }
00098     Worker::reset();
00099   }
00100 
00101   forceinline NoGoods&
00102   DFS::nogoods(void) {
00103     return path;
00104   }
00105 
00106   forceinline Space*
00107   DFS::next(void) {
00108     /*
00109      * The engine maintains the following invariant:
00110      *  - If the current space (cur) is not NULL, the path always points
00111      *    to exactly that space.
00112      *  - If the current space (cur) is NULL, the path always points
00113      *    to the next space (if there is any).
00114      *
00115      * This invariant is needed so that no-goods can be extracted properly
00116      * when the engine is stopped or has found a solution.
00117      *
00118      */
00119     start();
00120     while (true) {
00121       if (stop(opt))
00122         return NULL;
00123       while (cur == NULL) {
00124         if (path.empty())
00125           return NULL;
00126         cur = path.recompute(d,opt.a_d,*this);
00127         if (cur != NULL)
00128           break;
00129         path.next();
00130       }
00131       node++;
00132       switch (cur->status(*this)) {
00133       case SS_FAILED:
00134         fail++;
00135         delete cur;
00136         cur = NULL;
00137         path.next();
00138         break;
00139       case SS_SOLVED:
00140         {
00141           // Deletes all pending branchers
00142           (void) cur->choice();
00143           Space* s = cur;
00144           cur = NULL;
00145           path.next();
00146           return s;
00147         }
00148       case SS_BRANCH:
00149         {
00150           Space* c;
00151           if ((d == 0) || (d >= opt.c_d)) {
00152             c = cur->clone();
00153             d = 1;
00154           } else {
00155             c = NULL;
00156             d++;
00157           }
00158           const Choice* ch = path.push(*this,cur,c);
00159           cur->commit(*ch,0);
00160           break;
00161         }
00162       default:
00163         GECODE_NEVER;
00164       }
00165     }
00166     GECODE_NEVER;
00167     return NULL;
00168   }
00169 
00170   forceinline Statistics
00171   DFS::statistics(void) const {
00172     return *this;
00173   }
00174 
00175   forceinline 
00176   DFS::~DFS(void) {
00177     delete cur;
00178     path.reset();
00179   }
00180 
00181 }}}
00182 
00183 #endif
00184 
00185 // STATISTICS: search-sequential