Generated on Mon Jun 23 17:17:53 2008 for ell-2.3.0 by doxygen 1.5.1

src/ell/protein/S_LP.cc

Go to the documentation of this file.
00001 #include "ell/protein/S_LP.hh"
00002 #include "biu/RandomNumberFactory.hh"
00003 #include <cmath>    // for getFitness()
00004 
00005 
00006 namespace ell
00007 {
00008 
00009 S_LP::S_LP(biu::LatticeProtein_I* _latProt, biu::LatticeMoveSet* _moveSet)
00010     : latProt(_latProt), moveSet(_moveSet)
00011     {}
00012 
00013 S_LP::S_LP(const S_LP& state)
00014     : latProt(state.latProt->clone()), moveSet(state.moveSet->clone())
00015     {}
00016     
00017 S_LP::~S_LP() {
00018     if (latProt!=NULL) delete latProt;
00019     if (moveSet!=NULL) delete moveSet;
00020 }
00021 
00022 double  
00023 S_LP::getEnergy(void) const {
00024     assertbiu(latProt != NULL, "no lattice protein available (NULL)");
00025     return latProt->getEnergy();
00026 }
00027 
00028 int 
00029 S_LP::getFitness() const 
00030 {
00031     assertbiu(latProt != NULL, "no lattice protein available (NULL)");
00032     // rounds energy and casts it an int-value, then negate it
00033     double energy = latProt->getEnergy();
00034     if (energy >= 0)
00035         return - static_cast<int>(floor(energy + 0.5));
00036         else
00037             return - static_cast<int>(ceil(energy - 0.5));
00038 };
00039 
00040 std::string 
00041 S_LP::toString() const {
00042     assertbiu(latProt != NULL, "no lattice protein available (NULL)");
00043     return latProt->getStringRepresentation();
00044 }
00045 
00046 bool    
00047 S_LP::operator== (const State& state2) const {
00048     if ((const State*)latProt == &state2) 
00049         return true;
00050     const S_LP* s2 = dynamic_cast<const S_LP*>(&state2);
00051     if (s2 != NULL)
00052         return  *latProt == *(s2->latProt);
00053     return false;
00054 }
00055 
00056 bool
00057 S_LP::operator!= (const State& state2) const {
00058     return !(*this == state2);
00059 }
00060 
00061 State::NeighborListPtr 
00062 S_LP::getNeighborList() const {
00063     return NeighborListPtr(new SuccessiveNeighborList(this));
00064 }
00065 
00066 State::NeighborListPtr 
00067 S_LP::getRandomNeighborList() const {
00068     return NeighborListPtr(new RandomNeighborList(this));
00069 }
00070 
00071 State* S_LP::getRandomNeighbor(State* inPlaceNeigh) const {
00072     S_LP* s;
00073     // get state to work on
00074     if (inPlaceNeigh==NULL) {
00075         s = dynamic_cast<S_LP*>(this->clone());
00076         inPlaceNeigh = s;
00077     }
00078     else {
00079         s = dynamic_cast<S_LP*>(inPlaceNeigh);
00080         assertbiu(s != NULL, "given state is no S_LP");
00081         *(s->latProt) = *(this->latProt); // copy 
00082         *(s->moveSet) = *(this->moveSet); // copy 
00083     }
00084     
00085     // get random move index
00086     size_t index = biu::RNF::getRN(s->moveSet->getMoveNumber(s->latProt));
00087     if (s->moveSet->applyMoveInPlace(s->latProt, index) == NULL) {
00088         delete s;
00089         s = NULL;
00090     }
00091     
00092     return s;
00093 }
00094 
00095 size_t 
00096 S_LP::getNeighborNumber(void) const {
00097     assertbiu(moveSet != NULL, "no moveset available");
00098     return moveSet->getMoveNumber(latProt);
00099 }
00100 
00101 State* 
00102 S_LP::getNeighbor(const size_t index, State* neigh) const {
00103     assertbiu(moveSet != NULL, "no moveset available");
00104     assertbiu(index < moveSet->getMoveNumber(latProt), "index out of range");
00105 
00106     S_LP* s;
00107     if (neigh == NULL) {
00108          s = dynamic_cast<S_LP*>(this->clone());    // create copy
00109          neigh = s;
00110     } else {
00111         s = dynamic_cast<S_LP*>(neigh);                         // use neigh
00112         assertbiu( s!=NULL, "neigh is no S_LP*");
00113         *(s->latProt) = *(this->latProt); // copy 
00114         *(s->moveSet) = *(this->moveSet); // copy 
00115     }
00116     
00117     if (s->moveSet->applyMoveInPlace(s->latProt, index) == NULL) {
00118         delete s;
00119         s = NULL;
00120     }
00121     return s;
00122 }
00123 
00124 State* 
00125 S_LP::undoNeighborChange(const size_t index, State* const neigh) const {
00126     
00127     if (neigh == NULL) {
00128         return neigh;
00129     } else {
00130         S_LP* s = dynamic_cast<S_LP*>(neigh);
00131         assertbiu( s!=NULL, "neigh is no S_LP*");
00132         if (*neigh != *this)
00133             s->moveSet->undoLastMove(s->latProt);
00134         return s;
00135     }
00136 }
00137 
00138 State* 
00139 S_LP::applyNeighborChange(const size_t index, State* const copy) const {
00140     assertbiu(index < moveSet->getMoveNumber(latProt), "index out of range");
00141     assertbiu(copy != NULL, "no state to fill given");
00142 
00143     S_LP* c = dynamic_cast<S_LP*>(copy);
00144     assertbiu( c!=NULL || copy == NULL, "given copy is no S_LP*");
00145     if (c==NULL) {
00146         return c;
00147     } else {
00148         *(c->latProt) = *(this->latProt); // copy 
00149         *(c->moveSet) = *(this->moveSet); // copy 
00150     }
00151     if (c->moveSet->applyMoveInPlace(c->latProt, index) == NULL) {
00152         return NULL;
00153     }
00154     return c;
00155 }
00156 
00157 } // namespace ell