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 functions with private data
-
you can have different funtors objects of the same class, i.e. you can
use the same functionality with different parameters
-
you can heave a hierarchy of functors objects, which means that you have
a hierarchy of functions with defaults behaviors and specialized sub-functions
-
...
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.
-
vector The
most widely used container is a one-dimensional array of items.
Data manipulation: suppose v
is an STL vector<AtomType>.
Then
v[i]
is the ith element of v, as in standard C arrays
v.size()
is the number of elements of v
v.push_back(atom)
appends the atom
at end of v,
provided of course that atom
is of type AtomType
blabla
-
pair
This simple container allows you to hold two
data types together. It is very handy for temporary data handling. Assuming
p is a pair<AtomType1,
AtomType2>,
p.first()
and p.second()
refer to the encapsulated data, of respective types AtomType1and
AtomType2.
-
Blabla
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 acess to data is made simple
to the progammer, data structures are actually so complex, and debuggers
so willing to display everything that you get lines of template instanciation
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 nonehteless 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 diffrerent 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 strange it seems for a random algorithm, it is mandatory for debugging
purposes
-
random numbers are computed starting from a seed - starting from the same
seed will lead to the same series of pseudo-random numbers, and hence to
the same results of the algorithms. All examples in this tutorial will
use the RNG seeding procedure, see e.g. in Lesson1.
-
to simulate "true" random runs, you can just seed the RNG with a machine-clock
realted number, e.g. calling time(0), as done for isntance in Lesson3
(and after).
As RNGs only produce (by definition) numbers that are uniformly distributed
integers between 0 and some maximal number, EO provides you with random
numbers follwing 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:
-
The name of local varoiables shoudl start with a lower case letter
-
The name of the parameters to a function shoudl start with an underscore
(_)
The initialization parameters of constructors, for instance,
shoudl be named from the names of the variables they are used to initialize.
-
The names of classes should start with eo + an Uppercase letter
-
Blabla
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