KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > NaturalSelector


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;
11
12 import java.util.*;
13 import org.jgap.data.config.*;
14
15 /**
16  * Abstract base implementation of interface INaturalSelector.
17  *
18  * @author Neil Rotstan
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public abstract class NaturalSelector
23     implements INaturalSelector, Configurable {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.24 $";
26
27   private /*transient*/ Configuration m_config;
28
29   /**
30    *
31    * @param a_config the configuration to use
32    *
33    * @author Klaus Meffert
34    * @since 3.0
35    */

36   public NaturalSelector(Configuration a_config) {
37     m_config = a_config;
38   }
39
40   /**
41    * @return the (immutable) configuration to use
42    *
43    * @author Klaus Meffert
44    * @since 3.0
45    */

46   public Configuration getConfiguration() {
47     return m_config;
48   }
49
50   /**
51    * Add a Chromosome instance to this selector's working pool of Chromosomes.
52    *
53    * @param a_chromosomeToAdd the specimen to add to the pool
54    *
55    * @author Neil Rotstan
56    * @since 1.0
57    */

58   protected abstract void add(IChromosome a_chromosomeToAdd);
59
60   /**
61    * Comparator regarding only the fitness value. Best fitness value will
62    * be on first position of resulting sorted list
63    *
64    * @author Klaus Meffert
65    * @since 1.1
66    */

67   public class FitnessValueComparator
68       implements Comparator, java.io.Serializable JavaDoc {
69     public int compare(Object JavaDoc first, Object JavaDoc second) {
70       IChromosome chrom1 = (IChromosome) first;
71       IChromosome chrom2 = (IChromosome) second;
72       if (getConfiguration().getFitnessEvaluator().isFitter(chrom2,
73           chrom1)) {
74         return 1;
75       }
76       else if (getConfiguration().getFitnessEvaluator().isFitter(
77           chrom1, chrom2)) {
78         return -1;
79       }
80       else {
81         return 0;
82       }
83     }
84   }
85 }
86
Popular Tags