java.tutorial.problem2.PatternProblem.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 package jenes.tutorials.problem2;
034 
035 import jenes.GenerationEventListener;
036 import jenes.GeneticAlgorithm;
037 import jenes.Random;
038 import jenes.chromosome.IntegerChromosome;
039 import jenes.population.Individual;
040 import jenes.population.Population;
041 import jenes.population.Population.Statistics;
042 import jenes.stage.AbstractStage;
043 import jenes.stage.Parallel;
044 import jenes.stage.operator.common.OnePointCrossover;
045 import jenes.stage.operator.common.SimpleMutator;
046 import jenes.stage.operator.common.TournamentSelector;
047 import jenes.stage.operator.common.TwoPointsCrossover;
048 import jenes.tutorials.utils.Utils;
049 
050 
051 public class PatternProblem implements GenerationEventListener {
052     
053     private static int POPULATION_SIZE = 100;
054     private static int CHROMOSOME_LENGTH = 10;
055     private static int GENERATION_LIMIT = 1000;
056     private static int MAX_INT = 49;
057     
058     private PatternGA algorithm = null;
059     
060     public PatternProblem() {
061         IntegerChromosome chrom = new IntegerChromosome(CHROMOSOME_LENGTH,0,MAX_INT);
062         Individual<IntegerChromosome> ind = new Individual<IntegerChromosome>(chrom);
063         Population<IntegerChromosome> pop = new Population<IntegerChromosome>(ind, POPULATION_SIZE);
064         
065         algorithm = new PatternGA(pop, GENERATION_LIMIT);
066         algorithm.setElitism(5);
067         
068         AbstractStage<IntegerChromosome> selection = new TournamentSelector<IntegerChromosome>(2);
069         
070         Parallel<IntegerChromosome> parallel = new Parallel<IntegerChromosome>(new SimpleDispenser<IntegerChromosome>(2));
071         
072         AbstractStage<IntegerChromosome> crossover1p = new OnePointCrossover<IntegerChromosome>(0.8);
073         parallel.add(crossover1p);
074         
075         AbstractStage<IntegerChromosome> crossover2p = new TwoPointsCrossover<IntegerChromosome>(0.5);
076         parallel.add(crossover2p);
077         
078         AbstractStage<IntegerChromosome> mutation = new SimpleMutator<IntegerChromosome>(0.02);
079         
080         algorithm.addStage(selection);
081         algorithm.addStage(parallel);
082         algorithm.addStage(mutation);
083         algorithm.setBiggerIsBetter(false);
084         algorithm.addGenerationEventListener(this);
085     }
086     
087     public void run(int[] target, int precision) {
088         algorithm.setTarget(target);
089         algorithm.setPrecision(precision);
090         algorithm.evolve();
091         
092         Population.Statistics stats = algorithm.getCurrentPopulation().getStatistics();
093         GeneticAlgorithm.Statistics algostats = algorithm.getStatistics();
094         
095         System.out.println();
096         System.out.print("Target:[");
097         forint i = 0; i < target.length; ++i ) {
098             System.out.print(target[i]+ ( i < target.length - " " ""));
099         }
100         System.out.println("]");
101         System.out.println();
102         
103         System.out.println("Solution: ");
104         System.out.println(stats.getLegalLowestIndividual().getChromosome() );
105         System.out.println(stats.getLegalLowestIndividual());
106         System.out.format("found in %d ms and %d generations.\n", algostats.getExecutionTime(), algostats.getGenerations() );
107         System.out.println();
108     }
109     
110     public void onGeneration(GeneticAlgorithm ga, long time) {
111         Statistics stat = ga.getCurrentPopulation().getStatistics();
112         System.out.println("Current generation: " + ga.getGeneration());
113         System.out.println("\tBest score: " + stat.getLegalLowestScore());
114         System.out.println("\tAvg score: " + stat.getLegalScoreAvg());
115     }
116     
117     private static void randomize(int[] sample) {
118         for(int i=0;i<sample.length;i++)
119             sample[i] = Random.getInstance().nextInt(0, MAX_INT+1);
120     }
121     
122     public static void main(String[] args) {
123         
124         Utils.printHeader();
125         System.out.println();
126         
127         System.out.println("TUTORIAL 2:");
128         System.out.println("This algorithm aims to autonomously find a vector of integers that best matches with a target vector.");
129         System.out.println();
130         
131         Random.getInstance().setStandardSeed();
132         
133         PatternProblem problem = new PatternProblem();
134         int[] target = new int[CHROMOSOME_LENGTH];
135         
136         randomize(target);
137         problem.run(target, 2);
138         
139         randomize(target);
140         problem.run(target, 0);
141         
142     }
143     
144 }
Comments