Back to Lesson 5 - Tutorial main page - Top-Down page - Bottom-up page - Programming hints - EO documentation

Templates/eoOneMax_complete.h

The user-defined code is on pink background.. Only the the character colors have the usual meaning.

/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
The above line is usefulin Emacs-like editors
*/

/*
Template for creating a new representation in EO
================================================
*/

#ifndef _eoOneMax_h
#define _eoOneMax_h
 

/** 
* A simple example class for bitstring (is NOT supposed to be used :-)
*/
template< class FitT>
class eoOneMax: public EO<FitT> {
public:
  /** Deafult Ctor: nothing to be done */
 eoOneMax() {}

 virtual string className() const { return "eoOneMax"; }
 

  /** printing... */
     void printOn(ostream& _os) const
     {
          // First write the fitness
         EO<FitT>::printOn(_os);
         _os << ' ';
         _os << b.size() << ' ' ;
         for (unsigned i=0; i<b.size(); i++)
             _os << b[i] << ' ' ;
     }

  /** reading... 
   * of course, your readFrom must be able to read what printOn writes!!!
   */
 void readFrom(istream& _is)
 {
      // of course you should read the fitness first!
     EO<FitT>::readFrom(_is);

     unsigned s;
     _is >> s;
     b.resize(s);
     for (unsigned i=0; i<s; i++)
         {
             bool bTmp;
             _is >> bTmp;
             b[i] = bTmp;
         } 
 }
  // brute setting (we could also have defined a Ctor from a vector<bool>)
 void setB(vector<bool> & _b)
 {
     b=_b;
 }

  // brute accessing (we could also define operator[] ...)
 const vector<bool> & B()
 {
     return b;
 }


private:     // put all data here
 std::vector<bool> b;
};

#endif


Back to Lesson 5 - Tutorial main page - Top-Down page - Bottom-up page - Programming hints - EO documentation
Marc Schoenauer

Last modified: Sat May 4 06:02:46 2002