Introduction to Evolutionary Algorithms

6. The Creator

The creator is responsible for the creation and initialisation of new individuals, used in the initial population. The class TCreator defines the basic functionality (listing 4) of creator components. The function "CreateIndividual" returns a new TIndividual object, initialised with appropriate values.

  TCreator = class(TComponent)
    // Function is used to create individuals to form the initial population
    function CreateIndividual : TIndividual; virtual; abstract;
  end;

listing 4: TCreator - the base class for creators

The TTSPCreator descends from the TCreator to create randomly initialised TTSPIndividual objects. The class declaration, and code for the overloaded "CreateIndividual" function are shown in listing 5.

interface

type
  TTSPCreator = class(TCreator)
  private
    // The display component we are associated with
    FDisplay: TTSPDisplay;
  public
    // Function to create a random individual
    function CreateIndividual : TIndividual; override;
  published
    // Property to allow display to be associated at design time
    property Display : TTSPDisplay read FDisplay write FDisplay;
  end;


implementation

function TTSPCreator.CreateIndividual: TIndividual;
var
  New              : TTSPIndividual;
  i, j, Top, Temp  : Integer;
begin
  // Get the number of cities
  Top := FDisplay.CityCount;

  // Create the new individual
  New := TTSPIndividual.Create(Top);

  // Initialise it with a sequential route
  for i := 0 to Top - 1 do
    New.Route[i] := i;

  // Shuffle the route
  for i := Top - 1 downto 1 do
  begin
    j := Random(i);
    Temp := New.Route[j];
    New.Route[j] := New.Route[i];
    New.Route[i] := Temp;
  end;

  // Return the completed (random) individual
  Result := New;
end;

listing 5: TTSPCreator - creator class for the TSP

Back
Contents
Next