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.old.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.
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> 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.
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).
090 ga.evolve(); 5. Obtaining the result of evolution.
|
Tutorials >