jenes.tutorial.problem5.OCProblem.java

001 /*
002  * CISELab, Computational and Intelligent Systems Engineering Laboratory
003  * Department of Engineering
004  * University of Sannio, 82100 Benevento (ITALY)
005  * web-site: www.ciselab.org
006  *
007  * JENES, A Java Library for Genetic Algorithms
008  * Copyright (C) 2009, Luigi Troiano
009  *
010  * This library is free software; you can redistribute it and/or
011  * modify it under the terms of the GNU Lesser General Public
012  * License as published by the Free Software Foundation; either
013  * version 2.1 of the License, or (at your option) any later version.
014  *
015  * This library is distributed in the hope that it will be useful,
016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018  * Lesser General Public License for more details.
019  *
020  * You should have received a copy of the GNU Lesser General Public
021  * License along with this library; if not, write to the Free Software
022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
023 02110-1301  USA
024  *
025  * COPYRIGHT DISCLAIMER
026  * CISELab, hereby disclaims all copyright interest in the
027  * library `JENES' (A Java Library for Genetic Algorithms)
028  * written by Luigi Troiano et al.
029  *
030  * Luigi Troiano, 1 January 2009
031  * CISELab Coordinator
032  *
033  */
034 package jenes.tutorials.problem5;
035 
036 import jenes.GeneticAlgorithm;
037 import jenes.chromosome.GenericAlleleSet;
038 import jenes.chromosome.ObjectChromosome;
039 import jenes.population.Individual;
040 import jenes.population.Population;
041 import jenes.population.Population.Statistics;
042 import jenes.stage.operator.common.OnePointCrossover;
043 import jenes.stage.operator.common.SimpleMutator;
044 import jenes.stage.operator.common.TournamentSelector;
045 import jenes.tutorials.utils.Utils;
046 
047 public class OCProblem {
048     
049     private static int POPULATION_SIZE = 10;
050     private static int GENERATION_LIMIT = 100;
051     
052     private static ObjectChromosome template =
053             new ObjectChromosome( IntegerAlleleSet.createUniform(1009),
054             IntegerAlleleSet.createUniform(211030),
055             DoubleAlleleSet.createRandom(1001),
056             new GenericAlleleSet<Boolean>(true, false),
057             new GenericAlleleSet<Color>(Color.values()) );
058     
059     private static GeneticAlgorithm<ObjectChromosome> ga =
060             new GeneticAlgorithm<ObjectChromosome>( new Population<ObjectChromosome>(new Individual<ObjectChromosome>(template), POPULATION_SIZE), GENERATION_LIMIT){
061         
062         @Override
063         protected void evaluateIndividual(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( c ) {
075             case BLACK : acc += 10break;
076             case RED   : acc += 10break;
077             case WHITE : acc += 10break;
078             }
079             
080             individual.setScore(acc);
081         }
082         
083     };
084     
085     public static void main(String[] args)throws Exception {
086         Utils.printHeader();
087         System.out.println();
088         
089         System.out.println("TUTORIAL 5:");
090         System.out.println("Find the nearest sequence of values (i.e. objects) to the target.");
091         System.out.println();
092         
093         //Random.getInstance().setStandardSeed();
094         
095         ga.addStage(new TournamentSelector<ObjectChromosome>(3));
096         ga.addStage(new OnePointCrossover<ObjectChromosome>(0.8));
097         ga.addStage(new SimpleMutator<ObjectChromosome>(0.02));
098         
099         ga.evolve();
100         
101         Statistics stats = ga.getCurrentPopulation().getStatistics();
102         GeneticAlgorithm.Statistics algostats = ga.getStatistics();
103         
104         System.out.println("Solution: ");
105         System.out.println(stats.getLegalHighestIndividual().getChromosome() );
106         System.out.println(stats.getLegalHighestIndividual());
107         System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
108         System.out.println();
109         
110         Utils.printStatistics(stats);
111     }
112 }
Java2html
Comments