Tutorials‎ > ‎

### Tutorial 4: How to use SimpleGa

In this tutorial we shows how to use SimpleGA, a standard GeneticAlgorithm subclass. It represents the classical configuration of a genetic algorithm, using only the main operators (selector, crossover and mutator) in a very simple pipe. It's useful when no complex operators are needed and a fast ga set-up is required. The goal of this tutorial is to minimize the Shannon entropy of a set of values within the range [0,1].

Package:
jenes.tutorial.problem4

Files:
EntropyProblem.java

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

A DoubleChromosome is suitable for this problem with each allele within the range [0,1].
``57         Individual<DoubleChromosome> sample = new Individual<DoubleChromosome>(new DoubleChromosome(CHROMOSOME_LENGTH,0,1));58         Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE);``

### 2. Set-up the genetic algorithm

To use SimpleGA we have to subclass it and implement the evaluateIndividual method. A possible implementation is showed below.
``62             protected void evaluateIndividual(Individual<DoubleChromosome> individual) {63                 DoubleChromosome chrom = individual.getChromosome();64                 65                 int length = chrom.length();66                 67                 double sum = 0.0;68                 for(int i=0; i<length; ++i) {69                     sum += chrom.getValue(i);70                 }71                 72                 double entropy = 0.0;73                 for(int i=0; i<length; ++i) {74                     double pxi = chrom.getValue(i)/sum;75                     chrom.setValue(i, pxi);76                     77                     entropy -= (pxi * Math.log(pxi) / Math.log(2));78                 }79                 80                 individual.setScore(entropy);81             }``
The fitness of each individual is equal to the entropy value according to the classical entropy definition: entropy is the measure of the amount of information that is missing before reception and is sometimes referred to as Shannon entropy.

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

With SimpleGA our genetic algorithm is already configured regarding the operators to use. By default it uses tournament selection, one-point crossover (with probability= 0.8) and simple mutator (with probability=0.2). Besides SimpleGA uses by default a random replacing strategy. Anyway, it's possibile to modify this default configuration configuring operators and elitism strategy as you want. In our case we use the default SimpleGA configuration.

### 4. Customize the genetic algorithm

The whole configuration code is listed below:

``57         Individual<DoubleChromosome> sample = new Individual<DoubleChromosome>(new DoubleChromosome(CHROMOSOME_LENGTH,0,1));58         Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE);59         60         SimpleGA<DoubleChromosome> ga = new SimpleGA<DoubleChromosome>(pop, GENERATION_LIMIT) {61             @Override62             protected void evaluateIndividual(Individual<DoubleChromosome> individual) {63                 DoubleChromosome chrom = individual.getChromosome();64                 65                 int length = chrom.length();66                 67                 double sum = 0.0;68                 for(int i=0; i<length; ++i) {69                     sum += chrom.getValue(i);70                 }71                 72                 double entropy = 0.0;73                 for(int i=0; i<length; ++i) {74                     double pxi = chrom.getValue(i)/sum;75                     chrom.setValue(i, pxi);76                     77                     entropy -= (pxi * Math.log(pxi) / Math.log(2));78                 }79                 80                 individual.setScore(entropy);81             }82             83         };``