KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > DefaultMutationRateCalculator


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.impl;
11
12 import org.jgap.*;
13
14 /**
15  * Default implementation of a mutation rate calculcator.
16  *
17  * @author Klaus Meffert
18  * @since 1.1
19  */

20 public class DefaultMutationRateCalculator
21     extends BaseRateCalculator {
22   /** String containing the CVS revision. Read out via reflection!*/
23   private final static String JavaDoc CVS_REVISION = "$Revision: 1.16 $";
24
25   /**
26    * @param a_config the configuration to use
27    * @throws InvalidConfigurationException
28    *
29    * @author Klaus Meffert
30    * @since 3.0
31    */

32   public DefaultMutationRateCalculator(Configuration a_config)
33       throws InvalidConfigurationException {
34     super(a_config);
35   }
36
37   /**
38    * Calculates the mutation rate.
39    *
40    * @return calculated divisor of mutation rate probability (dividend is 1)
41    *
42    * @author Klaus Meffert
43    * @since 1.1 (same functionality as earlier, but not encapsulated)
44    */

45   public int calculateCurrentRate() {
46     int size = getConfiguration().getChromosomeSize();
47     if (size < 1) {
48       size = 1;
49     }
50     return size;
51   }
52
53   /**
54    * Determines whether mutation is to be carried out. In this case
55    * the rate is the size of the chromosome. There is therefore a
56    * probability of 1/totalgenes that a particular gene mutates.
57    *
58    * @param a_chrom ignored
59    * @param a_geneIndex ignored
60    * @return true if gene should be mutated
61    *
62    * @author Chris Knowles
63    * @since 2.0
64    */

65   public boolean toBePermutated(IChromosome a_chrom, int a_geneIndex) {
66     RandomGenerator generator = getConfiguration().getRandomGenerator();
67     return (generator.nextInt(calculateCurrentRate()) == 0);
68   }
69 }
70
Popular Tags