The TExaminer class is used to calculate the fitness of an individual. The fitness value is returned by the GetFitness function on examination of the Individual passed as a parameter. Fitness is represented as a double precision floating point number where lower values imply higher levels of fitness. This inverse scale is used because most problems that lend themselves to solution by EAs are optimisation problems. Optimisation generally means that we are attempting to minimise some cost value (distance in the travelling salesman problem). The class declaration for the TExaminer component is shown in listing 14.
TExaminer = class(TComponent)
// Returns the fitness of an individual, where lower <=> better
function GetFitness(Individual : TIndividual) : Double; virtual; abstract;
end;
|
listing 14: TExaminer - base class for the examiner
The TTSPExaminer component extends the TExaminer to provide a measure of the fitness of a TSP route. It makes use of the TTSPDisplay component to calculate the total distance travelled. This is then returned as the fitness value for the individual.
The examiner implements one of the most important building blocks of the evolutionary algorithm - the fitness function. The fitness function simulates the environmental pressures imposed on individuals and thus helps to guide the population towards a solution to the problem.
interface
type
TTSPExaminer = class(TExaminer)
private
// The display component we are associated with
FDisplay: TTSPDisplay;
public
// Returns the fitness of an individual as a real number where 0 => best
function GetFitness(Individual : TIndividual) : Double; override;
published
// Property to allow display to be associated at design time
property Display : TTSPDisplay read FDisplay write FDisplay;
end;
implementation
function TTSPExaminer.GetFitness(Individual: TIndividual): Double;
var
i : Integer;
Distance : Double;
Indi : TTSPIndividual;
begin
Indi := TTSPIndividual(Individual);
Distance := 0;
for i := 0 to Indi.Steps - 2 do
begin
Distance := Distance + FDisplay.DistanceBetween(Indi.Route[i],
Indi.Route[i + 1]);
end;
Distance := Distance + FDisplay.DistanceBetween(Indi.Route[Indi.Steps - 1],
Indi.Route[0]);
Result := Distance;
end;
|
listing 14: The TTSPExaminer component uses the distance travelled as a measure of fitness
| Back |
|
|
| Contents | ||
| Next | ||