01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licencing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
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 CVS_REVISION = "$Revision: 1.2 $";
25:
26: /**
27: * Starts the example.
28: * @param args ignored here
29: */
30: public static void main(String[] 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: } catch (InvalidConfigurationException e) {
47: e.printStackTrace();
48: System.exit(-2);
49: }
50: int progress = 0;
51: int percentEvolution = numEvolutions / 10;
52: for (int i = 0; i < numEvolutions; i++) {
53: genotype.evolve();
54: // Print progress.
55: // ---------------
56: if (percentEvolution > 0 && i % percentEvolution == 0) {
57: progress++;
58: IChromosome fittest = genotype.getFittestChromosome();
59: double fitness = fittest.getFitnessValue();
60: System.out.println("Generation " + i
61: + ": Currently fittest Chromosome has fitness "
62: + fitness);
63: if (fitness < 0.0001) {
64: break;
65: }
66: }
67: }
68: // Print summary.
69: // --------------
70: IChromosome fittest = genotype.getFittestChromosome();
71: System.out.println("Fittest Chromosome has fitness "
72: + fittest.getFitnessValue());
73: }
74: }
|