KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > simpleBoolean > SimpleExample


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package examples.simpleBoolean;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14
15 /**
16  * Simple class that demonstrates the basic usage of JGAP.
17  *
18  * @author Neil Rotstan
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public class SimpleExample {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private static final String JavaDoc CVS_REVISION = "$Revision: 1.8 $";
25
26   /**
27    * Starts the example.
28    * @param args if optional first argument provided, it represents the number
29    * of bits to use, but no more than 32
30    *
31    * @author Neil Rotstan
32    * @author Klaus Meffert
33    * @since 2.0
34    */

35   public static void main(String JavaDoc[] args) {
36     int numEvolutions = 500;
37     Configuration gaConf = new DefaultConfiguration();
38     gaConf.setPreservFittestIndividual(true);
39     gaConf.setKeepPopulationSizeConstant(false);
40     Genotype genotype = null;
41     int chromeSize;
42     if (args.length > 0) {
43       chromeSize = Integer.parseInt(args[0]);
44     }
45     else {
46       chromeSize = 16;
47     }
48     double maxFitness = Math.pow(2.0, (double) chromeSize) - 1;
49     if (chromeSize > 32) {
50       System.err.println("This example does not handle " +
51                          "Chromosomes greater than 32 bits in length.");
52       System.exit( -1);
53     }
54     try {
55       IChromosome sampleChromosome = new Chromosome(gaConf,
56           new BooleanGene(gaConf), chromeSize);
57       gaConf.setSampleChromosome(sampleChromosome);
58       gaConf.setPopulationSize(20);
59       gaConf.setFitnessFunction(new MaxFunction());
60       genotype = Genotype.randomInitialGenotype(gaConf);
61     }
62     catch (InvalidConfigurationException e) {
63       e.printStackTrace();
64       System.exit( -2);
65     }
66     int progress = 0;
67     int percentEvolution = numEvolutions / 100;
68     for (int i = 0; i < numEvolutions; i++) {
69       genotype.evolve();
70       // Print progress.
71
// ---------------
72
if (percentEvolution > 0 && i % percentEvolution == 0) {
73         progress++;
74         IChromosome fittest = genotype.getFittestChromosome();
75         double fitness = fittest.getFitnessValue();
76         System.out.println("Currently fittest Chromosome has fitness " +
77                            fitness);
78         if (fitness >= maxFitness) {
79           break;
80         }
81       }
82     }
83     // Print summary.
84
// --------------
85
IChromosome fittest = genotype.getFittestChromosome();
86     System.out.println("Fittest Chromosome has fitness " +
87                        fittest.getFitnessValue());
88   }
89 }
90
Popular Tags