1 10 package org.jgap; 11 12 18 public class ChromosomeForTest 19 extends Chromosome { 20 private boolean m_isCloned; 21 22 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 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 if (getConfiguration() == null) { 58 throw new IllegalArgumentException ( 59 "Configuration instance must not be null"); 60 } 61 getConfiguration().lockSettings(); 65 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 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 newGenes[i] = sampleGenes[i].newGene(); 99 newGenes[i].setToRandomValue(generator); 102 } 103 return new ChromosomeForTest(getConfiguration(), newGenes); 107 } 108 109 public synchronized Object 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 (iex.getMessage()); 118 } 119 } 120 121 public static class TestResultHolder { 122 static int computedTimes; 123 } 124 public boolean isHandlerFor(Object a_obj, Class a_class) { 125 if (a_class == this.getClass()) { 126 return true; 127 } 128 else { 129 return false; 130 } 131 } 132 133 public Object perform(Object a_obj, Class a_class, Object a_params) 134 throws Exception { 135 return randomInitialChromosome2(); 136 } 137 } 138 | Popular Tags |