1 10 package org.jgap.gp.impl; 11 12 import java.io.*; 13 import org.jgap.*; 14 import org.jgap.gp.*; 15 16 23 public class TournamentSelector 24 implements INaturalGPSelector, Serializable { 25 26 private final static String CVS_REVISION = "$Revision: 1.1 $"; 27 28 private int tournament_size; 29 30 35 public TournamentSelector() { 36 this(5); 37 } 38 39 46 public TournamentSelector(int a_tournament_size) { 47 tournament_size = a_tournament_size; 48 } 49 50 60 public IGPProgram select(GPGenotype a_genotype) { 61 GPPopulation pop = a_genotype.getGPPopulation(); 62 IGPProgram bestProgram = null; 63 int index = 0; 64 RandomGenerator random = a_genotype.getGPConfiguration().getRandomGenerator(); 65 IGPFitnessEvaluator evaluator = a_genotype.getGPConfiguration(). 66 getGPFitnessEvaluator(); 67 for (int i = 0; i < tournament_size; i++) { 68 index = (int) (random.nextDouble() * pop.getPopSize()); 69 if (bestProgram == null) { 70 bestProgram = pop.getGPProgram(index); 71 } 72 else { 73 if (evaluator.isFitter(pop.getGPProgram(index), bestProgram)) { 74 bestProgram = pop.getGPProgram(index); 75 } 76 } 77 } 78 return bestProgram; 79 } 80 } 81 | Popular Tags |