src/ell/S_Explicit.cc
Go to the documentation of this file.00001 #include "ell/S_Explicit.hh"
00002
00003 #include <sstream>
00004 #include <iomanip>
00005
00006 #include <biu/RandomNumberFactory.hh>
00007
00008 namespace ell
00009 {
00010 const std::string S_Explicit::ID = std::string("ell::S_Explicit");
00011 const biu::Alphabet S_Explicit::numberAlph = biu::Alphabet(std::string("0123456789"),1);
00012
00013
00014 S_Explicit::S_Explicit( const LandscapeGraph* lg_
00015 , const size_t nodeIndex_ )
00016 : lg(lg_)
00017 , nodeIndex(nodeIndex_)
00018 , sRepLength(0)
00019 {
00020 assertbiu( lg != NULL, "no LandscapeGraph given (==NULL)");
00021 assertbiu( nodeIndex < lg->nodes(), "node index out of range");
00022
00023 std::ostringstream os;
00024 os <<lg->nodes();
00025
00026 sRepLength = os.str().size();
00027 }
00028
00029 S_Explicit::~S_Explicit()
00030 {
00031 }
00032
00033 const std::string &
00034 S_Explicit::getID( void ) const {
00035 return S_Explicit::ID;
00036 }
00037
00038
00039
00040 size_t
00041 S_Explicit::
00042 getNeighborNumber(void) const
00043 {
00044 return lg->degree(nodeIndex);
00045 }
00046
00047 bool
00048 S_Explicit::
00049 operator== (const State& state2) const
00050 {
00051 const S_Explicit*const s2 = dynamic_cast<const S_Explicit* const>(&state2);
00052 assertbiu(s2 != NULL, "given state to compare to is no S_Explicit instance");
00053
00054 return (this->nodeIndex == s2->nodeIndex);
00055 }
00056 bool
00057 S_Explicit::
00058 operator!= (const State& state2) const
00059 {
00060 const S_Explicit*const s2 = dynamic_cast<const S_Explicit* const>(&state2);
00061 assertbiu(s2 != NULL, "given state to compare to is no S_Explicit instance");
00062
00063 return (this->nodeIndex != s2->nodeIndex);
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 bool
00082 S_Explicit::
00083 operator< (const State& state2) const
00084 {
00085 const S_Explicit*const s2 = dynamic_cast<const S_Explicit* const>(&state2);
00086 assertbiu(s2 != NULL, "given state to compare to is no S_Explicit instance");
00087
00088 const double e1 = this->getEnergy();
00089 const double e2 = s2->getEnergy();
00090 return ( e1 < e2 || ( e1 == e2 && this->toString().compare(s2->toString()) < 0 ));
00091 }
00092
00093
00094
00095
00096 double
00097 S_Explicit::
00098 getEnergy() const
00099 {
00100 return lg->getNodeEnergy(nodeIndex);
00101 }
00102
00103
00104
00105
00106
00107
00108 unsigned int
00109 S_Explicit::
00110 getMinimalDistance(const State& state2) const
00111 {
00112 return 0;
00113 }
00114
00115
00116
00117 S_Explicit*
00118 S_Explicit::
00119 clone(State* toFill) const
00120 {
00121 if (toFill == NULL) {
00122 return new S_Explicit( lg, nodeIndex );
00123 }
00124
00125 assertbiu( dynamic_cast<S_Explicit*>(toFill) != NULL
00126 , "given State is no S_Explicit instance");
00127 S_Explicit* s = static_cast<S_Explicit*>(toFill);
00128
00129
00130 s->lg = this->lg;
00131 s->nodeIndex = this->nodeIndex;
00132 s->sRepLength = this->sRepLength;
00133
00134 return s;
00135 }
00136
00137
00138
00139 State*
00140 S_Explicit::
00141 fromString(const std::string& stringRep) const {
00142 assertbiu(stringRep.size() > 0, "given string representation is empty");
00143
00144 std::istringstream is (stringRep);
00145 size_t idx = UINT_MAX;
00146 is >> idx;
00147 assertbiu(idx != UINT_MAX, "could not read node index from string representation");
00148
00149 return new S_Explicit( lg, idx );
00150 }
00151
00152
00153
00154
00155 std::string
00156 S_Explicit::
00157 toString() const {
00158 std::ostringstream os;
00159 os <<std::setw(sRepLength)
00160 <<std::setfill('0')
00161 <<nodeIndex;
00162 return os.str();
00163 }
00164
00165
00166
00167
00168
00169
00170 std::string&
00171 S_Explicit::
00172 toString( std::string & toFill ) const {
00173
00174 toFill = toString();
00175 return toFill;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184 State::NeighborListPtr
00185 S_Explicit::
00186 getNeighborList() const {
00187 return NeighborListPtr(new SuccessiveNeighborList(this));
00188 }
00189
00190
00191
00192
00193
00194 State::NeighborListPtr
00195 S_Explicit::
00196 getRandomNeighborList() const {
00197 return NeighborListPtr(new RandomNeighborList(this));
00198 }
00199
00200
00201
00202
00203 State*
00204 S_Explicit::
00205 getRandomNeighbor(State* inPlaceNeigh ) const {
00206
00207 size_t pos = biu::RNF::getRN(this->getNeighborNumber());
00208
00209 return getNeighbor( pos, inPlaceNeigh );
00210
00211 }
00212
00214
00215
00216
00217
00218 CSequence
00219 S_Explicit::
00220 compress(void) const {
00221
00222 return numberAlph.compressS(this->toString());
00223 }
00224
00225
00226
00227
00228
00229
00230 CSequence&
00231 S_Explicit::
00232 compress(CSequence& toFill) const {
00233
00234 toFill = this->compress();
00235 return toFill;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 State*
00247 S_Explicit::
00248 uncompress(const CSequence& cseq, State* toFill ) const {
00249 assertbiu(cseq.size()>0, "no compressed sequence given (size==0)");
00250
00251 S_Explicit* ret = NULL;
00252 if (toFill == NULL) {
00253 ret = new S_Explicit( lg, nodeIndex );
00254 } else {
00255 ret = dynamic_cast<S_Explicit*>(toFill);
00256 assertbiu(ret != NULL, "given state toFill is no S_Explicit instance");
00257 ret->lg = lg;
00258 }
00259
00260 std::istringstream is( numberAlph.decompressS(cseq, sRepLength) );
00261 ret->nodeIndex = UINT_MAX;
00262 is >> ret->nodeIndex;
00263 assertbiu(ret->nodeIndex != UINT_MAX, "could not parse node index from compressed string representation");
00264
00265 return ret;
00266 }
00267
00268
00269
00270
00271
00272
00273 State*
00274 S_Explicit::
00275 uncompress(const CSequence& cseq) {
00276 assertbiu(cseq.size()>0, "no compressed sequence given (size==0)");
00277
00278 std::istringstream is( numberAlph.decompressS(cseq, sRepLength) );
00279 size_t idx = UINT_MAX;
00280 is >> idx;
00281 assertbiu(idx != UINT_MAX, "could not parse node index from compressed string representation");
00282
00283 if (idx==UINT_MAX)
00284 return NULL;
00285
00286 nodeIndex = idx;
00287
00288 return this;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298 State*
00299 S_Explicit::
00300 getNeighbor(const size_t index, State* neigh) const {
00301
00302 S_Explicit * ret = NULL;
00303 if (neigh == NULL) {
00304 ret = new S_Explicit( lg, nodeIndex );
00305 } else {
00306 ret = dynamic_cast<S_Explicit*>(neigh);
00307 assertbiu(ret != NULL, "the given neigh State is no S_Explicit instance");
00308 ret->lg = lg;
00309 }
00310
00311 LandscapeGraph::AdjItPair adj = lg->adjNodes( nodeIndex );
00312
00313 for (size_t i=0; i<index && adj.first !=adj.second; i++) {
00314 adj.first++;
00315 }
00316 assertbiu( adj.first != adj.second, "index out of range");
00317
00318 ret->nodeIndex = *(adj.first);
00319
00320 return ret;
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 State*
00332 S_Explicit::
00333 undoNeighborChange(const size_t index, State* const neigh) const {
00334 if (neigh == NULL) {
00335 return neigh;
00336 }
00337
00338 S_Explicit* ret = dynamic_cast<S_Explicit*>(neigh);
00339 assertbiu(ret != NULL, "the given neigh State is no S_Explicit instance");
00340
00341 ret->lg = lg;
00342 ret->nodeIndex = nodeIndex;
00343
00344 return ret;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354 State*
00355 S_Explicit::
00356 applyNeighborChange(const size_t index, State* const copy) const {
00357 S_Explicit* ret = dynamic_cast<S_Explicit*>(copy);
00358 assertbiu(ret != NULL, "the given neigh State is no S_Explicit instance");
00359
00360 LandscapeGraph::AdjItPair adj = lg->adjNodes( nodeIndex );
00361
00362 for (size_t i=0; i<index && adj.first !=adj.second; i++) {
00363 adj.first++;
00364 }
00365 assertbiu( adj.first != adj.second, "index out of range");
00366
00367 ret->nodeIndex = *(adj.first);
00368
00369 return ret;
00370 }
00371
00372 }
00373
00374
00375 namespace ell
00376 {
00377
00378 S_Explicit::LandscapeGraph::
00379 LandscapeGraph( )
00380 : Graph_UD( 0 )
00381 {
00382
00383 }
00384
00385 S_Explicit::LandscapeGraph::
00386 ~LandscapeGraph()
00387 {
00388
00389 }
00390
00391 size_t
00392 S_Explicit::LandscapeGraph::
00393 addNode( const double energy ) {
00394
00395 size_t newIndex = nodes();
00396 assertbiu( newIndex == nodeEnergy.size(), "node label size and node number differ");
00397
00398 setNodeNumber( newIndex + 1 );
00399
00400 nodeEnergy.push_back(energy);
00401
00402 return newIndex;
00403 }
00404
00405 double
00406 S_Explicit::LandscapeGraph::
00407 getNodeEnergy( const size_t nodeIndex ) const {
00408 assertbiu(nodeIndex < nodeEnergy.size(), "node index out of range");
00409 return nodeEnergy[nodeIndex];
00410 }
00411
00412
00413 }