Hide Comments
Hide Comments

Comments (0)

The Fitness property is a floating point value that specifies the correctness of the individual DNA. Fitness is used by the genetic components to decide which individuals are better than others. Fitness is a score you define for every individual using an    OnEvaluateFitness event handler. It may be any floating point number. However, fitness must have relative meaning among the entire range of assigned fitness numbers, e.g., a fitness of -10 must be worse than a fitness of -3 which must be worse than a fitness of 5, etc (or vice versa, the    FitnessMethod property tells the genetic component whether lower or higher numbers are desirable). The    NormalizedFitness value is the fitness of the individual normalized over the entire population's fitness values so that it is a value between 0 and 1 (where 0 will be the individual with the lowest fitness and 1 will be the individual with the highest fitness).

Using each individual's calculated fitness and the FitnessMethod, the genetic component will seek to either maximize the solution (e.g., keep evolving for individuals whose fitness are greater than other individuals in the population) or to minimize the solution (e.g., find the individuals whose fitness are less than other individuals).
 

Namespace: RSGeneticBase

expandingSyntax

Delphi

public
  property Fitness: TGAFitness read GetFitness;
 

Property Value

Type: TGAFitness

expandingExceptions
expandingExamples

To define your fitness function, you need to define an OnEvaluateFitness event handler:

For the 8-queens problem, we are going to choose a fitness function that calculates the number of non-attacking pairs of queens. A completely solved solution would be 28 non-attacking pairs of queens.

Delphi

procedure TForm1.RSGeneticAlgorithm1EvaluateFitness(Sender: TObject;
   const Item: TRSGAIndividual; var Fitness: TGAFitness);
 var
    i: Integer;
    j: Integer;
    Queen: Integer;
    newQueen: Integer;
    d1, d2: Integer;
 begin
      // fitness function for 8 queens problem is the number of non-attacking
      // pairs of queens (perfect is 28)
      Fitness := 0;
      with Sender as TRSGeneticAlgorithm do
      for i := 0 to Genes.Count - 2 do
      begin
           Queen := Genes[i].AsInteger[Item.Index];
           if Queen >= NUM_QUEENS then Fitness := Fitness - 1;
           d1 := Queen + 1;
           d2 := Queen - 1;
           for j := i + 1 to Genes.Count - 1 do
           begin
                NewQueen := Genes[j].AsInteger[Item.Index];
                if NewQueen >= NUM_QUEENS then Fitness := Fitness - 1;
                if (NewQueen <> Queen) and (NewQueen <> d1) and (NewQueen <> d2) then
                   Fitness := Fitness + 1;
                Inc(d1);
                Dec(d2);
           end;
      end;
 end;

expandingSee Also

Comments (0)