Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidTournamentProb = errors.New("crossover probability must be in the [0,1] range")
ErrInvalidTournamentProb is the error returned when trying to set an invalid tournament selection probability
var ErrInvalidTruncRatio = errors.New("truncation selection ratio must be in the (0,1] range")
ErrInvalidTruncRatio is the error returned when trying to set an invalid selection ratio for truncation selection
var Rank = RankBased{ Selector: StochasticUniversalSampling{}, Map: MapRankToScore, }
Rank is a preconfigured all-round rank-based selection strategy. It uses StochasticUniversalSampling as selector and MapRankToScore as mapping function.
var RouletteWheel = rouletteWheel{}
RouletteWheel implements selection of N candidates from a population by selecting N candidates at random where the probability of each candidate getting selected is proportional to its fitness score.
This is analogous to each candidate being assigned an area on a roulette wheel proportionate to its fitness and the wheel being spun N times. Candidates may be selected more than once. In some instances, particularly with small population sizes, the randomness of selection may result in excessively high occurrences of particular candidates. If this is a problem, StochasticUniversalSampling provides an alternative fitness-proportionate strategy for selection.
var SigmaScaling = NewSigmaScaling(StochasticUniversalSampling{})
SigmaScaling is the default sigma scaling selection strategy. It uses StochasticUniversalSampling as its selector.
Functions ¶
func MapRankToScore ¶
MapRankToScore maps a population index to a relative pseudo-fitness score that can be used for fitness-proportionate selection. The general contract for the mapping function is:
f(rank) >= f(rank + 1)
For all legal values of rank, assuming natural scores.
The default mapping function is a simple linear transformation, but this can be overridden by composition. Alternative implementations can be linear or non-linear and either natural or non-natural. rank is a zero-based index into the population (0 <= rank < population size)
Returns size - rank
func NewSigmaScaling ¶
NewSigmaScaling creates a sigma-scaled selection strategy. This is an alternative to straightforward fitness-proportionate selection such as that offered by RouletteWheelSelection and StochasticUniversalSampling. It uses the mean population fitness and fitness standard deviation to adjust individual fitness scores.
selector is the proportionate selector that will be delegated to after fitness scores have been adjusted using sigma scaling.
Early on in an evolutionary algorithm this helps to avoid premature convergence caused by the dominance of one or two relatively fit candidates in a population of mostly unfit individuals. It also helps to amplify minor fitness differences in a more mature population where the rate of improvement has slowed.
Types ¶
type Identity ¶
type Identity struct{}
Identity is a selection strategy that returns identical candidates
type MappingFunc ¶
MappingFunc is the type of functions that maps a population index to a relative pseudo-fitness score that can be used for fitness-proportionate selection. The general contract for the mapping function is:
f(rank) >= f(rank + 1) for all legal values of rank and assuming natural scores.
type RankBased ¶
type RankBased struct {
Selector evolve.Selection
Map MappingFunc
}
RankBased is selection strategy that is similar to fitness-proportionate selection except that is uses relative fitness rather than absolute fitness in order to determine the probability of selection for a given individual (i.e. the actual numerical fitness values are ignored and only the ordering of the sorted population is considered).
RankBased is implemented in terms of a mapping function and delegation to a fitness-proportionate selector. The mapping function converts ranks into relative fitness scores that are used to drive the delegate selector.
func (RankBased) Select ¶
func (rb RankBased) Select( pop evolve.Population, natural bool, size int, rng *rand.Rand) []interface{}
Select selects the specified number of candidates from the population.
- pop must be sorted by descending fitness, i.e the fittest individual of the population should be pop[0]. - natural indicates fitter individuals have fitness scores. - size is the number of individual selections to perform (not necessarily the number of distinct candidates to select, since the same individual may potentially be selected more than once).
Returns the selected candidates.
type StochasticUniversalSampling ¶
type StochasticUniversalSampling struct{}
StochasticUniversalSampling is an alternative to RouletteWheelSelection as a fitness-proportionate selection strategy. Ensures that the frequency of selection for each candidate is consistent with its expected frequency of selection.
func (StochasticUniversalSampling) Select ¶
func (StochasticUniversalSampling) Select( pop evolve.Population, natural bool, size int, rng *rand.Rand) []interface{}
Select selects the specified number of candidates from the population.
Implementations may assume that the population is sorted in descending order according to fitness (so the fittest individual is the first item in the list). NOTE: It is an error to call this method with an empty or nil population.
pop is the population from which to select. natural indicates whether higher fitness values represent fitter individuals or not. size is the number of individual selections to make (not necessarily the number of distinct candidates to select, since the same individual may potentially be selected more than once).
Returns a slice containing the selected candidates. Some individual candidates may potentially have been selected multiple times.
func (StochasticUniversalSampling) String ¶
func (StochasticUniversalSampling) String() string
type Tournament ¶
type Tournament struct {
// contains filtered or unexported fields
}
Tournament is a selection strategy that picks a pair of candidates at random and then selects the fitter of the two candidates with probability p, where p is the configured selection probability (therefore the probability of the less fit candidate being selected is 1 - p).
func NewTournament ¶
func NewTournament() *Tournament
NewTournament creates a TournamentSelection selection strategy where the probability of selecting the fitter of two randomly chosen candidates is set to 0.7.
func (*Tournament) Select ¶
func (ts *Tournament) Select( pop evolve.Population, natural bool, size int, rng *rand.Rand) []interface{}
Select selects the specified number of candidates from the population.
func (*Tournament) SetProb ¶
func (ts *Tournament) SetProb(prob float64) error
SetProb sets a constant probability that fitter of two randomly chosen candidates will be selected.
The probability of selecting the fitter of two candidates must be greater than 0.5 to be useful (if it is not, there is no selection pressure, or the pressure is in favour of weaker candidates, which is counter-productive). If prob is not in the (0.5,1] range SetProb will return ErrInvalidTournamentProb
func (*Tournament) SetProbRange ¶
func (ts *Tournament) SetProbRange(min, max float64) error
SetProbRange sets the range of possible tournament selection probabilities.
The specific probability will be randomly chosen with the pseudo random number generator argument passed to Select, by linearly converting from (0.5,1) to [min,max).
If min and max are not bounded by (0.5,1] SetProbRange will return ErrInvalidTournamentProb.
func (*Tournament) String ¶
func (ts *Tournament) String() string
type Truncation ¶
type Truncation struct {
// contains filtered or unexported fields
}
Truncation implements the selection of n candidates from a population by simply selecting the n candidates with the highest fitness scores (the rest is discarded). The same candidate is never selected more than once.
func NewTruncation ¶
func NewTruncation() *Truncation
NewTruncation creates a TruncationSelection configured with the provided options.
If no options are provided the selection ratio will vary uniformly between 0 and 1.
func (*Truncation) Select ¶
func (ts *Truncation) Select(pop evolve.Population, natural bool, size int, rng *rand.Rand) []interface{}
Select selects the fittest candidates. If the selectionRatio results in fewer selected candidates than required, then these candidates are selected multiple times to make up the shortfall.
pop is the population of evolved and evaluated candidates from which to select. natural indicates whether higher fitness values represent fitter individuals or not. size is the number of candidates to select from the evolved population.
Returns the selected candidates.
func (*Truncation) SetRatio ¶
func (ts *Truncation) SetRatio(ratio float64) error
SetRatio sets a constant selection ratio, that is the proportion of the highest ranked candidates to select from the population.
If ratio is not in the (0,1] range SetRatio will return ErrInvalidTruncRatio
func (*Truncation) SetRatioRange ¶
func (ts *Truncation) SetRatioRange(min, max float64) error
SetRatioRange sets the range of possible truncation selection ratio.
The specific ratio will be randomly chosen with the pseudo random number generator argument of Select, by linearly converting from (0.5,1.0) to [min,max).
If min and max are not bounded by [0,1] SetRatioRange will return ErrInvalidTruncRatio.
func (*Truncation) String ¶
func (ts *Truncation) String() string