1 10 package org.jgap.gp.impl; 11 12 import java.io.*; 13 import java.util.*; 14 15 import org.jgap.gp.*; 16 17 23 public class FitnessProportionateSelection 24 implements INaturalGPSelector, Serializable { 25 26 private final static String CVS_REVISION = "$Revision: 1.4 $"; 27 28 public IGPProgram select(GPGenotype a_genotype) { 29 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 |