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

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           // get minimal string representation length 
00023         std::ostringstream os;
00024         os <<lg->nodes();
00025           // == length of highest node index string
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       // How many neighbor indices are accessible.
00039       // @return the number of neighbors
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           // check
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           // check
00063         return (this->nodeIndex != s2->nodeIndex);
00064     }
00065     
00066         /* Implements a unique order on states based on their energy and
00067          * string representation. A state is smaller than another one iff
00068          * it has smaller energy or it has equal energy and a 
00069          * lexicographically smaller string representation (tie breaker).
00070          * 
00071          * This function will be overwritten to achive a better performance
00072          * than calling getEnergy() and getString().
00073          * 
00074          * If the energy function is non-degenerate the string comparison
00075          * is obsolete.
00076          * 
00077          * @param state2 the State object to compare to
00078          * @return true if this state is smaller than state2 according to 
00079          *         the unique order of the states
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           // check
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         /* Returns the state specific energy. */
00096     double
00097     S_Explicit::
00098 	getEnergy() const
00099     {
00100         return lg->getNodeEnergy(nodeIndex);
00101     }
00102 
00103     
00104         /* Returns the minimal number of steps via valid
00105          *  neighbored states from this to another valid State.
00106          *  @param state2 the State to reach
00107          */
00108     unsigned int
00109     S_Explicit::
00110 	getMinimalDistance(const State& state2) const
00111     {
00112         return 0;
00113     }
00114 
00115     
00116         /* Returns a pointer to a clone of the current state. */
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           // cast toFill
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           // copy data
00130         s->lg = this->lg;
00131         s->nodeIndex = this->nodeIndex;
00132         s->sRepLength = this->sRepLength;
00133         
00134         return s;
00135     }
00136     
00137         /* Returns a new State based on the current state. The new state
00138          *  differs only by the information given by stringRep. */
00139     State* 
00140     S_Explicit::
00141 	fromString(const std::string& stringRep) const {
00142         assertbiu(stringRep.size() > 0, "given string representation is empty");
00143           // parse index from stream
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           // create new state
00149         return new S_Explicit( lg, idx );
00150     }
00151     
00152         /* Returns a specific std::string representation of this
00153          *  State.
00154          */
00155     std::string 
00156     S_Explicit::
00157 	toString() const {
00158         std::ostringstream os;
00159         os  <<std::setw(sRepLength) // the length of the string representation
00160             <<std::setfill('0')     // the leading fill character for shorter indices
00161             <<nodeIndex;            // the index
00162         return os.str();
00163     }
00164     
00165         /* Fill the given string with a specific std::string 
00166          * representation of this State.
00167          * @param toFill the string to overwrite
00168          * @return the changed toFill in parameter
00169          */
00170     std::string& 
00171     S_Explicit::
00172 	toString( std::string & toFill ) const {
00173           // no the best way but easiest ....
00174         toFill = toString();
00175         return toFill;
00176     }
00177     
00178 
00179 
00180     // neighborhood
00181         /* Returns a list of all VALID neighbored states in
00182          *  the energy landscape in a specific order.
00183          */
00184     State::NeighborListPtr 
00185     S_Explicit::
00186 	getNeighborList() const {
00187         return NeighborListPtr(new SuccessiveNeighborList(this));
00188     }
00189 
00190         /* Returns a list of all VALID neighbored states in
00191          *  the energy landscape in a random order.
00192          *  If a state is given, this is changed to a neighbor.
00193          */
00194     State::NeighborListPtr 
00195     S_Explicit::
00196 	getRandomNeighborList() const  {
00197         return NeighborListPtr(new RandomNeighborList(this));
00198     }
00199 
00200         /* Returns a VALID random neighbored state.
00201          *  If a state is given, this is changed to a neighbor.
00202          */
00203     State* 
00204     S_Explicit::
00205 	getRandomNeighbor(State* inPlaceNeigh ) const {
00206           // get random neighbor index
00207         size_t pos = biu::RNF::getRN(this->getNeighborNumber());
00208           // generate random neighbor 
00209         return getNeighbor( pos, inPlaceNeigh );
00210 
00211     }
00212     
00214     
00215         /* Access to a compressed sequence representation of the state.
00216          * @return the compressed sequence representation
00217          */
00218     CSequence 
00219     S_Explicit::
00220 	compress(void) const {
00221           // compress string representation
00222         return numberAlph.compressS(this->toString());
00223     }
00224     
00225         /* Access to a compressed sequence representation of the state.
00226          * @param toFill a data structure to write the compressed 
00227          *               representation too
00228          * @return the compressed sequence representation
00229          */
00230     CSequence& 
00231     S_Explicit::
00232 	compress(CSequence& toFill) const {
00233           // compress
00234         toFill = this->compress();
00235         return toFill;
00236     }
00237     
00238         /* Uncompresses a compressed sequencce representation into a new
00239          * State object.
00240          * @param cseq the compressed sequence representation of a state
00241          * @param toFill a state object to uncompress too or NULL if a new 
00242          *               object has to be created
00243          * @return new State object that is encoded in cseq or NULL in error
00244          *         case.
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           // create return value
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           // parse and set index
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           // create state
00265         return ret;
00266     }
00267     
00268         /* Uncompresses a compressed sequencce representation into a this
00269          * State object.
00270          * @param cseq the compressed sequence representation of a state
00271          * @return this or NULL in error case
00272          */
00273     State* 
00274     S_Explicit::
00275 	uncompress(const CSequence& cseq) {
00276         assertbiu(cseq.size()>0, "no compressed sequence given (size==0)");
00277           // parse index
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           // check for parsing error
00283         if (idx==UINT_MAX)
00284             return NULL;
00285           // set index
00286         nodeIndex = idx;
00287           // create state
00288         return this;
00289     }
00290     
00291     
00292       // Access to a specific neighbor.
00293       // @param index the index of the neighbor in [0, getNeighborNumber())
00294       // @param neigh if != NULL this state should be converted into the 
00295       //              neighbor otherwise a new one is created and returned
00296       // @return the new neighbor state or NULL if the index is >= 
00297       // getNeighborNumber()
00298     State* 
00299     S_Explicit::
00300 	getNeighbor(const size_t index, State* neigh) const {
00301           // set up state to return
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           // get neighbor list
00311         LandscapeGraph::AdjItPair adj = lg->adjNodes( nodeIndex );
00312           // go to requested neighbor in adjacency list
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           // set neighbor node index
00318         ret->nodeIndex = *(adj.first);
00319           // return neighbor
00320         return ret;
00321     }
00322     
00323       // Undo the changes done to this state to generate the given neighbor.
00324       // The goal is to do less operations than doing a full copy like
00325       // (*neigh = *this).
00326       // @param index the neighbor index that was applied last 
00327       //              [ 0, getNeighborNumber() )
00328       // @param neigh the state resulting from the last changes
00329       // @return the copy of this object via undoing the last changes in 
00330       //         neigh or NULL if neigh == NULL
00331     State* 
00332     S_Explicit::
00333 	undoNeighborChange(const size_t index, State* const neigh) const {
00334         if (neigh == NULL) {
00335             return neigh;
00336         }
00337           // casting
00338         S_Explicit* ret = dynamic_cast<S_Explicit*>(neigh);
00339         assertbiu(ret != NULL, "the given neigh State is no S_Explicit instance");
00340           // undo possible changes
00341         ret->lg = lg;
00342         ret->nodeIndex = nodeIndex;
00343           // return copy of this
00344         return ret;
00345     }
00346     
00347       // Applies the necessary changes to a given state to get the neighbor.
00348       // Note: \@copy is expected to be a copy (i.e. *this == *copy) and
00349       // therefore direct changes are possible and != NULL. 
00350       // @param index the neighbor to generate
00351       // @param copy a copy of *this the changes should be applied to
00352       // @return copy = the neighbor state of the given index or NULL if the index
00353       //         is out of range
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           // get neighbor list
00360         LandscapeGraph::AdjItPair adj = lg->adjNodes( nodeIndex );
00361           // go to requested neighbor in adjacency list
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           // set neighbor node index
00367         ret->nodeIndex = *(adj.first);
00368           // return indexed neighbor
00369         return ret;
00370     }
00371 
00372 } // namespace ell
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           // get index of new node
00395         size_t newIndex = nodes();
00396         assertbiu( newIndex == nodeEnergy.size(), "node label size and node number differ");
00397           // increase node number
00398         setNodeNumber( newIndex + 1 );
00399           // set node label
00400         nodeEnergy.push_back(energy);
00401           // return index
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 } // namespace ell