Tutorials‎ > ‎

Tutorial 5: ObjectChromosome

This tutorial shows how to use the ObjectChromosome in Jenes. The ObjectChromosome represents a complex chromosome with genes containing an object allele value. Given a search space made of object chromosome variables, we aim at finding the nearest sequence of values (representable with specified objects) to a target one. In our example problem each chromosome has five genes belonging to different object allele values. The first gene is an integer in the range [0,9], the second is also an integer but in the range [10,30], the third is a real number in the range [0,1], the fourth is a boolean and the last is a gene which alphabet is {BLACK, RED, WHITE}

Package:
jenes.tutorials.problem5

Files:
OCProblem.java
Color.java
DoubleAlleleSet.java
IntegerAlleleSet.java.

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

An ObjectChromosome is suitable for this problem. It will have five genes each of them with a different allele set: two integer allele sets for the first two genes, the first with the range [0,9] and the second with range [10,30], a double allele set for the third gene with range [0,1], a boolean allele set for the forth gene and an enumeration based allele set containing {BLACK, RED, WHITE } for the last gene.

To instantiate an allele set we can implement the AlleleSet interface or use the GenericAlleleSet class. The integer allele set is obtained by subclassing the GenericAlleleSet class and implementing  two factory methods, used to instantiate the integer values in their specified ranges (the values are chosen at random or with a uniform probability distribution within the specified ranges). The real allele set is obtained in the same way. The boolean allele set is obtained by creating a GenericAlleleSet object containg the boolean values true and false. Finally, we define an enumeration, called Color, containing the values BLACK, RED and WHITE, then we create a GenericAlleleSet object with the values of Color enumeration. The code is showed below.

36 public enum Color {
37     
38     RED, BLACK, WHITE;
39     
40 }

We can now build our chromosome representation.

053     private static ObjectChromosome template =
054             new ObjectChromosomeIntegerAlleleSet.createUniform(1009),
055             IntegerAlleleSet.createUniform(211030),
056             DoubleAlleleSet.createRandom(1001),
057             new GenericAlleleSet<Boolean>(true, false),
058             new GenericAlleleSet<Color>(Color.values()) );


2. Set-up the genetic algorithm

The code to evaluate an individual in our problem is shown below.

062         @Override
063         public void evaluate(Individual<ObjectChromosome> individual) {
064             ObjectChromosome template = individual.getChromosome();
065             
066             int i1 = (Integer)template.getValue(0);
067             int i2 = (Integer)template.getValue(1);
068             double d = (Double)template.getValue(2);
069             boolean b = (Boolean)template.getValue(3);
070             Color c = (Color)template.getValue(4);
071             
072             double acc = b ? (3*i1 + 4*i2 + d: i1;
073             
074             switch) {
075             case BLACK : acc += 10break;
076             case RED   : acc += 10break;
077             case WHITE : acc += 10break;
078             }
079             
080             individual.setScore(acc);
081         }


In our example the best individuals are those with the highest values for the integer and double genes, with a true value for the boolean gene and with any value for the "Color" gene.
Therefore, the best individual that we expect running this example could be: [ 9, 30, 0.99, true, BLACK ].

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

In this tutorial we use the classical simple algorithm configuration.

098         ga.addStage(new TournamentSelector<ObjectChromosome>(3));
099         ga.addStage(new OnePointCrossover<ObjectChromosome>(0.8));
100         ga.addStage(new SimpleMutator<ObjectChromosome>(0.02));

4. Customize the genetic algorithm

The global genetic algorithm goal is to maximize the fitness values. The whole code is visible in the files attached to this tutorial.