001 /*
002 * JENES
003 * A time and 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.problem5;
020
021 import jenes.Fitness;
022 import jenes.GeneticAlgorithm;
023 import jenes.chromosome.GenericAlleleSet;
024 import jenes.chromosome.ObjectChromosome;
025 import jenes.population.Individual;
026 import jenes.population.Population;
027 import jenes.population.Population.Statistics;
028 import jenes.population.Population.Statistics.Group;
029 import jenes.stage.operator.common.OnePointCrossover;
030 import jenes.stage.operator.common.SimpleMutator;
031 import jenes.stage.operator.common.TournamentSelector;
032 import jenes.tutorials.utils.Utils;
033
034 /**
035 * Tutorial illustrating the use of object-oriented chromosomes, whose
036 * allele set can be defined by the user for each gene.
037 *
038 * In this example the chromosomes are combinations of colors. We aim at finding
039 * the vector of colors closest to a given sequence.
040 *
041 * This class defines the problem.
042 *
043 * @author Luigi Troiano
044 *
045 * @version 2.0
046 * @since 1.0
047 */
048 public class OCProblem {
049
050 private static int POPULATION_SIZE = 10;
051 private static int GENERATION_LIMIT = 100;
052
053 private static ObjectChromosome template =
054 new ObjectChromosome( IntegerAlleleSet.createUniform(10, 0, 9),
055 IntegerAlleleSet.createUniform(21, 10, 30),
056 DoubleAlleleSet.createRandom(10, 0, 1),
057 new GenericAlleleSet<Boolean>(true, false),
058 new GenericAlleleSet<Color>(Color.values()) );
059
060 private static Fitness<ObjectChromosome> fitness = new Fitness<ObjectChromosome>(true) {
061
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( 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 private static GeneticAlgorithm<ObjectChromosome> ga =
085 new GeneticAlgorithm<ObjectChromosome>( fitness, new Population<ObjectChromosome>(new Individual<ObjectChromosome>(template), POPULATION_SIZE), GENERATION_LIMIT);
086
087
088 public static void main(String[] args)throws Exception {
089 Utils.printHeader();
090 System.out.println();
091
092 System.out.println("TUTORIAL 5:");
093 System.out.println("Find the sequence of colors nearest to the target.");
094 System.out.println();
095
096 //Random.getInstance().setStandardSeed();
097
098 ga.addStage(new TournamentSelector<ObjectChromosome>(3));
099 ga.addStage(new OnePointCrossover<ObjectChromosome>(0.8));
100 ga.addStage(new SimpleMutator<ObjectChromosome>(0.02));
101
102 ga.evolve();
103
104 Statistics stats = ga.getCurrentPopulation().getStatistics();
105 GeneticAlgorithm.Statistics algostats = ga.getStatistics();
106
107 Group legals = stats.getGroup(Population.LEGALS);
108
109 System.out.println("Solution: ");
110 System.out.println(legals.get(0));
111 System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
112 System.out.println();
113
114 Utils.printStatistics(stats);
115 }
116 }
|
|
|