Hide Comments
Hide Comments

Comments (0)

Specifies how the genetic component selects individuals from the current generation to be used as parents of the next generation. The genetic component provides some built-in methods and allows you to write your own by using the  OnSelection event.
 

Namespace: RSGeneticBase

expandingSyntax

Delphi

public
  property SelectionMethod: TGASelectionMethod read FSelectionMethod write SetSelectionMethod default smStochasticTournament;
 

Property Value

Type: TGASelectionMethod

expandingExamples

The following example runs the genetic algorithm component using the different selection methods to solve the N queens problem:

Delphi

procedure TForm1.btnRunClick(Sender: TObject);
 var
   SavePop: TRSGAPopulation;
   Gen: Integer;
 begin
   Screen.Cursor := crHourGlass;
   // save population as is
   SavePop := TRSGAPopulation.Create(nil, RSGeneticAlgorithm1.Population.ItemClass);
   try
      if not RSGeneticAlgorithm1.Initialized then
         RSGeneticAlgorithm1.Initialize;
 
      SavePop.Assign(RSGeneticAlgorithm1.Population);
      Gen := RSGeneticAlgorithm1.Generation;
 
      FCurrentSeries := RouletteSeries;
      RunSelectionMethod( smRoulette, Gen, SavePop );
 
      FCurrentSeries := RandomSeries;
      RunSelectionMethod( smRandom, Gen, SavePop );
 
      FCurrentSeries := TournamentSeries;
      RunSelectionMethod( smTournament, Gen, SavePop );
 
      FCurrentSeries := StochasticTournamentSeries;
      RunSelectionMethod( smStochasticTournament, Gen, SavePop );
 
      FCurrentSeries := ElitistSeries;
      RunSelectionMethod( smElitist, Gen, SavePop );
 
   finally
      Screen.Cursor := crDefault;
      SavePop.Free;
   end;
 end;
 
 procedure TForm1.RunSelectionMethod( SelectionMethod: TGASelectionMethod; CurrentGeneration: Integer; InitialPopulation: TRSGAPopulation );
 var
   i: Integer;
 begin
   RSGeneticAlgorithm1.SelectionMethod := SelectionMethod;
   RSGeneticAlgorithm1.Generation := CurrentGeneration;
   RSGeneticAlgorithm1.Population := InitialPopulation;
   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;

expandingSee Also

Comments (0)