KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > distinctGenes > MyChromosome


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  * Sample class: Descendent of Chromosome that creates Chromosomes with n
17 * CompositeGenes. All but one CompositeGene's have 4 sub-genes, the last one
18 * has only 3 sub-genes.
19  *
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class MyChromosome
24     extends Chromosome {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private static final String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
27
28   public MyChromosome()
29       throws InvalidConfigurationException {
30     super();
31   }
32
33   public MyChromosome(final Configuration a_configuration)
34       throws InvalidConfigurationException {
35     super(a_configuration);
36   }
37
38   public MyChromosome(final Configuration a_configuration,
39                       final int a_desiredSize)
40       throws InvalidConfigurationException {
41     super(a_configuration, a_desiredSize);
42   }
43
44   public MyChromosome(final Configuration a_configuration,
45                       final Gene a_sampleGene, final int a_desiredSize)
46       throws InvalidConfigurationException {
47     super(a_configuration, a_sampleGene, a_desiredSize);
48   }
49
50   public MyChromosome(final Configuration a_configuration, Gene a_sampleGene,
51                       int a_desiredSize,
52                       IGeneConstraintChecker a_constraintChecker)
53       throws InvalidConfigurationException {
54     this(a_configuration, a_sampleGene, a_desiredSize);
55   }
56
57   public boolean isHandlerFor(Object JavaDoc a_obj, Class JavaDoc a_class) {
58     if (a_class == MyChromosome.class) {
59       return true;
60     }
61     else {
62       return false;
63     }
64   }
65
66   /**{@inheritDoc}*/
67   public Object JavaDoc perform(Object JavaDoc a_obj, Class JavaDoc a_class, Object JavaDoc a_params)
68       throws Exception JavaDoc {
69     return randomInitialMyChromosome(getConfiguration());
70   }
71
72   public static IChromosome randomInitialMyChromosome(Configuration
73       a_configuration)
74       throws InvalidConfigurationException {
75     // Sanity check: make sure the given configuration isn't null.
76
// -----------------------------------------------------------
77
if (a_configuration == null) {
78       throw new IllegalArgumentException JavaDoc(
79           "Configuration instance must not be null");
80     }
81     // Lock the configuration settings so that they can't be changed
82
// from now on.
83
// -------------------------------------------------------------
84
a_configuration.lockSettings();
85     // First see if we can get a Chromosome instance from the pool.
86
// If we can, we'll randomize its gene values (alleles) and then
87
// return it.
88
// -------------------------------------------------------------
89
IChromosomePool pool = a_configuration.getChromosomePool();
90     if (pool != null) {
91       IChromosome randomChromosome = pool.acquireChromosome();
92       if (randomChromosome != null) {
93         Gene[] genes = randomChromosome.getGenes();
94         RandomGenerator generator = a_configuration.getRandomGenerator();
95         for (int i = 0; i < genes.length; i++) {
96           genes[i].setToRandomValue(generator);
97         }
98         randomChromosome.setFitnessValueDirectly(FitnessFunction.
99                                                  NO_FITNESS_VALUE);
100         return randomChromosome;
101       }
102     }
103     // We weren't able to get a Chromosome from the pool, so we have to
104
// construct a new instance and build it from scratch.
105
// ------------------------------------------------------------------
106
IChromosome sampleChromosome =
107         a_configuration.getSampleChromosome();
108     sampleChromosome.setFitnessValue(FitnessFunction.NO_FITNESS_VALUE);
109     Gene[] sampleGenes = sampleChromosome.getGenes();
110     Gene[] newGenes = new Gene[sampleGenes.length];
111     RandomGenerator generator = a_configuration.getRandomGenerator();
112     // All genes except the last one should contain 4 fields.
113
// ------------------------------------------------------
114
for (int i = 0; i < newGenes.length - 1; i++) {
115       CompositeGene newGene = new CompositeGene(a_configuration);
116       for (int j = 0; j < 4; j++) {
117         Gene field = new BooleanGene(a_configuration);
118         newGene.addGene(field);
119       }
120       newGenes[i] = newGene; //sampleGenes[i].newGene();
121
// Set the gene's value (allele) to a random value.
122
// ------------------------------------------------
123
newGenes[i].setToRandomValue(generator);
124     }
125     // The last gene should contain 3 fields only.
126
// -------------------------------------------
127
CompositeGene newGene = new CompositeGene(a_configuration);
128     for (int j = 0; j < 3; j++) {
129       Gene field = new BooleanGene(a_configuration);
130       newGene.addGene(field);
131       newGene.setToRandomValue(generator);
132     }
133     newGenes[newGenes.length - 1] = newGene;
134     // Finally, construct the new chromosome with the new random
135
// genes values and return it.
136
// ---------------------------------------------------------
137
return new Chromosome(a_configuration, newGenes);
138   }
139 }
140
Popular Tags