KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > impl > FitnessProportionateSelection


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.gp.impl;
11
12 import java.io.*;
13 import java.util.*;
14
15 import org.jgap.gp.*;
16
17 /**
18  * Selects individuals proportionally according to their adjusted fitness.
19  *
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class FitnessProportionateSelection
24     implements INaturalGPSelector, Serializable {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
27
28   public IGPProgram select(GPGenotype a_genotype) {
29     /**@todo speedup*/
30     double chosen = a_genotype.getGPConfiguration().getRandomGenerator().
31         nextFloat() * a_genotype.getTotalFitness();
32     int num = 0;
33     GPPopulation pop = a_genotype.getGPPopulation();
34     int popSize = pop.size();
35     num = Arrays.binarySearch(pop.getFitnessRanks(), (float) chosen);
36     if (num >= 0) {
37       return pop.getGPProgram(num);
38     }
39     else {
40       for (num = 1; num < popSize; num++) {
41         if (chosen < pop.getFitnessRank(num)) {
42           break;
43         }
44       }
45       num--;
46       if (num >= popSize - 1) {
47         if (popSize - 1 < 1) {
48           num = 1;
49         }
50         else {
51           num = a_genotype.getGPConfiguration().getRandomGenerator().
52               nextInt(popSize - 1);
53         }
54       }
55       return pop.getGPProgram(num);
56     }
57   }
58 }
59
Popular Tags