Hide Comments
Hide Comments

Comments (0)

Prepares the genetic component for evolution (or resets the genetic population to start over). On calling the Initialization method, the Population is set to the  InitialPopulation size. The  OnInitialization method is called. Then, the Population's DNA are randomized and each individual's fitness is evaluated.

Namespace: RSGeneticBase

expandingSyntax

Delphi

public 
  procedure Initialize; virtual; 
 

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

Reference

OnInitialize

Comments (0)