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

src/ell/LT_MinimaSet.cc

Go to the documentation of this file.
00001 
00002 #include <iomanip>
00003 #include <fstream>
00004 #include <sstream>
00005 #include <cmath>
00006 #include <algorithm>
00007 
00008 #include <biu/assertbiu.hh>
00009 
00010 #include "ell/LT_MinimaSet.hh"
00011 
00012 
00013 namespace ell {
00014 
00015 const std::string LT_MinimaSet::LT_ID = "MS";
00016 
00017 LT_MinimaSet::LT_MinimaSet()
00018 : mfeIndex(LandscapeTopology::INVALID_INDEX), sorted(true)
00019 { }
00020 
00022 LT_MinimaSet::LT_MinimaSet( const LT_MinimaSet& toCopy )
00024 {
00025     this->operator =(toCopy);
00026 }
00027 
00028 
00029 LT_MinimaSet&
00030 LT_MinimaSet::
00031 operator = ( const LT_MinimaSet& toCopy )
00032 {
00033       // check if toCopy is not the same object than this
00034     if (this == &toCopy) {
00035         return *this;
00036     }
00037       // clear data structures
00038     this->clear();
00039     
00040       // copy all minima
00041     vMinima.resize(toCopy.vMinima.size(), NULL);
00042     for (size_t i=0; i<vMinima.size(); i++) {
00043         vMinima[i] = toCopy.vMinima[i]->clone();
00044     }
00045       // copy remaining information
00046     mfeIndex = toCopy.mfeIndex;
00047     sorted = toCopy.sorted;
00048 
00049     return *this;
00050 }
00051 
00052 void 
00053 LT_MinimaSet::clear() {
00054     for (size_t i = 0; i < vMinima.size(); ++i)
00055         delete vMinima[i];
00056     vMinima.clear();
00057     mfeIndex = LandscapeTopology::INVALID_INDEX;
00058     sorted = true;
00059 }
00060 
00061 LT_MinimaSet::~LT_MinimaSet() {
00062     clear();
00063 }
00064 
00065 bool
00066 LT_MinimaSet::addSaddle(const size_t i, const size_t j, const State* const s) {
00067     // no saddle handling and therefore no change
00068     return false;
00069 }
00070 
00071 bool
00072 LT_MinimaSet::addSaddle(const State* const si, const State* const sj, const State* const s) {
00073     // no saddle handling and therefore no change
00074     return false;
00075 }
00076 
00077 const size_t 
00078 LT_MinimaSet::addMin(const State* const s) {
00079     assertbiu(s != NULL, "given minimum State to add is NULL");
00080     assertbiu(getMinIndex(s) == LandscapeTopology::INVALID_INDEX, "given minimum is known and already part of the landscape");
00081     
00082     // add a copy of s to the set
00083     vMinima.push_back(s->clone());
00084     
00085      // update mfeIndex if necessary
00086     if (vMinima.size() == 1) {
00087         mfeIndex = 0;
00088     } else {
00089         if (State::less( s, vMinima[mfeIndex])) {
00090               // new energy is smaller 
00091               // OR new energy is equal but string representation is smaller
00092             mfeIndex = vMinima.size()-1;
00093               // is not sorted anymore because smallest is at the end now
00094             sorted = false;
00095         } else if (sorted) {
00096               // compare last with newly added if the new one is not smaller
00097             sorted = State::less( vMinima[vMinima.size()-2], s );
00098         }
00099     }
00100 
00101     return vMinima.size()-1;
00102 }
00103 
00104 
00105 const State* const
00106 LT_MinimaSet::getMin(size_t i) const {
00107     assertbiu(i < vMinima.size(), "minimum index i out of bound!");
00108     
00109     return vMinima[i];
00110 }
00111 
00112 
00113 const size_t 
00114 LT_MinimaSet::getMinIndex(const State* const s) const{
00115     assertbiu(s != NULL, "given minimum State to get index of is NULL");
00116 
00117     for (size_t i = 0; i < vMinima.size(); ++i)
00118         if(*vMinima[i] == *s) 
00119             return i;
00120     
00121     return LandscapeTopology::INVALID_INDEX;
00122 }
00123 
00124 const size_t 
00125 LT_MinimaSet::getMinCount() const {
00126 
00127     return vMinima.size();
00128 }
00129 
00130 const State* const 
00131 LT_MinimaSet::getMFEState() const {
00132     assertbiu(mfeIndex != ell::LandscapeTopology::INVALID_INDEX, "no minimum present and therefore no mfe state too");
00133     return vMinima[mfeIndex];   
00134 }
00135 
00136 const std::vector<const State* > & 
00137 LT_MinimaSet::getAllMin() const {
00138     return vMinima;
00139 }
00140 
00141 
00142 
00144 // Calculates the relative 'distance' between landscape topologies.
00145 // On the LandscapeTopology level it is done on the minima only. For each
00146 // minimum m present in only one of the two LTs a value of 
00147 // MISSING_MINIMUM_PENALTY*(e^(-energy(m)/BOLTZMANN_KT)) is summed. The 
00148 // resulting sum is normalized by the number of these not-matched minima.
00149 // @param lt the landscape topology to calculate the distance to
00150 // @return the distance
00151 double
00152 LT_MinimaSet::
00153 getDistance(const LandscapeTopology* const lt) const
00155 {
00156     assertbiu(lt!=NULL, "given LandscapeTopology to compare to is NULL!");
00157 
00158       // check if comparing with itself
00159     if (this == lt)
00160         return 0.0;
00161     
00162       // check if lt is an LT_MinimaSet object
00163     const LT_MinimaSet* const ms = dynamic_cast<const LT_MinimaSet* const>(lt);
00164     if ( ms == NULL ) { 
00165         // no LT_MinimaSet --> use default function
00166         return LandscapeTopology::getDistance(lt);
00167     }
00168 
00169       // get access to all minima
00170     typedef std::vector<const State*> StateVec;
00171       // m1 are all sorted minima of this object
00172     StateVec* m1 = &(this->vMinima);
00173     if (!this->isSorted()) {
00174         m1 = new StateVec(vMinima);
00175         std::sort(m1->begin(), m1->end(), State::less);
00176     }
00177       // m2 are all sorted minima of ms object
00178     StateVec* m2 = &(ms->vMinima);
00179     if (!ms->isSorted()) {
00180         m2 = new StateVec(ms->vMinima);
00181         std::sort(m2->begin(), m2->end(), State::less);
00182     }
00183     
00184       // calculate distance via helper function
00185     double dist = LandscapeTopology::getSortedMinimaDistance(*m1,*m2);
00186     
00187       // garbage collection
00188     if ( ! this->isSorted() ) {
00189         delete m1;
00190     }
00191     if ( ! ms->isSorted() ) {
00192         delete m2;
00193     }
00194     
00195     return dist;
00196 }
00197 
00198 
00199 void 
00200 LT_MinimaSet::sort() {
00201       // sort only if necessary
00202     if (sorted)
00203         return;
00204       // sort minima
00205     std::sort(vMinima.begin(),vMinima.end(),State::less);
00206       // update mfe index
00207     mfeIndex = 0;
00208       // set sorted
00209     sorted = true;
00210 }
00211 
00212 bool
00213 LT_MinimaSet::isSorted() const {
00214     return sorted;
00215 }
00216 
00217 std::pair<int,std::string>
00218 LT_MinimaSet::read( std::istream & in, 
00219                     const State * const templateState )
00220 {
00221 
00222     typedef std::pair<int,std::string> RETURNTYPE;
00223     State* s;
00224     
00225       // stores the mapping of the minima indices in the file to the indices
00226       // used in the barrier tree to add the barriers
00227     Int2SizeT_MAP idx2min;
00228     
00229     std::string line;
00230     std::stringstream sstr;
00231     
00232     bool headerParsed = false;
00233     size_t dim = 0;
00234     size_t lineNumber = 0;
00235     
00236     std::string::size_type i;
00237     
00238     
00242 
00243     while( std::getline(in, line) ) {
00244         ++lineNumber;
00245         s = NULL;
00246         
00248         if(line.find("GRAPHVIZ") != std::string::npos) {
00249             break;
00250         }
00251         
00253           // check if next line describes the header information
00255         i = line.find(OUTPUT_KEY_ELL_HEAD);
00256         if(i != std::string::npos) {
00257             
00258             std::string lt_id("");
00259             dim = 0;
00260             std::string stateDescr("");
00261             
00262               // parse the minimum
00263             RETURNTYPE ret 
00264                 = LandscapeTopology::parseHeader(   line.substr(i), 
00265                                                     lt_id,
00266                                                     dim,
00267                                                     stateDescr );
00268               // check for parsing errors
00269             if (ret.first != 0) {
00270                 sstr.clear();
00271                 sstr <<"line " <<lineNumber
00272                     <<" : "<<ret.second;
00273                 return RETURNTYPE(ret.first,sstr.str());
00274             }
00275               // check for semantical errors
00276             if ( lt_id.compare(LT_MinimaSet::LT_ID) != 0 ) {
00277                 return RETURNTYPE(-1,"ELL-LT-TYPE not supported");
00278             }
00279             if (stateDescr.compare(templateState->getID()) != 0 ) {
00280                 return RETURNTYPE(-2,"STATETYPE differs from given type");
00281             } 
00282             headerParsed = true;
00283         }
00284         
00286           // check if next line describes a minimum
00288         i = line.find(OUTPUT_KEY_MINIMUM);
00289         if(i != std::string::npos) {
00290               // check if header already parsed
00291             if (!headerParsed) {
00292                 sstr.clear();
00293                 sstr <<"line " <<lineNumber
00294                     <<" : minimum present but no header found so far";
00295                 return RETURNTYPE(-2,sstr.str());
00296             }
00297             
00298               // count the new found minimum
00299             if (dim == 0) {
00300                 return RETURNTYPE(-3,"MORE minima present than given via DIMENSION");
00301             }
00302             dim--;
00303             
00304               // parse the minimum
00305             RETURNTYPE ret 
00306                 = LandscapeTopology::parseMinimum(  *this, 
00307                                                     line.substr(i), 
00308                                                     templateState, 
00309                                                     idx2min );
00310             
00311               // check for parsing errors
00312             if (ret.first != 0) {
00313                 sstr.clear();
00314                 sstr <<"line " <<lineNumber
00315                     <<" : "<<ret.second;
00316                 return RETURNTYPE(ret.first,sstr.str());
00317             }
00318         } 
00319         
00320           // garbage collection
00321         if (s != NULL) {
00322             delete s;
00323             s = NULL;
00324         }
00325         
00326     }
00327 
00328     if (dim > 0) {
00329         return RETURNTYPE(-3,"LESS minima present than given via DIMENSION");
00330     }
00331     
00332     return RETURNTYPE(0,"");
00333 }
00334 
00335 void
00336 LT_MinimaSet::write(    std::ostream& out, 
00337                         const bool writeGraph ) const
00338 {
00339     size_t dim = vMinima.size();
00340 
00343     
00344     const std::string dummyID = "ell::State";
00345     LandscapeTopology::writeHeader(out, LT_ID, dim, (vMinima.size()==0?dummyID:vMinima[0]->getID()));
00346 
00347       // put all minima in with..
00348       // format : //MINIMUM index toString()
00349     for (size_t i = 0; i < dim; ++i) {
00350         LandscapeTopology::writeMinimum( out, vMinima.at(i), i );
00351     }
00352     
00353     if (writeGraph) {
00354         out << "\n" << "//GRAPHVIZ"<<"\n";
00355         
00357         out << "graph SADDLENET {"<<"\n";
00358         for (size_t i = 0; i < dim; ++i) {
00359             out << "\t\tM"<<i<<";"<<"\n";           
00360         }   
00361         out << "label = \"\\n\\nLT_MinimaSet Diagram\\nwith "<<dim<<" minima \";"<<"\n";
00362         out << "fontsize=20;"<<"\n";
00363         out << "} "<<"\n";
00364     }
00365     
00366       // flush the output stream
00367     out.flush();
00368     
00369 }
00370 
00371 
00372 }//ell
00373 
00374