KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > ChromosomeForTest


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 org.jgap;
11
12 /**
13  * Derived Chromosome class for testing purposes only.
14  *
15  * @author Klaus Meffert
16  * @since 2.5
17  */

18 public class ChromosomeForTest
19     extends Chromosome {
20   private boolean m_isCloned;
21
22   // Default constructor needed for construction via newInstance()
23
public ChromosomeForTest()
24       throws InvalidConfigurationException {
25     super(Genotype.getStaticConfiguration());
26   }
27
28   public ChromosomeForTest(Configuration a_config, final Gene[] a_initialGenes)
29       throws InvalidConfigurationException {
30     super(a_config, a_initialGenes);
31   }
32
33   public int getComputedTimes() {
34     return TestResultHolder.computedTimes;
35   }
36
37   public void resetComputedTimes() {
38     TestResultHolder.computedTimes = 0;
39   }
40
41   public double getFitnessValue() {
42     if (m_isCloned && m_fitnessValue < 0) {
43       // Record test result!
44
TestResultHolder.computedTimes++;
45     }
46     return super.getFitnessValue();
47   }
48
49   public void resetIsCloned() {
50     m_isCloned = false;
51   }
52
53   public IChromosome randomInitialChromosome2()
54       throws InvalidConfigurationException {
55     // Sanity check: make sure the given configuration isn't null.
56
// -----------------------------------------------------------
57
if (getConfiguration() == null) {
58       throw new IllegalArgumentException JavaDoc(
59           "Configuration instance must not be null");
60     }
61     // Lock the configuration settings so that they can't be changed
62
// from now on.
63
// -------------------------------------------------------------
64
getConfiguration().lockSettings();
65     // First see if we can get a Chromosome instance from the pool.
66
// If we can, we'll randomize its gene values (alleles) and then
67
// return it.
68
// ------------------------------------------------------------
69
IChromosomePool pool = getConfiguration().getChromosomePool();
70     if (pool != null) {
71       IChromosome randomChromosome = pool.acquireChromosome();
72       if (randomChromosome != null) {
73         Gene[] genes = randomChromosome.getGenes();
74         RandomGenerator generator = getConfiguration().getRandomGenerator();
75         for (int i = 0; i < genes.length; i++) {
76           genes[i].setToRandomValue(generator);
77         }
78         randomChromosome.setFitnessValueDirectly(FitnessFunction.
79                                                  NO_FITNESS_VALUE);
80         return randomChromosome;
81       }
82     }
83     // If we got this far, then we weren't able to get a Chromosome from
84
// the pool, so we have to construct a new instance and build it from
85
// scratch.
86
// ------------------------------------------------------------------
87
IChromosome sampleChromosome = getConfiguration().getSampleChromosome();
88     Gene[] sampleGenes = sampleChromosome.getGenes();
89     Gene[] newGenes = new Gene[sampleGenes.length];
90     RandomGenerator generator = getConfiguration().getRandomGenerator();
91     for (int i = 0; i < newGenes.length; i++) {
92       // We use the newGene() method on each of the genes in the
93
// sample Chromosome to generate our new Gene instances for
94
// the Chromosome we're returning. This guarantees that the
95
// new Genes are setup with all of the correct internal state
96
// for the respective gene position they're going to inhabit.
97
// -----------------------------------------------------------
98
newGenes[i] = sampleGenes[i].newGene();
99       // Set the gene's value (allele) to a random value.
100
// ------------------------------------------------
101
newGenes[i].setToRandomValue(generator);
102     }
103     // Finally, construct the new chromosome with the new random
104
// genes values and return it.
105
// ---------------------------------------------------------
106
return new ChromosomeForTest(getConfiguration(), newGenes);
107   }
108
109   public synchronized Object JavaDoc clone() {
110     try {
111       ChromosomeForTest chrom = new ChromosomeForTest(getConfiguration(),
112           ( (Chromosome)super.clone()).getGenes());
113       chrom.m_isCloned = true;
114       return chrom;
115     }
116     catch (InvalidConfigurationException iex) {
117       throw new IllegalStateException JavaDoc(iex.getMessage());
118     }
119   }
120
121   public static class TestResultHolder {
122     static int computedTimes;
123   }
124   public boolean isHandlerFor(Object JavaDoc a_obj, Class JavaDoc a_class) {
125     if (a_class == this.getClass()) {
126       return true;
127     }
128     else {
129       return false;
130     }
131   }
132
133   public Object JavaDoc perform(Object JavaDoc a_obj, Class JavaDoc a_class, Object JavaDoc a_params)
134       throws Exception JavaDoc {
135     return randomInitialChromosome2();
136   }
137 }
138
Popular Tags