Final Year Project

  • > Comparison and Optimisation of Natural Computation Techniques to Solve the Travelling Salesman Problem
  • > Created using WPF for the UI elements

Learn More

View On Github

Project Overview

For my final year project I decided to do further research into the travelling salesman problem and how natural computation could be used to solve the problem. Due to the time constraints of the project I was able to implement a Genetic Algorithm and the Ant Colony Optimisation Algorithm, using these algorithms I was able to generate results and compare them against each other. After presenting the results to my lecturers within a viva presentation one of the lecturers with background knowledge of the algorithms said that my results are exactly as they expected.

Features

  • > Functioning UI
  • > Entering Graphs directly into application
  • > Importing Graphs
  • > Exporting Graphs
  • > Visual Feedback for graphs and solutions
  • > Tracking Algorithms Progress

UI

The UI of the project is made completely using WPF, I chose WPF as I have experience with it and it is a simple solution to making an effective UI. As the projects outcome is to compare results the UI is an added benefit, and is very helpful when debugging. It can be argued that the application could just be a console application returning results however I wanted to create a more interactive application with visual feedback.

The benefit of WPF is I can easily drag and drop a element onto the application, give it a name and the appropriate data it needs, then use it within the code either giving a button a click event or taking the data from a text box.

Genetic Algorithm

To apply a genetic algorithm to the travelling salesman problem there are a lot of modifications that need to be made. To begin with we need to set up how the structure of the algorithm would be, which parts of the TSP would be a chromosome for example. I have created a diagram to show this.

If a gene is a valid graph node, then a chromosome can be a valid solution as the order of the genes is the order the salesman will travel.


The main problem when using a genetic algorithm is how reproduction and mutation will work, this is because a valid solution can only travel to each node once and during typical genetic algorithm reproduction we may travel to some nodes many times and ignore other nodes.

To solve this I utilise Partially Mapped Crossover Operation, this crossover method allows the chromosomes to produce child solutions which are valid solutions where each node is visited only once. The diagram to the left shows the steps.

  1. > Step 1: Parents are Selected*
  2. > Step 2: Offspring take parent 1s middle section
  3. > Step 3: Implement parent 2s points which are not duplicates
  4. > Step 4: Fill in missing genes with remaining nodes
*The selection method used is ranked selection as this promotes stronger chromosomes to be selected within reproduction.


Mutation

For mutation we need to create methods which do not create solutions which are not valid, therefore I created 2 methods:

> Swap Mutation - 2 genes are picked at random and their positions are "swapped".

> Reverse Mutation - 2 Points are selected within the chromosome and the genes between these 2 points are reversed or "flipped".

Ant Colony Optimisation

Ants

There are a few key differences with Ant Colony Optimisation algorithms and the way it has been implemented within my project these are as follows:

> Artificial Ants - The ants within my project have memory and need to have memory as we do not want them to travel to nodes they have already travelled too. They are also more aware of their surroundings, meaning they know the distance to the next nodes.

> No "main" Nest - Meaning when the ants are created within the algorithm they are scattered between the nodes, meaning each node should have a equal amount of ants at each of them for when the algorithm starts

public void Initialise(List nodes)
{
	originalMap = new List<.TSPGraphNode>(nodes);
	paths.Clear();
	antColony.Clear();
	bestAntPerIteration.Clear();
	lastBestDistance = double.MaxValue;
	iterationsSinceBestChange = 0;
	totalIterations = 0;

	for(int i = 0; i < nodes.Count; i++)
	{
		
		for(int j = 0; j < antPerNode; j++)
		{
			Ant ant = new Ant(this);
			ant.currentTour.Add(nodes[i]);
			antColony.Add(ant);
		}
	}
}
							
public void AddPheromone()
{
	double totalDistance = GetTotalDistance();

	Path pathToDecay = null;
	for (int i = 0; i < currentTour.Count - 1; i++)
	{

		pathToDecay = manager.GetPath(currentTour[i], currentTour[i + 1]);

		pathToDecay.pheromoneLevel += manager.pheromoneConstant / totalDistance;
	}

	pathToDecay = manager.GetPath(currentTour.Last(), currentTour.First());
	pathToDecay.pheromoneLevel += manager.pheromoneConstant / totalDistance;
}
							

Pheromones

The way that ants communicate to each other is through the environment by leaving pheromones, within this application the ants drop a level of pheromones on a "Path" where a path is two nodes. These paths are then analysed when the ant comes to choose which node to travel to, the more pheromones on a path the more influence a ant feels to take that path.

To incorporate pheromone decay I utilised a constant value to multiply the pheromone level by, also as the application is running per iteration we have no "Time" therefore we need to add pheromones dependant on the distance the ant has travelled as opposed to the time it took.

Results

To generate results I utilised the importing feature to allow for the same graph to be tested against the different algorithms. The results can be seen in the table to the left and the graphs below.


Optimisation

Due to the time constraints of the project I only attempted to optimise one of the programs, I did 2 attempts of optimising the genetic algorithm.

First optimisation I tried implementing a dynamic population size for the genetic algorithm however this lead to inaccurate final results and was deemed a failure.

The second optimisation I tried was to add elitism to the algorithm, where the best chromosomes of the previous population are placed into the next population, this ultimately lowered the time it took to get to a final result as adding elitism is encouraging a much stronger population.

I am happy to go into further detail for this or any projects on my portfolio if needs be. I am also happy to distribute the report/write-up for the project if needs be.

View On Github