Genetic Algorithm

The Traveling Salesman Problem

by Nikolai Shokhirev


This is an interface-based implementation of Dan Taylor's  Evolutionary TSP Algorithm demo program.  At that website you can also find his article "Introduction to Evolutionary Algorithms" , PowerPoint presentation and the source code for two examples.

Our implementation of this classical optimization problem can be downloaded here

Below are some comments to the code.

List of units

unit fEA_TSP;  { main form }
unit uIEA;     { Generic interfaces }
unit uITSP;    { TSP-specific Interfaces }
unit uEA;      { Implementation of all interfaces }
unit uUtilsEA; { type definitions }

General interfaces

General-purpose interfaces - nothing TSP-specific:

  IIndividual = interface
    procedure SetFitness(const Value: TFloat);
    function GetFitness: TFloat;
    property Fitness: TFloat read GetFitness write SetFitness;
// Here and later "Getters" and "Setters" for properties are omitted (see the complete code)

  TInterfaceCompare = function (Item1, Item2: IIndividual): Integer of object;

  ICreator = interface
    // Function is used to create individuals to form the initial population
    function CreateIndividual: IIndividual;

  IKiller = interface
    // Function to kill off some unfit population members
    // should delete them from the list and return the number killed
    function Kill(Pop: IPopulation) : Integer;

  IKillerPercentage = interface(IKiller)
    // Percentage of population to be killed
    property Percentage : TFloat read GetPercentage write SetPercentage;

  IParentSelector = interface
    // This function returns a reference to a single parent, selected from Pop
    function SelectParent(Population: IPopulation): IIndividual;

  IBreeder = interface
    // This function should return a new population member based on parents
    // selected using the ParentSelector object
    function BreedOffspring(PSelector: IParentSelector; Pop: IPopulation): IIndividual;

  IMutator = interface
    // This function performs mutation(s) on the individual passed
    procedure Mutate(Individual: IIndividual);

  IExaminer = interface
    // Returns the fitness of an individual, where lower <=> better
    function GetFitness(Individual: IIndividual) : TFloat;

  IPopulation = interface
    procedure Add(New: IIndividual); // Adds an individual to the population
    procedure Delete(I: Integer); // Deletes an individual from the population
    procedure Generation; // Runs a single generation
    procedure Initialise(Size: Integer); // Initialise the population
    procedure Clear; // Clear ourselves out
    function FitnessOf(I: Integer): TFloat; // Get the fitness of an individual
    // Access to the population members
    property Pop[I: Integer]: IIndividual read GetIndividual; default;
    property Count: Integer read GetCount; // The size of the population
    property ParentSelector: IParentSelector read GetParentSelector write SetParentSelector;
    property Breeder : IBreeder read GetBreeder write SetBreeder;
    property Killer : IKiller read GetKiller write SetKiller;
    property Mutator : IMutator read GetMutator write SetMutator;
    property Creator : ICreator read GetCreator write SetCreator;
    property Examiner : IExaminer read GetExaminer write SetExaminer;
    property OnChange : TNotifyEvent read GetOnChange write SetOnChange;

TSP-specific interfaces

ITSPIndividual = interface(IIndividual)
    property RouteArray[I: Integer]: Integer read GetRouteArray write SetRouteArray;
    property Steps: Integer read GetSteps write SetSteps;

// interface for communication with Display object (see below)
  ITSPController = interface
    {  Get the distance between two cities }
    function DistanceBetween(C1, C2 : Integer) : TFloat;
    procedure RandomCities; {  Places the cities at random points }
    { Area limits }
    property Xmin: TFloat read GetXmin write SetXmin;
    property Xmax: TFloat read GetXmax write SetXmax;
    property Ymin: TFloat read GetYmin write SetYmin;
    property Ymax: TFloat read GetYmax write SetYmax;
    {  Access to the cities array }
    property Cities[I: Integer] : TPoint2D read GetCity;
    {  Properties... }
    property CityCount: Integer read GetCityCount write SetCityCount;

  ITSPDisplay = interface
    procedure DrawMap; {  Call this to draw the map }
    {  Draw the map with a route  }
    procedure DrawMapWithRoute(Individual : ITSPIndividual);

  ITSPCreator = interface(ICreator)
    function CreateIndividual : IIndividual;
    property Controller : ITSPController read GetController write SetController;

  ITSPMutator = interface(IMutator)
    // Probability of doing a transposition
    property Transposition : TFloat read GetTrans write SetTrans;
    // Probability of doing an inversion
    property Inversion : TFloat read GetInv write SetInv;

  ITSPExaminer = interface(IExaminer)
    // Returns the fitness of an individual as a real number where 0 => best
    property Controller : ITSPController read GetController write SetController;

Display object

This object is used instead of TChart components in Dan Taylor's original implementation. It is created dynamically at run-time and should not be installed. 

  TTSPDisplay = class(TPainter0)
    fController: ITSPController;
    {  Clear the map }
//    procedure Clear;  //it exists in the base class
    procedure DrawCities; { Show the cities }
    procedure DrawRoute(Individual: ITSPIndividual); {  Display a route  }
    constructor Create(aOwner: TComponent; aParent: TWinControl);
    destructor Destroy; override;
    procedure DrawMap; { Call this to draw the map }
    {  Draw the map with a route  }
    procedure DrawMapWithRoute(Individual: ITSPIndividual);
    property Controller: ITSPController read GetController write SetController;


©Nikolai Shokhirev, 2002 - 2016.


Home  |  |   Other Projects  |   Programming  |   Publications  |   Resume

Please e-mail me at