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.problem1;
020
021 import jenes.GeneticAlgorithm;
022 import jenes.chromosome.BooleanChromosome;
023 import jenes.population.Individual;
024 import jenes.population.Population;
025 import jenes.stage.AbstractStage;
026 import jenes.stage.operator.common.OnePointCrossover;
027 import jenes.stage.operator.common.SimpleMutator;
028 import jenes.stage.operator.common.TournamentSelector;
029 import jenes.tutorials.utils.Utils;
030
031 /**
032 * Tutorial implementing a basic genetic algorithm.
033 * The problem consists in finding a vector full of zeros or ones.
034 *
035 * @author Luigi Troiano
036 * @author Pierpaolo Lombardi
037 *
038 * @version 1.0
039 *
040 * @since 1.0
041 */
042 public class BooleanProblem {
043
044 private static int POPULATION_SIZE=50;
045 private static int CHROMOSOME_LENGTH=100;
046 private static int GENERATION_LIMIT=1000;
047
048 public static void main(String[] args) throws Exception {
049
050 Utils.printHeader();
051 System.out.println();
052
053 System.out.println("TUTORIAL 1:");
054 System.out.println("This algorithm aims to find a vector of booleans that is entirely true or false.");
055 System.out.println();
056
057 // Random.getInstance().setStandardSeed();
058
059 Individual<BooleanChromosome> sample = new Individual<BooleanChromosome>(new BooleanChromosome(CHROMOSOME_LENGTH));
060 Population<BooleanChromosome> pop = new Population<BooleanChromosome>(sample, POPULATION_SIZE);
061
062 GeneticAlgorithm<BooleanChromosome> ga = new GeneticAlgorithm<BooleanChromosome>
063 (pop, GENERATION_LIMIT) {
064 @Override
065 public void evaluateIndividual(Individual<BooleanChromosome> individual) {
066 BooleanChromosome chrom = individual.getChromosome();
067 int count = 0;
068 int length=chrom.length();
069 for(int i=0;i<length;i++)
070 if(chrom.getValue(i))
071 count++;
072
073 individual.setScore(count);
074 }
075 };
076
077 AbstractStage<BooleanChromosome> selection = new TournamentSelector<BooleanChromosome>(3);
078 AbstractStage<BooleanChromosome> crossover = new OnePointCrossover<BooleanChromosome>(0.8);
079 AbstractStage<BooleanChromosome> mutation = new SimpleMutator<BooleanChromosome>(0.02);
080 ga.addStage(selection);
081 ga.addStage(crossover);
082 ga.addStage(mutation);
083
084 ga.setElitism(1);
085
086 ga.setBiggerIsBetter(false);
087 ga.evolve();
088
089 Population.Statistics stats = ga.getCurrentPopulation().getStatistics();
090 GeneticAlgorithm.Statistics algostats = ga.getStatistics();
091
092 System.out.println("Objective: " + (ga.isBiggerBetter() ? "Max! (All true)" : "Min! (None true)"));
093 System.out.println();
094
095 Individual solution = ga.isBiggerBetter() ? stats.getLegalHighestIndividual() : stats.getLegalLowestIndividual();
096
097 System.out.println("Solution: ");
098 System.out.println( solution );
099 System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
100 System.out.println();
101
102 Utils.printStatistics(stats);
103 }
104
105 }
|
|
|