KickJava   Java API By Example, From Geeks To Geeks.

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


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  * The inversion operator randomly selects one chromosomes from the
17  * population and inverses it by randomly picking a splitting locus on which
18  * to swap the first part with the last part of the chromosome.
19  *
20  * @author Klaus Meffert
21  * @since 2.3
22  */

23 public class InversionOperator
24     extends BaseGeneticOperator {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
27
28   /**
29    * Default constructor.<p>
30    * Attention: The configuration used is the one set with the static method
31    * Genotype.setConfiguration.
32    *
33    * @author Klaus Meffert
34    * @since 2.3
35    */

36   public InversionOperator()
37       throws InvalidConfigurationException {
38     this(Genotype.getStaticConfiguration());
39   }
40
41   /**
42    * Constructs a new instance of this operator.
43    * @param a_config the configuration to use
44    *
45    * @author Klaus Meffert
46    * @since 3.0
47    */

48   public InversionOperator(Configuration a_config)
49       throws InvalidConfigurationException {
50     super(a_config);
51   }
52
53   /**
54    * @author Klaus Meffert
55    * @since 2.3
56    */

57   public void operate(final Population a_population,
58                       final List a_candidateChromosomes) {
59     // Work out the number of crossovers that should be performed.
60
// -----------------------------------------------------------
61
int size = Math.min(getConfiguration().getPopulationSize(),
62                         a_population.size());
63     RandomGenerator generator = getConfiguration().getRandomGenerator();
64     // For the inversion, grab a random chromosome, pick a random
65
// locus (gene location), and then swap that gene and all genes
66
// to the "right" (those with greater loci) of that gene with the left
67
// genes.
68
// -------------------------------------------------------------------
69
int index1;
70     index1 = generator.nextInt(size);
71     IChromosome chrom1 = a_population.getChromosome(index1);
72     IChromosome firstMate = (IChromosome) chrom1.clone();
73     Gene[] firstGenes = firstMate.getGenes();
74     int locus = generator.nextInt(firstGenes.length);
75     // Swap the genes.
76
// ---------------
77
Gene[] invertedGenes = new Gene[firstGenes.length];
78     int index = 0;
79     int len = firstGenes.length;
80     for (int j = locus; j < len; j++) {
81       invertedGenes[index++] = firstGenes[j];
82     }
83     for (int j = 0; j < locus; j++) {
84       invertedGenes[index++] = firstGenes[j];
85     }
86     try {
87       firstMate.setGenes(invertedGenes);
88     }
89     catch (InvalidConfigurationException cex) {
90       // Rethrow to have an unchecked exception.
91
// ---------------------------------------
92
throw new Error JavaDoc(cex);
93     }
94     // Add the modified chromosome to the candidate pool so that it'll be
95
// considered for natural selection during the next phase of evolution.
96
// --------------------------------------------------------------------
97
a_candidateChromosomes.add(firstMate);
98   }
99
100   /**
101    * Compares the given GeneticOperator to this GeneticOperator.
102    *
103    * @param a_other the instance against which to compare this instance
104    * @return a negative number if this instance is "less than" the given
105    * instance, zero if they are equal to each other, and a positive number if
106    * this is "greater than" the given instance
107    *
108    * @author Klaus Meffert
109    * @since 2.6
110    */

111   public int compareTo(final Object JavaDoc a_other) {
112     if (a_other == null) {
113       return 1;
114     }
115     InversionOperator op = (InversionOperator) a_other;
116     // Everything is equal. Return zero.
117
// ---------------------------------
118
return 0;
119   }
120 }
121
Popular Tags