src/ell/StateAcceptor.cc
Go to the documentation of this file.00001 #include "ell/StateAcceptor.hh" 00002 #include "biu/RandomNumberFactory.hh" // for SA_Metropolis 00003 #include <cmath> // exp for SA_Metropolis 00004 00005 00006 namespace ell { 00007 00008 bool 00009 SA_All::accept(const StateCollector* const sc, const State& succ) const 00010 { 00011 return true; 00012 } 00013 00014 00015 bool 00016 SA_Le::accept(const StateCollector* const sc, const State& succ) const 00017 { 00018 return succ.operator<(*(sc->getLastAdded())); 00019 } 00020 00021 bool 00022 SA_E_Eq::accept(const StateCollector* const sc, const State& succ) const 00023 { 00024 return ( succ.getEnergy() == sc->getLastAdded()->getEnergy()); 00025 } 00026 00027 bool 00028 SA_E_Gr::accept(const StateCollector* const sc, const State& succ) const 00029 { 00030 return ( succ.getEnergy() > sc->getLastAdded()->getEnergy()); 00031 } 00032 00033 bool 00034 SA_E_GrEq::accept(const StateCollector* const sc, const State& succ) const 00035 { 00036 return ( succ.getEnergy() >= sc->getLastAdded()->getEnergy()); 00037 } 00038 00039 bool 00040 SA_E_Le::accept(const StateCollector* const sc, const State& succ) const 00041 { 00042 return ( succ.getEnergy() < sc->getLastAdded()->getEnergy()); 00043 } 00044 00045 bool 00046 SA_E_LeEq::accept(const StateCollector* const sc, const State& succ) const 00047 { 00048 return ( succ.getEnergy() <= sc->getLastAdded()->getEnergy()); 00049 } 00050 00051 SA_Max_E::SA_Max_E( const double MaxEnergy) : MAX_ENERGY(MaxEnergy) {} 00052 bool 00053 SA_Max_E::accept(const StateCollector* const sc, const State& succ) const 00054 { 00055 return ( succ.getEnergy() <= MAX_ENERGY); 00056 } 00057 00058 00059 SA_Metropolis::SA_Metropolis(const double _kT) : kT(_kT) {} 00060 00061 bool 00062 SA_Metropolis::accept(const StateCollector* const sc, const State& succ) const 00063 { 00064 // compute energy difference 00065 double deltaE = succ.getEnergy() - sc->getLastAdded()->getEnergy(); 00066 // check if negative energy difference --> always accept 00067 if (deltaE < 0) 00068 return true; 00069 // compute probability for accepting state -> Metropolis 00070 double p_accept = exp( - deltaE / kT); 00071 00072 // compute random number 0 <= r <= 1 00073 double r = (((double) biu::RNF::getRN()) / ((double) biu::RNF::getMaxRN())); 00074 00075 // return true with probability p_accept 00076 if ( r < p_accept ) return true; 00077 else return false; 00078 } 00079 00080 } // namespace ell