KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Implementation of a NaturalSelector that ensures a certain threshold of the
17  * best chromosomes are taken to the next generation.
18  *
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public class ThresholdSelector
23     extends NaturalSelector {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.14 $";
26
27   /**
28    * Stores the chromosomes to be taken into account for selection
29    */

30   private List m_chromosomes;
31
32   /**
33    * Indicated whether the list of added chromosomes needs sorting
34    */

35   private boolean m_needsSorting;
36
37   /**
38    * Comparator that is only concerned about fitness values
39    */

40   private FitnessValueComparator m_fitnessValueComparator;
41
42   private ThresholdSelectorConfigurable m_config
43       = new ThresholdSelectorConfigurable();
44
45   /**
46    * Default constructor. Uses threshold of 30 percent.<p>
47    * Attention: The configuration used is the one set with the static method
48    * Genotype.setConfiguration.
49    *
50    * @author Klaus Meffert
51    * @since 2.6
52    */

53   public ThresholdSelector() {
54     this(Genotype.getStaticConfiguration(), 0.3d);
55   }
56
57   /**
58    * @param a_config the configuration to use
59    * @param a_bestChromosomes_Percentage indicates the number of best
60    * chromosomes from the population to be selected for granted. All other
61    * chromosomes will be selected in a random fashion. The value must be in
62    * the range from 0.0 to 1.0.
63    *
64    * @author Klaus Meffert
65    * @since 2.0
66    */

67   public ThresholdSelector(final Configuration a_config,
68                            final double a_bestChromosomes_Percentage) {
69     super(a_config);
70     if (a_bestChromosomes_Percentage < 0.0000000d
71         || a_bestChromosomes_Percentage > 1.0000000d) {
72       throw new IllegalArgumentException JavaDoc("Percentage must be between 0.0"
73                                          + " and 1.0 !");
74     }
75     m_config.m_bestChroms_Percentage = a_bestChromosomes_Percentage;
76     m_chromosomes = new Vector();
77     m_needsSorting = false;
78     m_fitnessValueComparator = new FitnessValueComparator();
79   }
80
81   /**
82    * Select a given number of Chromosomes from the pool that will move on
83    * to the next generation population. This selection will be guided by the
84    * fitness values. The chromosomes with the best fitness value win.
85
86    * @param a_howManyToSelect the number of Chromosomes to select
87    * @param a_from_pop the population the Chromosomes will be selected from
88    * @param a_to_pop the population the Chromosomes will be added to
89    *
90    * @author Klaus Meffert
91    * @since 2.0
92    */

93   public void select(final int a_howManyToSelect, final Population a_from_pop,
94                      Population a_to_pop) {
95     if (a_from_pop != null) {
96       int size = a_from_pop.size();
97       for (int i = 0; i < size; i++) {
98         add(a_from_pop.getChromosome(i));
99       }
100     }
101     int canBeSelected;
102     if (a_howManyToSelect > m_chromosomes.size()) {
103       canBeSelected = m_chromosomes.size();
104     }
105     else {
106       canBeSelected = a_howManyToSelect;
107     }
108     // Sort the collection of chromosomes previously added for evaluation.
109
// Only do this if necessary.
110
// -------------------------------------------------------------------
111
if (m_needsSorting) {
112       Collections.sort(m_chromosomes, m_fitnessValueComparator);
113       m_needsSorting = false;
114     }
115     // Select the best chromosomes for granted
116
int bestToBeSelected = (int) Math.round(canBeSelected
117                                             * m_config.m_bestChroms_Percentage);
118     for (int i = 0; i < bestToBeSelected; i++) {
119       a_to_pop.addChromosome( (IChromosome) m_chromosomes.get(i));
120     }
121     // Fill up the rest by randomly selecting chromosomes
122
int missing = a_howManyToSelect - bestToBeSelected;
123     RandomGenerator rn = getConfiguration().getRandomGenerator();
124     int index;
125     int size = m_chromosomes.size();
126     for (int i = 0; i < missing; i++) {
127       index = rn.nextInt(size);
128       a_to_pop.addChromosome( (IChromosome) m_chromosomes.get(index));
129     }
130   }
131
132   /**
133    * @return false as we allow to return the same chromosome multiple times
134    *
135    * @author Klaus Meffert
136    * @since 2.0
137    */

138   public boolean returnsUniqueChromosomes() {
139     return false;
140   }
141
142   public void empty() {
143     m_chromosomes.clear();
144     m_needsSorting = false;
145   }
146
147   /**
148    *
149    * @param a_chromosomeToAdd Chromosome
150    *
151    * @author Klaus Meffert
152    * @since 2.0
153    */

154   protected void add(final IChromosome a_chromosomeToAdd) {
155     m_chromosomes.add(a_chromosomeToAdd);
156     m_needsSorting = true;
157   }
158
159   /**
160    * Comparator regarding only the fitness value. Best fitness value will
161    * be on first position of resulting sorted list.
162    *
163    * @author Klaus Meffert
164    * @since 2.0
165    */

166   private class FitnessValueComparator
167       implements Comparator {
168     public int compare(final Object JavaDoc a_first, final Object JavaDoc a_second) {
169       IChromosome chrom1 = (IChromosome) a_first;
170       IChromosome chrom2 = (IChromosome) a_second;
171       if (getConfiguration().getFitnessEvaluator().isFitter(chrom2.
172           getFitnessValue(), chrom1.getFitnessValue())) {
173         return 1;
174       }
175       else if (getConfiguration().getFitnessEvaluator().isFitter(
176           chrom1.getFitnessValue(), chrom2.getFitnessValue())) {
177         return -1;
178       }
179       else {
180         return 0;
181       }
182     }
183   }
184   class ThresholdSelectorConfigurable {
185     /**
186      * This percentage indicates the number of best chromosomes from the
187      * population to be selected for granted. All other chromosomes will
188      * be selected in a random fashion.
189      */

190     public double m_bestChroms_Percentage;
191   }
192 }
193
Popular Tags