KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > distinctGenes > Main


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.distinctGenes;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14
15 /**
16  * Simple class that demonstrates how to configure JGAP to use differently
17  * composed genes.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class Main {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private static final String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
25
26   /**
27    * Starts the example.
28    * @param args ignored here
29    */

30   public static void main(String JavaDoc[] args) {
31     int numEvolutions = 500;
32     Configuration gaConf = new DefaultConfiguration();
33     gaConf.reset();
34     gaConf.setFitnessEvaluator(new DeltaFitnessEvaluator());
35     gaConf.setPreservFittestIndividual(true);
36     gaConf.setKeepPopulationSizeConstant(false);
37     Genotype genotype = null;
38     final int chromeSize = 41; //number of genes in each chromosome
39
try {
40       IChromosome sampleChromosome = new MyChromosome(gaConf,
41           new BooleanGene(gaConf), chromeSize);
42       gaConf.setSampleChromosome(sampleChromosome);
43       gaConf.setPopulationSize(20);
44       gaConf.setFitnessFunction(new SampleFitnessFunction());
45       genotype = Genotype.randomInitialGenotype(gaConf);
46     }
47     catch (InvalidConfigurationException e) {
48       e.printStackTrace();
49       System.exit( -2);
50     }
51     int progress = 0;
52     int percentEvolution = numEvolutions / 10;
53     for (int i = 0; i < numEvolutions; i++) {
54       genotype.evolve();
55       // Print progress.
56
// ---------------
57
if (percentEvolution > 0 && i % percentEvolution == 0) {
58         progress++;
59         IChromosome fittest = genotype.getFittestChromosome();
60         double fitness = fittest.getFitnessValue();
61         System.out.println("Generation " + i +
62                            ": Currently fittest Chromosome has fitness " +
63                            fitness);
64         if (fitness < 0.0001) {
65           break;
66         }
67       }
68     }
69     // Print summary.
70
// --------------
71
IChromosome fittest = genotype.getFittestChromosome();
72     System.out.println("Fittest Chromosome has fitness " +
73                        fittest.getFitnessValue());
74   }
75 }
76
Popular Tags