Generated on Tue Dec 16 13:34:01 2008 for ell-3.0.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 // TODO maybe better to replace pointer copy with real clone to avoid dependency to external data
00010 // inconsistency to copy construction 
00011 S_LP::S_LP(biu::LatticeProtein_I* _latProt, biu::LatticeMoveSet* _moveSet)
00012     : latProt(_latProt), moveSet(_moveSet)
00013     {}
00014 
00015 S_LP::S_LP(const S_LP& state)
00016     : latProt(state.latProt->clone()), moveSet(state.moveSet->clone())
00017     {}
00018     
00019 S_LP::~S_LP() {
00020     if (latProt!=NULL) delete latProt;
00021     if (moveSet!=NULL) delete moveSet;
00022 }
00023 
00024 double  
00025 S_LP::getEnergy(void) const {
00026     assertbiu(latProt != NULL, "no lattice protein available (NULL)");
00027     return latProt->getEnergy();
00028 }
00029 
00030 std::string 
00031 S_LP::toString() const {
00032     assertbiu(latProt != NULL, "no lattice protein available (NULL)");
00033     return latProt->getStringRepresentation();
00034 }
00035 
00036 /* Fills the given string with a specific std::string 
00037  * representation of this State.
00038  * @param toFill the string to overwrite
00039  * @return the changed toFill in parameter
00040  */
00041 std::string& 
00042 S_LP::toString( std::string & toFill ) const
00043 {
00044      // can be improved
00045     toFill = latProt->getStringRepresentation();
00046     return toFill;
00047 }
00048 
00049 
00050 bool    
00051 S_LP::operator== (const State& state2) const {
00052     if ((const State*)latProt == &state2) 
00053         return true;
00054     const S_LP* s2 = dynamic_cast<const S_LP*>(&state2);
00055     if (s2 != NULL)
00056         return  *latProt == *(s2->latProt);
00057     return false;
00058 }
00059 
00060 bool
00061 S_LP::operator!= (const State& state2) const {
00062     return !(*this == state2);
00063 }
00064 
00065 bool 
00066 S_LP::operator< (const State& lp2) const
00067 {
00068     if (this == &lp2) {
00069         return false;
00070     }
00071     const S_LP* s2 = dynamic_cast<const S_LP*>(&lp2);
00072     assertbiu(s2!=NULL, "lp2 is no S_LP object");
00073     assertbiu(this->latProt->getLength() == s2->latProt->getLength()
00074                 ,"the two S_LP objects differ in length");
00075     if (this->getEnergy() < s2->getEnergy())
00076         return true;
00077     if ( this->getEnergy() == s2->getEnergy() ) {
00078         biu::MoveSequence m1 = this->latProt->getMoveSeqAbs();
00079         biu::MoveSequence m2 = this->latProt->getMoveSeqAbs();
00080         return std::lexicographical_compare(    m1.begin(), m1.end()
00081                                                 , m2.begin(), m2.end() );
00082     }
00083     return false;
00084 }
00085 
00086 State::NeighborListPtr 
00087 S_LP::getNeighborList() const {
00088     return NeighborListPtr(new SuccessiveNeighborList(this));
00089 }
00090 
00091 State::NeighborListPtr 
00092 S_LP::getRandomNeighborList() const {
00093     return NeighborListPtr(new RandomNeighborList(this));
00094 }
00095 
00096 State* S_LP::getRandomNeighbor(State* inPlaceNeigh) const {
00097     S_LP* s;
00098     // get state to work on
00099     if (inPlaceNeigh==NULL) {
00100         s = dynamic_cast<S_LP*>(this->clone());
00101         inPlaceNeigh = s;
00102     }
00103     else {
00104         s = dynamic_cast<S_LP*>(inPlaceNeigh);
00105         assertbiu(s != NULL, "given state is no S_LP");
00106         *(s->latProt) = *(this->latProt); // copy 
00107         *(s->moveSet) = *(this->moveSet); // copy 
00108     }
00109     
00110     // get random move index
00111     size_t index = biu::RNF::getRN(s->moveSet->getMoveNumber(s->latProt));
00112     if (s->moveSet->applyMoveInPlace(s->latProt, index) == NULL) {
00113         delete s;
00114         s = NULL;
00115     }
00116     
00117     return s;
00118 }
00119 
00120 size_t 
00121 S_LP::getNeighborNumber(void) const {
00122     assertbiu(moveSet != NULL, "no moveset available");
00123     return moveSet->getMoveNumber(latProt);
00124 }
00125 
00126 State* 
00127 S_LP::getNeighbor(const size_t index, State* neigh) const {
00128     assertbiu(moveSet != NULL, "no moveset available");
00129     assertbiu(index < moveSet->getMoveNumber(latProt), "index out of range");
00130 
00131     S_LP* s;
00132     if (neigh == NULL) {
00133          s = dynamic_cast<S_LP*>(this->clone());    // create copy
00134          neigh = s;
00135     } else {
00136         s = dynamic_cast<S_LP*>(neigh);                         // use neigh
00137         assertbiu( s!=NULL, "neigh is no S_LP*");
00138         *(s->latProt) = *(this->latProt); // copy 
00139         *(s->moveSet) = *(this->moveSet); // copy 
00140     }
00141     
00142     if (s->moveSet->applyMoveInPlace(s->latProt, index) == NULL) {
00143         delete s;
00144         s = NULL;
00145     }
00146     return s;
00147 }
00148 
00149 State* 
00150 S_LP::undoNeighborChange(const size_t index, State* const neigh) const {
00151     
00152     if (neigh == NULL) {
00153         return neigh;
00154     } else {
00155         S_LP* s = dynamic_cast<S_LP*>(neigh);
00156         assertbiu( s!=NULL, "neigh is no S_LP*");
00157         if (*neigh != *this)
00158             s->moveSet->undoLastMove(s->latProt);
00159         return s;
00160     }
00161 }
00162 
00163 State* 
00164 S_LP::applyNeighborChange(const size_t index, State* const copy) const {
00165     assertbiu(index < moveSet->getMoveNumber(latProt), "index out of range");
00166     assertbiu(copy != NULL, "no state to fill given");
00167 
00168     S_LP* c = dynamic_cast<S_LP*>(copy);
00169     assertbiu( c!=NULL || copy == NULL, "given copy is no S_LP*");
00170     if (c==NULL) {
00171         return c;
00172     } else {
00173         *(c->latProt) = *(this->latProt); // copy 
00174         *(c->moveSet) = *(this->moveSet); // copy 
00175     }
00176     if (c->moveSet->applyMoveInPlace(c->latProt, index) == NULL) {
00177         return NULL;
00178     }
00179     return c;
00180 }
00181 
00182 } // namespace ell