Generated on Tue Dec 16 13:34:02 2008 for ell-3.0.0 by doxygen 1.5.1

src/ell/Walk.cc

Go to the documentation of this file.
00001 #include "ell/Walk.hh"
00002 #include <biu/assertbiu.hh>
00003 
00004 
00005 namespace ell
00006 {
00010 
00011 //STATIC Walk::walk function    
00012     StateCollector* 
00013     Walk::walk(
00014                 const State* const start, 
00015                 StateCollector* const scWalk,
00016                 const NeighborGenerator* const ng, 
00017                 const StateAcceptor* const sa, 
00018                 const WalkAbortionCriterion* const wac,
00019                 StateCollector* const scRej
00020                 )
00021     {
00022         scWalk->add(*start);
00023         State* lastAccepted = NULL;
00024         bool foundNext = true;
00025         
00026         while (foundNext && !wac->abort(scWalk)) 
00027         {
00028               // clone pointer of last accepted state to ensure that the
00029               // neighbor list is still available when scWalk is updated
00030             lastAccepted = scWalk->getLastAdded()->clone(lastAccepted);
00031             
00032             State::NeighborListPtr neighbors = ng->getNeighborList(lastAccepted);
00033             State::NeighborList::Iterator it = neighbors->begin();
00034             
00035             foundNext = false;
00036             // find new neighbor
00037             while (neighbors->end() != it) 
00038             {               
00039                 if (sa->accept(scWalk, *it)) 
00040                 {
00041                     scWalk->add(*it);
00042                     foundNext = true;
00043                     break;
00044                 }
00045                 else if (scRej!=NULL) {
00046                     scRej->add(*it);
00047                 }
00048                 ++it;
00049             }
00050             
00051         }
00052         
00053          // clear memory
00054         if (lastAccepted != NULL)  {
00055             delete lastAccepted;
00056         }
00057         
00058         return scWalk;
00059     }
00060 
00061 
00062     
00066 
00067 // WalkAdaptive Constructor 
00068     WalkAdaptive::WalkAdaptive(const NeighborGenType ngt) 
00069         : t(ngt)
00070     { }
00071 
00072 // STATIC walkAdaptive function of WalkAdaptive class
00073     StateCollector* 
00074     WalkAdaptive::walkAdaptive(
00075             const State* const s,
00076             StateCollector* const scWalk,
00077             const NeighborGenType t,
00078             StateCollector* const scRej
00079             ) 
00080     {
00081         // Decide which kind of NeighborGenerator to use
00082         NeighborGenerator* ng;
00083         switch (t) {
00084             case RANDOM:
00085                 ng = new NG_SuccessiveRandom();
00086                 break;
00087             case SUCCESSIVE:
00088                 ng = new NG_Successive();
00089                 break;
00090             default:
00091                 assertbiu(false, "NeighborGenType not supported");
00092                 break;
00093         }
00094         
00095           // Succesor State must be smaller according to State ordering
00096         SA_Le sa;
00097         
00098         // An adaptive walk is always finished at a local minimum,
00099         // there is no additional abortion criterion
00100         WAC_OpenEnd wac;
00101         
00102         // call generic walk function
00103         Walk::walk(s, scWalk, ng, &sa, &wac, scRej);
00104         
00105         delete ng;
00106         
00107         return scWalk;
00108     }
00109 
00110 // VIRTUAL const walk function of WalkAdaptive class
00111     StateCollector* 
00112     WalkAdaptive::walk (const State* const s, 
00113                         StateCollector* const sc,
00114                         StateCollector* const scRej) 
00115     const
00116     {
00117         // call static function
00118         return WalkAdaptive::walkAdaptive(s, sc, this->t, scRej);
00119     }
00120 
00121     
00122     
00126     
00127 // WalkAdaptive Constructor 
00128     WalkGradient::WalkGradient() 
00129     { }
00130 
00131 //static    
00132     StateCollector* 
00133     WalkGradient::walkGradient(
00134             const State* const start, 
00135             StateCollector* const scWalk
00136             ) 
00137     {
00138           // add start to walk
00139         scWalk->add(*start);
00140           // temporary pointers to perform gradient walk
00141         const State * lastAdded = NULL;
00142         State * bestNeigh = NULL;
00143         
00144         do {
00145             
00146               // pointer of last accepted state to ensure that the
00147               // neighbor list is still available when scWalk is updated
00148             lastAdded = scWalk->getLastAdded();
00149             assertbiu(lastAdded != NULL, "no last element in StateCollector");
00150               // treshold of best energy found so far
00151             double minEfound = lastAdded->getEnergy();
00152             
00153             State::NeighborListPtr neighbors = lastAdded->getNeighborList();
00154             State::NeighborList::Iterator it = neighbors->begin();
00155 
00156             // look for best neighbor
00157             bestNeigh = lastAdded->clone(bestNeigh);
00158             while (neighbors->end() != it) {
00159                 if ( it->operator<(*bestNeigh) ) 
00160                 {
00161                     bestNeigh = it->clone(bestNeigh);
00162                     minEfound = bestNeigh->getEnergy();
00163                 }
00164                 ++it;
00165             }
00166             
00167             if (State::less(bestNeigh,lastAdded)) {
00168                 // found new gradient neighbor 
00169                 scWalk->add(*bestNeigh);
00170             } else {
00171                 delete bestNeigh;
00172                 bestNeigh = NULL;
00173             }
00174         } while (bestNeigh != NULL);  // have found local minimum
00175         
00176         return scWalk;
00177     }
00178     
00179 // virtual
00180     StateCollector* 
00181     WalkGradient::walk(
00182             const State* const start, 
00183             StateCollector* const scWalk,
00184             StateCollector* const scRej) 
00185     const
00186     {
00187         // call static function
00188         return WalkGradient::walkGradient(start, scWalk);
00189     }
00190     
00191     
00195 // constructor
00196     WalkMC::WalkMC( 
00197             const WalkAbortionCriterion* const wac_, 
00198             const double kT_) 
00199         : wac(wac_) , kT(kT_) {
00200     }
00201     
00202 // static
00203 StateCollector* 
00204     WalkMC::walkMC(
00205             const State* const start, 
00206             StateCollector* const sc, 
00207             const WalkAbortionCriterion* const wac, 
00208             const double kT,
00209             StateCollector* const scRej) 
00210     {
00211         NG_Random ng;
00212 
00213         // Successor State is admitted using Metropolis criterion
00214         SA_Metropolis sa(kT);
00215 
00216         Walk::walk(start, sc, &ng, &sa, wac, scRej);
00217 
00218         return sc;
00219     }
00220     
00221 // virtual
00222     StateCollector* 
00223     WalkMC::walk(
00224             const State* const start, 
00225             StateCollector* const sc,
00226             StateCollector* const scRej)
00227     const
00228     {
00229         // call static function
00230         return WalkMC::walkMC(start, sc, this->wac, this->kT, scRej);
00231     }
00232     
00236     
00237     WalkRandom::WalkRandom(
00238             const WalkAbortionCriterion* const wac_
00239             )
00240         : wac(wac_)
00241     {
00242     }
00243 
00244 // static
00245     StateCollector* 
00246     WalkRandom::walkRandom(
00247             const State* const start, 
00248             StateCollector* const sc,
00249             const WalkAbortionCriterion* const wac, 
00250             StateCollector* const scRej)
00251     {
00252         NG_SuccessiveRandom ng;
00253         // accept all states = random walk
00254         SA_All sa; 
00255         
00256         Walk::walk(start, sc, &ng, &sa, wac, scRej);
00257         
00258         return sc;
00259     }   
00260 
00261 // virtual
00262     StateCollector* 
00263     WalkRandom::walk(
00264             const State* const start, 
00265             StateCollector* const sc,
00266             StateCollector* const scRej)
00267     const
00268     {
00269         // call static function
00270         return WalkRandom::walkRandom(start, sc, this->wac, scRej);
00271     }   
00272     
00273 
00274 }// namespace ell