KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
13 import org.jgap.*;
14
15 /**
16  * This genetic operator performs Gaussian mutation across all genes in a given
17  * Chromosome.
18  *
19  * @author Klaus Meffert (modified JOONEGAP source)
20  * @since 2.0
21  */

22 public class GaussianMutationOperator
23     extends BaseGeneticOperator {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.21 $";
26
27   private double m_deviation;
28
29   private RandomGenerator m_rg;
30
31   /**
32    * Constructs a GaussianMutationOperator with a default deviation of 0.05.
33    * Attention: The configuration used is the one set with the static method
34    * Genotype.setConfiguration.
35    * @throws InvalidConfigurationException
36    *
37    * @author Klaus Meffert
38    * @since 2.0
39    */

40   public GaussianMutationOperator()
41       throws InvalidConfigurationException {
42     this(Genotype.getStaticConfiguration());
43   }
44
45   /**
46    * Constructs a GaussianMutationOperator with a default deviation of 0.05.
47    * @param a_config the configuration to use
48    *
49    * @throws InvalidConfigurationException
50    *
51    * @author Klaus Meffert
52    * @since 3.0
53    */

54   public GaussianMutationOperator(Configuration a_config)
55       throws InvalidConfigurationException {
56     this(a_config, 0.05d);
57   }
58
59   /**
60    * Constructs a GaussianMutationOperator with the given deviation.
61    * @param a_configuration the configuration to use
62    * @param a_deviation sic
63    *
64    * @throws InvalidConfigurationException
65    *
66    * @since 3.0 (since 2.0 without a_configuration)
67    */

68   public GaussianMutationOperator(final Configuration a_configuration,
69                                   final double a_deviation)
70       throws InvalidConfigurationException {
71     super(a_configuration);
72     m_deviation = a_deviation;
73   }
74
75   /**
76    * Executes the operation.
77    *
78    * @param a_population containing chromosomes to be mutated
79    * @param a_candidateChromosomes resulting chromosomes
80    *
81    * @author Klaus Meffert
82    * @since 2.0
83    */

84   public void operate(final Population a_population,
85                       final List a_candidateChromosomes) {
86     int size = Math.min(getConfiguration().getPopulationSize(),
87                         a_population.size());
88     if (m_rg == null) {
89       RandomGenerator rn = getConfiguration().getRandomGenerator();
90       m_rg = rn;
91     }
92 // if (rn instanceof RandomGenerator) {
93
// setRandomGenerator(rn);
94
// }
95
// else {
96
// setRandomGenerator(new GaussianRandomGenerator(1.0d));
97
// }
98
for (int i = 0; i < size; i++) {
99       Gene[] genes = a_population.getChromosome(i).getGenes();
100       IChromosome copyOfChromosome = null;
101       // For each Chromosome in the population...
102
// ----------------------------------------
103
for (int j = 0; j < genes.length; j++) {
104         double nextGaussian = m_rg.nextDouble();
105         double diff = nextGaussian * m_deviation;
106         // ...take a copy of it...
107
// -----------------------
108
if (copyOfChromosome == null) {
109           copyOfChromosome = (IChromosome) a_population.getChromosome(i).clone();
110           // ...add it to the candidate pool...
111
// ----------------------------------
112
a_candidateChromosomes.add(copyOfChromosome);
113           // ...then Gaussian mutate all its genes
114
genes = copyOfChromosome.getGenes();
115         }
116         // Process all atomic elements in the gene. For a StringGene this
117
// would be the length of the string, for an IntegerGene, it is
118
// always one element.
119
// --------------------------------------------------------------
120
if (genes[j] instanceof CompositeGene) {
121           CompositeGene compositeGene = (CompositeGene) genes[j];
122           for (int k = 0; k < compositeGene.size(); k++) {
123             mutateGene(compositeGene.geneAt(k), diff);
124           }
125         }
126         else {
127           mutateGene(genes[j], diff);
128         }
129       }
130     }
131   }
132
133   /**
134    * Helper: mutate all atomic elements of a gene.
135    *
136    * @param a_gene the gene to be mutated
137    * @param a_percentage the percentage the gene is to be mutated with
138    *
139    * @author Klaus Meffert
140    * @since 2.0
141    */

142   private void mutateGene(final Gene a_gene, final double a_percentage) {
143     for (int k = 0; k < a_gene.size(); k++) {
144       // Mutate atomic element by given percentage.
145
// ------------------------------------------
146
a_gene.applyMutation(k, a_percentage);
147     }
148   }
149
150   private void setRandomGenerator(final RandomGenerator a_rg) {
151     m_rg = a_rg;
152   }
153
154   /**
155    * Compares the given GeneticOperator to this GeneticOperator.
156    *
157    * @param a_other the instance against which to compare this instance
158    *
159    * @return a negative number if this instance is "less than" the given
160    * instance, zero if they are equal to each other, and a positive number if
161    * this is "greater than" the given instance
162    *
163    * @author Klaus Meffert
164    * @since 2.6
165    */

166   public int compareTo(final Object JavaDoc a_other) {
167     if (a_other == null) {
168       return 1;
169     }
170     GaussianMutationOperator op = (GaussianMutationOperator) a_other;
171     if (m_deviation != op.m_deviation) {
172       if (m_deviation > op.m_deviation) {
173         return 1;
174       }
175       else {
176         return -1;
177       }
178     }
179     // Everything is equal. Return zero.
180
// ---------------------------------
181
return 0;
182   }
183
184   /**
185    * @return the deviation set
186    *
187    * @author Klaus Meffert
188    * @since 3.1
189    */

190   public double getDeviation() {
191     return m_deviation;
192   }
193 }
194
Popular Tags