Tutorials‎ > ‎

Tutorial 1: A simple boolean problem

Let's start with a simple problem. Given a search space made of boolean variables, we aim at finding a solution made entirely of true values (or false values). This problem is faced by designing a genetic algorithm working on boolean chromosomes. The fitness function is computed by counting the number of true values within the chromosome. So in order to find the solution with all true values we can maximize the fitness function, whilst we minimize the fitness function in order to find a solution made of all false values.

This tutorial will give the opportunity of understanding how a genetic algorithm is structured and taylored to an optimization problem. Moreover, we will see how listeners can be used to capture the algorithms events.

Package:
jenes.tutorial.problem1

Files:
BooleanProblem.java

1. Choose a problem suitable chromosome and create the initial population.

Choosing a suitable chromosome representation is the most important task to do before running a genetic algorithm. Which representation to use depends on the structure of problem solutions.  In our case, solutions are made of boolean arrays. Thus, BooleanChromosome looks to be the approppriate choice. Chromosomes represent the key component of solutions (i.e. Individuals). For building the initial population we need a prototype of solutions (sample), as shown by the following code.

062         Individual<BooleanChromosome> sample = new Individual<BooleanChromosome>(new BooleanChromosome(CHROMOSOME_LENGTH));
063         Population<BooleanChromosome> pop = new Population<BooleanChromosome>(sample, POPULATION_SIZE);

where

047     private static int POPULATION_SIZE=50;
048     private static int CHROMOSOME_LENGTH=100;
049     private static int GENERATION_LIMIT=1000;

2. Set-up the genetic algorithm.

Any algorithm in jenes is based on GeneticAlgorithm, an abstract class whose only abstract method is evaluateIndividual that is problem dependant. The code to subclass GeneticAlgorithm and to evaluate an individual in our problem is shown below.

065         GeneticAlgorithm<BooleanChromosome> ga = new GeneticAlgorithm<BooleanChromosome>
066                 (pop, GENERATION_LIMIT) {
067             @Override
068             protected void evaluateIndividual(Individual<BooleanChromosome> individual) {
069                 BooleanChromosome chrom = individual.getChromosome();
070                 int count = 0;
071                 int length=chrom.length();
072                 for(int i=0;i<length;i++)
073                     if(chrom.getValue(i))
074                         count++;
075                 
076                 individual.setScore(count);
077             }
078         };

3. Choose the operators to be used by genetic algorithm and add them as stages in the ga.

After the genetic algorithm is defined, we need to specify the sequence of operators population will pass through. The simplest scheme contains only three operators in sequence: one selector, one crossover and one mutator. However it is possible to create a more complex pipe having paralleles and sequences. For the purpose of this tutorial we will adopt the simple structure.

080         AbstractStage<BooleanChromosome> selection = new TournamentSelector<BooleanChromosome>(3);
081         AbstractStage<BooleanChromosome> crossover = new OnePointCrossover<BooleanChromosome>(0.8);
082         AbstractStage<BooleanChromosome> mutation = new SimpleMutator<BooleanChromosome>(0.02);

083         ga.addStage(selection);
084         ga.addStage(crossover);
085         ga.addStage(mutation);

4. Set the algorithm parameters and run the evolution.

It is possible to customize the genetic algorithm setting the elitism value and the optimization goal before to run the evolution. The elitism is the number of best individuals to hold in the next generation (1 in our case).

087         ga.setElitism(1);

The optimization goal can be either to maximize (by default) or minimize the fitness value of individuals. For example we consider the minimization problem.

089         ga.setBiggerIsBetter(false);

Finally, we can make the algorithm running.

090         ga.evolve();

5. Obtaining the result of evolution.

Jenes provides statistics for both the algorithm and the population. The first refer to statistics concerning the algorithm run, namely the times of initialization, starting, evolution, and generations. The second to the distribution of solutions and related fitness values, such as the individuals ordered by decreasing fitness function, the mean max, and min of fitness values. They can be retrieved at any moment. We will use them when the algorithm has finished.

092         Population.Statistics stats = ga.getCurrentPopulation().getStatistics();
093         GeneticAlgorithm.Statistics algostats = ga.getStatistics();
094         
095         System.out.println("Objective: " + (ga.isBiggerBetter() ? "Max! (All true)" "Min! (None true)"));
096         System.out.println();
097         
098         Individual solution = ga.isBiggerBetter() ? stats.getLegalHighestIndividual() : stats.getLegalLowestIndividual();
099         
100         System.out.println("Solution: ");
101         System.out.println( solution.getChromosome() );
102         System.out.println( solution );
103         System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
104         System.out.println();