Tutorial main page - Top-Down page - Bottom-up page - Programming hints - EO documentation

EO Programming guide

Templates, Functors, STL Library, random numbers, EO programming style!


Templates
Most EO code si written using templates. This allows to write generic code, i.e. involving a class which doesn't have to be known when writing the code -- but only when compiling it. In some sense this is similar to naming variables in algebra: you can write a lot of equations involving some variable $x$ without knowing even it if will be an integer or a float (or a matrix or ...). The main basic type that is templatized in EO is the fitness: an EO object is some object which has a fitness of some type F that can be anything. The definition for that is (seeEO.h)

template<F> class EO

The idea is that, later in your code, you can declare for instance as in FirstBitGA.cpp

typedef eoBin<double> Genotype;

meaning that in that file, you will manipulate as Genotype objects that are EO objects whosefitness is a double.

Whereas the advantages are obvious (writing generic reusable code instead of having to rewrite the same pieces of code for different types), there are some drawbacks: namely, it makes some of the compiler error messages hard to understand; and it forbids the compilation of most parts of EO into an object library file, as the actual types are not known in advance.



Functors

Though EO is a library, it contains almost no functions!
EO only contains functors, that are objects which have a method called operator(). Such objects are used  as if they were a function, but the big differences are that

Functors are so intimately linked to EO that a base class (eoFunctorBase) has been designed to hold all functors. This base class is itself divided into three derived class. These classes tell you immediately what kind of arguments the operator() method requires and what kind of result it produces. See EO conventions, and the inheritance diagram of class eoFunctorBase.

Example

A good example is given by the eoEvalFuncPtr class

class MyClass
{ ...
    void operator()(ArgType arg)
       {
            // do what you have to do
       }
}; // end of class declaration

is used  later in the code in something  like
 

ArgType myArgument;
MyClass myObject;    // myObject is an object of class MyClass ...
myObject(myArgument);  // calls method operator() of object myObject with argument myArgument ...
 
 
 

Why not plain C functions?

Consider for instance variation operators. Of course, we could declare them as function acting on some objects.

Blabla



A very brief introduction to STL

All EO heavily relies on STL, the Standard Template Library.
But you don't have to know more than a few words of STL to use EO (like with "hello", "please" and "goodbye" you can survive in a foreign country :-) and even to contribute to new EO features.

You will only find here the basics of STL that you will need to understand most of EO code - and to guess what the parts you don't understand are actually doing. Don't worry, I don't understand everything :-)

STL provides the user with container and algorithms. And you can apply (almost) all algorithms on (almost) all containers.

Containers
Containers are high level data types used to hold simpler data - the most widely used example of a container is the vector construct.
The use of STL containers relieve the user from memory management.

There are many other types of containers that are not used in EO and that we will not present here.

STL Algorithms
Algorithms are functions acting on containers - the most widely used example of a STL algorithm is the sort function.
Blabla

Drawbacks
The main drawback I see in using STL is that it makes it almost impossible to use a debugger normally: whereas access to data is made simple to the programmer, data structures are actually so complex, and debuggers so willing to display everything that you get lines of template instantiation when asking your debugger what is inside some container! For instance I could never visualize some v[i] with gbd, v being an STL vector!
But there nonetheless are so many advantages !!!



Random numbers
Evolutionary Algorithms make intensive use of random numbers. Random numbers are simulated in computers by using pseudo-random number generators (RNGs for short), i.e. functions that return series of numbers who look random (w.r.t. some statistical criteria).

To make sure the random number generator is as good as possible, and to ensure reproducibility of the results across different platforms, EO has its own RNG, the ``Mersenne Twister'' random number generator MT19937 (thanks to Takuji Nishimura, see eoRNG.h comments).

Though you can define and use as many RNGs as you wish in EO, the library also provides you with a global RNG termed rng: using only that single RNG in all calls to random numbers allows one to be able to reproduce a given run:

As RNGs only produce (by definition) numbers that are uniformly distributed integers between 0 and some maximal number, EO provides you with random numbers following different probability distribution (e.g. floating point following normal distribution).

EO also provides random_generators that can be used in STL call to generate series of random numbers, as in eoPop initializers.



EO conventions and naming style
A few naming conventions should help you to navigate more easily through EO:

Tutorial main page - Top-Down page - Bottom-up page - Programming hints - EO documentation

Marc Schoenauer

Last modified: Mon Nov 6 07:01:57 CET 2000