001 /*
002 * JENES
003 * A time asnd memory efficient Java library for genetic algorithms and more
004 * Copyright (C) 2011 Intelligentia srl
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
018 */
019 package jenes.tutorials.old.problem5;
020
021 import jenes.GeneticAlgorithm;
022 import jenes.chromosome.GenericAlleleSet;
023 import jenes.chromosome.ObjectChromosome;
024 import jenes.population.Individual;
025 import jenes.population.Population;
026 import jenes.population.Population.Statistics;
027 import jenes.stage.operator.common.OnePointCrossover;
028 import jenes.stage.operator.common.SimpleMutator;
029 import jenes.stage.operator.common.TournamentSelector;
030 import jenes.tutorials.utils.Utils;
031
032 /**
033 * Tutorial illustrating the use of object-oriented chromosomes, whose
034 * allele set can be defined by the user for each gene.
035 *
036 * In this example the chromosomes are combinations of colors. We aim at finding
037 * the vector of colors closest to a given sequence.
038 *
039 * This class defines the problem.
040 *
041 * @author Luigi Troiano
042 * @author Pierpaolo Lombardi
043 *
044 * @version 1.0
045 * @since 1.0
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(10, 0, 9),
054 IntegerAlleleSet.createUniform(21, 10, 30),
055 DoubleAlleleSet.createRandom(10, 0, 1),
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 public 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 += 10; break;
076 case RED : acc += 10; break;
077 case WHITE : acc += 10; break;
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 sequence of colors nearest 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 }
|
|
|