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