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.tutorials.problem4

Files:
EntropyProblem.java
EntropyFitness.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].
```55         Individual<DoubleChromosome> sample = new Individual<DoubleChromosome> (new DoubleChromosome(CHROMOSOME_LENGTH,0,1)); 56         Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE);```

### 2. Set-up the genetic algorithm

To use SimpleGA we have to define only the fitness function coding. In this case 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.

```38     @Override 39     public void evaluate(Individual<DoubleChromosome> individual) { 40         DoubleChromosome chrom = individual.getChromosome(); 41  42         int length = chrom.length(); 43  44         double sum = 0.0; 45         for (int i = 0; i < length; ++i) { 46             sum += chrom.getValue(i); 47         } 48  49         double entropy = 0.0; 50         for (int i = 0; i < length; ++i) { 51             double pxi = chrom.getValue(i) / sum; 52             chrom.setValue(i, pxi); 53  54             entropy -= (pxi * Math.log(pxi) / Math.log(2)); 55         } 56  57         individual.setScore(entropy); 58     }```

### 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 (attemps = 2), one-point crossover (probability = 0.8) and simple mutator (probability = 0.2). Besides SimpleGA uses by default a random replacing strategy for elitism. Anyway, it's possibile to modify this default configuration providing different parameters to operators and/or a different elitism strategy. In our case we use the default SimpleGA configuration.

### 4. Customize the genetic algorithm

The whole configuration code is listed below:

```55         Individual<DoubleChromosome> sample = new Individual<DoubleChromosome>(new DoubleChromosome(CHROMOSOME_LENGTH,0,1)); 56         Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE); 57          58         ga = new SimpleGA<DoubleChromosome>(null, pop, GENERATION_LIMIT); 59          60         System.out.println("Solving Max!:"); 61         solve( EntropyFitness.MAX ); 62  63         System.out.println("Solving Min!:"); 64         solve( EntropyFitness.MIN );```

As you can see, the genetic algorithm has been initialized without the definition of the fitness function to use. This is possibile in that cases in wich the fitness could be determinated only at a later time in respect the creation of the genetic algorithm. Anyway the definition of a fitness function it required before the evolve method of GeneticAlgorithm is invoked, and this is possible just making the following method call:

```68         ga.setFitness(fitness); 69         ga.evolve();```