KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jgap.distr.*;
15
16 /**
17  * A implementation of the IPopulationMerger interface that merges two
18  * populations as specified based on the fitness function, that is, the n
19  * fittest chromosomes are returned in the new population, where n is supplied
20  * by parameter.
21  *
22  * @author Henrique Goulart
23  * @since 2.0
24  */

25 public class FittestPopulationMerger
26     implements IPopulationMerger {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.16 $";
29
30   public Population mergePopulations(final Population a_population1,
31                                      final Population a_population2,
32                                      final int a_new_population_size) {
33     /**@todo check if configurations of both pops are equal resp.
34      * their fitness evaluators!*/

35     try {
36       // All the chromosomes are placed in the first population for sorting.
37
a_population1.addChromosomes(a_population2);
38       // A sorting is made according to the chromosomes fitness values
39
// See the private class FitnessChromosomeComparator below to understand.
40
List allChromosomes = a_population1.getChromosomes();
41       Collections.sort(allChromosomes, new FitnessChromosomeComparator(
42           a_population1.getConfiguration()));
43       //Then a new population is created and the fittest "a_new_population_size"
44
//chromosomes are added.
45
Chromosome[] chromosomes = (Chromosome[]) allChromosomes.toArray(new
46           Chromosome[0]);
47       Population mergedPopulation = new Population(a_population1.
48           getConfiguration(), a_new_population_size);
49       for (int i = 0; i < a_new_population_size && i < chromosomes.length; i++) {
50         mergedPopulation.addChromosome(chromosomes[i]);
51       }
52       // Return the merged population.
53
// -----------------------------
54
return mergedPopulation;
55     } catch (InvalidConfigurationException iex) {
56       // This should never happen
57
throw new IllegalStateException JavaDoc(iex.getMessage());
58     }
59   }
60
61   /**
62    * This class is used to sort the merged population chromosomes
63    * according to their fitness values. For convenience, the
64    * sorting is done in a reverse way, so this comparator
65    * returns 1 if the first chromosome has a LOWER fitness value.
66    *
67    * @author Henrique Goulart
68    * @since 2.0
69    */

70   private class FitnessChromosomeComparator
71       implements Comparator {
72     private transient Configuration m_config;
73
74     // Reference to the current FitnessEvaluator Object, used for comparing
75
// chromosomes.
76
private FitnessEvaluator m_fEvaluator;
77
78     public FitnessChromosomeComparator(Configuration a_config) {
79       m_config = a_config;
80       m_fEvaluator = m_config.getFitnessEvaluator();
81     }
82
83     /**
84      * Implements the compare method using the fitness function.
85      * The comparation is implemented in a reverse way to make the
86      * merging easier (the list of chromosomes is sorted in a
87      * descending fitness value order).
88      *
89      * @param a_o1 first IChromosome to compare
90      * @param a_o2 second IChromosome to compare
91      * @return @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
92      */

93     public int compare(final Object JavaDoc a_o1, final Object JavaDoc a_o2) {
94       // The two objects passed are always Chromosomes, so a cast must be made.
95
IChromosome chr1 = (IChromosome) a_o1;
96       IChromosome chr2 = (IChromosome) a_o2;
97       // Reverse comparison.
98
if (m_fEvaluator.isFitter(chr2.getFitnessValue(),
99                                 chr1.getFitnessValue())) {
100         return 1;
101       }
102       else if (m_fEvaluator.isFitter(chr1.getFitnessValue(),
103                                      chr2.getFitnessValue())) {
104         return -1;
105       }
106       else {
107         return 0;
108       }
109     }
110   }
111 }
112
Popular Tags