/*
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
*/
/**
* Performs a tournament selection restricted to only the best, or worst, n
* indivdiuals in the population. If the best individuals, then tournament selection
* will prefer the better among them; if the worst individuals, then tournament selection
* will prefer the worse among them. The procedure for performing restriction is expensive to
* set up and bring down, so it's not appropriate for steady-state evolution. Like
* TournamentSelection, the size of the tournament can be any
* 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.
*
* The tournament size can be any floating point value >= 1.0. If it is a non-
* integer value x then either a tournament of size ceil(x) is used
* (with probability x - floor(x)), else a tournament of size floor(x) is used.
*
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.size
double >= 1
(the tournament size)
base.n
int > 0
(the number of best-individuals to select from)
base.n-fraction
0.0 <= double < 1.0 (default is 1)
(the number of best-individuals to select from, as a fraction of the total population)
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_N_FRACTION = "n-fraction";
public static final String P_PICKWORST = "pick-worst";
public static final String P_SIZE = "size";
/** Base size of the tournament; this may change. */
public int size;
/** Probablity of picking the size plus one. */
public double probabilityOfPickingSizePlusOne;
/** Do we pick the worst instead of the best? */
public boolean pickWorst;
/** Sorted, normalized, totalized fitnesses for the population */
public int[] sortedPop;
public static final int NOT_SET = -1;
public int bestn = NOT_SET;
public double bestnFrac = NOT_SET;
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();
if ( state.parameters.exists(base.push(P_N),def.push(P_N)) )
{
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));
}
else if (state.parameters.exists(base.push(P_N_FRACTION),def.push(P_N_FRACTION)) )
{
if (state.parameters.exists(base.push(P_N),def.push(P_N)) )
state.output.fatal("Both n and n-fraction specified for BestSelection.", base.push(P_N),def.push(P_N));
bestnFrac =
state.parameters.getDoubleWithMax(base.push(P_N_FRACTION),def.push(P_N_FRACTION),0.0,1.0);
if (bestnFrac <= 0.0)
state.output.fatal("n-fraction must be a double floating-point value greater than 0.0 and <= 1.0", base.push(P_N_FRACTION),def.push(P_N_FRACTION));
}
else state.output.fatal("Either n or n-fraction must be defined for BestSelection.", base.push(P_N),def.push(P_N));
pickWorst = state.parameters.getBoolean(base.push(P_PICKWORST),def.push(P_PICKWORST),false);
double val = state.parameters.getDouble(base.push(P_SIZE),def.push(P_SIZE),1.0);
if (val < 1.0)
state.output.fatal("Tournament size must be >= 1.",base.push(P_SIZE),def.push(P_SIZE));
else if (val == (int) val) // easy, it's just an integer
{
size = (int) val;
probabilityOfPickingSizePlusOne = 0.0;
}
else
{
size = (int) Math.floor(val);
probabilityOfPickingSizePlusOne = val - size; // for example, if we have 5.4, then the probability of picking *6* is 0.4
}
}
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
Ads help maintain this website.