/* Copyright 2006 by Sean Luke Licensed under the Academic Free License version 3.0 See the file "LICENSE" for more information */ package ec.select; import ec.util.*; import ec.*; /* * BestSelection.java * * Created: Thu Feb 10 18:52:09 2000 * By: Sean Luke */ /** * Picks among the best n individuals in a population in * direct proportion to their absolute * fitnesses as returned by their fitness() methods relative to the * fitnesses of the other "best" individuals in that n. This is expensive to * set up and bring down, so it's not appropriate for steady-state evolution. * If you're not familiar with the relative advantages of * selection methods and just want a good one, * use TournamentSelection instead. Not appropriate for * multiobjective fitnesses. * *

* Note: Fitnesses must be non-negative. 0 is assumed to be the worst fitness. * *

Typical Number of Individuals Produced Per produce(...) call
Always 1.

Parameters
base.pick-worst
bool = true or false (default)
(should we pick from among the worst n individuals in the tournament instead of the best n?)
base.n
int > 0 (default is 1)
(the number of best-individuals to select from)

Default Base
select.best * @author Sean Luke * @version 1.0 */ public class BestSelection extends SelectionMethod { /** Default base */ public static final String P_BEST = "best"; public static final String P_N = "n"; public static final String P_PICKWORST = "pick-worst"; /** Sorted, normalized, totalized fitnesses for the population */ public float[] sortedFit; /** Sorted population -- since I *have* to use an int-sized individual (short gives me only 16K), I might as well just have pointers to the population itself. :-( */ public int[] sortedPop; /** Do we pick the worst instead of the best? */ public boolean pickWorst; public int bestn; public Parameter defaultBase() { return SelectDefaults.base().push(P_BEST); } // don't need clone etc. public void setup(final EvolutionState state, final Parameter base) { super.setup(state,base); Parameter def = defaultBase(); bestn = state.parameters.getInt(base.push(P_N),def.push(P_N),1); if (bestn == 0 ) state.output.fatal("n must be an integer greater than 0", base.push(P_N),def.push(P_N)); pickWorst = state.parameters.getBoolean(base.push(P_PICKWORST),def.push(P_PICKWORST),false); } public void prepareToProduce(final EvolutionState s, final int subpopulation, final int thread) { // load sortedPop integers final Individual[] i = s.population.subpops[subpopulation].individuals; sortedPop = new int[i.length]; for(int x=0;x