Hide Comments
Hide Comments

Comments (0)

The Evolve method is the heart of the genetic component. The Evolve method is responsible for "breeding" the    Population towards an answer for your goal. Each generation, the Evolve method selects parents and then    Reproduces children for the new generation by using the genetic operations: first crossover, then mutation, and finally inversion. You can specify the number of generations to evolve or use the    FitnessCutoff,    GenerationLimit and    DiversityLimit properties to automatically stop the genetic process. Successive calls to the Evolve method continues the evolutionary process from where the Evolve method stopped (use    Initialize to reset the process). If this is your first call to Evolve, it automatically calls the Initialize method.

Specify the    Operations to control which operations are performed while evolving: crossover, mutation, and inversion operations. You can also specify the chances of each operation occurring every generation using the    CrossoverProbability,    MutationProbability, and    InversionProbability properties.

The    SelectionMethod property (or    OnSelection event) specifies how the genetic component selects individuals from the current generation to be used as parents of the next generation.

The FitnessCutoff property aborts the evolutionary process when any child meets or exceeds (if FitnessMethod is fmMaximize) or is less than (if FitnessMethod is fmMinimize) the cutoff value. The    UseFitnessCutoff property must be true to use the fitness cutoff.

The DiversityLimit property aborts the evolutionary process when the diversity of the children falls below the DiversityLimit, e.g., all the children are too alike to go any further. The    UseDiversityLimit property must be true to use the diversity limit.

The GenerationLimit property aborts the evolutionary process when the required number of generations have been evolved. The    UseGenerationLimit property must be true to use the generation limit.
 
 
 

Namespace: RSGeneticBase

expandingSyntax

Delphi

public 
  function Evolve( Generations: Integer ): Integer; overload; 
 

Parameters

Generations

Type: Integer

Return Value

Type: Integer

expandingExamples

The following code uses the Evolve method to evolve a solution:

Delphi

var
   i: Integer;
 begin
   RSGeneticAlgorithm1.SelectionMethod := SelectionMethod;
   RSGeneticAlgorithm1.Generation := CurrentGeneration;
   RSGeneticAlgorithm1.OnEvaluateFitness := RSGeneticAlgorithm1EvaluateFitness;
 
   RSGeneticAlgorithm1.Evolve(StrToInt(Edit1.Text));
 
   // print statistics
   with RSGeneticAlgorithm1 do
   begin
     Memo1.Lines.Add('');
     Memo1.Lines.Add('Selection Method: '+GetEnumName(TypeInfo(TGASelectionMethod), Ord(RSGeneticAlgorithm1.SelectionMethod)));
     Memo1.Lines.Add('Number of generations:' + IntToStr(Generation));
     Memo1.Lines.Add('Fittest Individual:' + FittestIndividual.Bits.ToString);
     Memo1.Lines.Add('     Score:' + FloatToStr(RSGeneticAlgorithm1.FittestIndividual.Fitness));
     Memo1.Lines.Add('Avg Fitness:' + FloatToStr(AvgFitness));
     Memo1.Lines.Add('Min Fitness:' + FloatToStr(MinFitness));
     Memo1.Lines.Add('Max Fitness:' + FloatToStr(MaxFitness));
     if MaxFitness = FitnessCutoff then
         for i := 0 to RSGeneticAlgorithm1.Genes.Count - 1 do
         begin
           sgChessBoard.Cols[i].Clear;
           sgChessBoard.Cells[i, RSGeneticAlgorithm1.Genes[i].AsInteger[RSGeneticAlgorithm1.FittestIndividual.Index]] := 'Q';
         end;
   end;
 end;
 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)