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
00034 if (this == &toCopy) {
00035 return *this;
00036 }
00037
00038 this->clear();
00039
00040
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
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
00068 return false;
00069 }
00070
00071 bool
00072 LT_MinimaSet::addSaddle(const State* const si, const State* const sj, const State* const s) {
00073
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
00083 vMinima.push_back(s->clone());
00084
00085
00086 if (vMinima.size() == 1) {
00087 mfeIndex = 0;
00088 } else {
00089 if (State::less( s, vMinima[mfeIndex])) {
00090
00091
00092 mfeIndex = vMinima.size()-1;
00093
00094 sorted = false;
00095 } else if (sorted) {
00096
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
00145
00146
00147
00148
00149
00150
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
00159 if (this == lt)
00160 return 0.0;
00161
00162
00163 const LT_MinimaSet* const ms = dynamic_cast<const LT_MinimaSet* const>(lt);
00164 if ( ms == NULL ) {
00165
00166 return LandscapeTopology::getDistance(lt);
00167 }
00168
00169
00170 typedef std::vector<const State*> StateVec;
00171
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
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
00185 double dist = LandscapeTopology::getSortedMinimaDistance(*m1,*m2);
00186
00187
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
00202 if (sorted)
00203 return;
00204
00205 std::sort(vMinima.begin(),vMinima.end(),State::less);
00206
00207 mfeIndex = 0;
00208
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
00226
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
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
00263 RETURNTYPE ret
00264 = LandscapeTopology::parseHeader( line.substr(i),
00265 lt_id,
00266 dim,
00267 stateDescr );
00268
00269 if (ret.first != 0) {
00270 sstr.clear();
00271 sstr <<"line " <<lineNumber
00272 <<" : "<<ret.second;
00273 return RETURNTYPE(ret.first,sstr.str());
00274 }
00275
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
00288 i = line.find(OUTPUT_KEY_MINIMUM);
00289 if(i != std::string::npos) {
00290
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
00299 if (dim == 0) {
00300 return RETURNTYPE(-3,"MORE minima present than given via DIMENSION");
00301 }
00302 dim--;
00303
00304
00305 RETURNTYPE ret
00306 = LandscapeTopology::parseMinimum( *this,
00307 line.substr(i),
00308 templateState,
00309 idx2min );
00310
00311
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
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
00348
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
00367 out.flush();
00368
00369 }
00370
00371
00372 }
00373
00374