Skip to content
Dave Knoester edited this page Jul 17, 2014 · 6 revisions

All ones is a canonical problem in genetic algorithms, mostly used for sanity checking. In all-ones, fitness is simply the number of 1s in a bitstring. There's exactly one fitness peak, and no epistasis. EALib is overkill for this problem, but here we go.

First, the includes:

#include <ea/evolutionary_algorithm.h>
#include <ea/generational_models/steady_state.h>
#include <ea/genome_types/bitstring.h>
#include <ea/fitness_functions/all_ones.h>

As you can see from the filenames, these includes bring in the definitions for an evolutionary algorithm, a steady state generational model, a bitstring genome type, and the all-ones fitness function. A central design feature of EALib is the components (like fitness functions, genome types, and yes, even evolutionary algorithms themselves) are found in their own .h file.

Now, let's define our all-ones EA. EALib code is heavily templated, so it's often easier to define the EA in a typedef, like so:

typedef evolutionary_algorithm
< direct<bitstring>
, all_ones
, mutation::operators::per_site<mutation::site::bit>
, recombination::asexual
, generational_models::steady_state< >
, ancestors::random_bitstring
> ea_type;

Unpacking this a bit, the top-level component is an evolutionary_algorithm. It has a number of template parameters. The first, direct<bitstring>, is the representation for individuals in the EA's population. The representation defines how to interpret the genome. The second parameter, all_ones, is the fitness function. We then find the mutation operator, recombination type, generational model, and ancestor generator. Each of these components can be defined in a variety of different ways, which we'll explore in later sections.

The rest of the code for this example can be found in ealib/examples/all_ones.cpp.

Clone this wiki locally