The parent selector is used to select a single parent for use in breeding. The TParentSelector class defines a basic interface - see listing 8. The "SelectParent" function returns a reference to the individual in the population which has been selected to be the parent.
TParentSelector = class(TComponent)
// This function returns a reference to a single parent, selected from Pop
function SelectParent(Population : TPopulation) : TIndividual; virtual; abstract;
end;
|
listing 8: TParentSelector - base class selecting a parent for breeding
The TParentSelectorTournament class implements a simple parent selection scheme...
This parent selection scheme is close to what happens in many natural species where breeding partners are chosen based on displays of strength or healthiness. Code for the TParentSelectorTournament class and the "SelectParent" function is shown in listing 9.
interface
type
TTSPParentSelectorTournament = class(TTSPParentSelector)
public
function SelectParent(Population : TPopulation) : TIndividual; override;
end;
implementation
function TTSPParentSelectorTournament.SelectParent(Population: TPopulation): TIndividual;
var
i1, i2 : Integer;
begin
// Select a random individual
i1 := Random(Population.Count);
// Select a *different* random individual
repeat
i2 := Random(Population.Count);
until i1 <> i2;
// Hold the tournament and return the fittest of the two
if Population.FitnessOf(i1) < Population.FitnessOf(i2) then
Result := Population[i1]
else
Result := Population[i2]
end;
|
listing 9: TParentSelectorTournament - selects a parent based on a tournament between two individuals
| Back |
|
|
| Contents | ||
| Next | ||