20th August 2012 · By Lee Jacobson

Finding a solution

Parents

Offspring

Creating the Genetic Algorithm

City.java





package tsp;



public class City {

int x;

int y;





public City(){

this .x = ( int )(Math.random()*200);

this .y = ( int )(Math.random()*200);

}





public City( int x, int y){

this .x = x;

this .y = y;

}





public int getX(){

return this .x;

}





public int getY(){

return this .y;

}





public double distanceTo(City city){

int xDistance = Math.abs(getX() - city.getX());

int yDistance = Math.abs(getY() - city.getY());

double distance = Math.sqrt( (xDistance*xDistance) + (yDistance*yDistance) );



return distance;

}



@Override

public String toString(){

return getX()+ ", " +getY();

}

}

TourManager.java





package tsp;



import java.util.ArrayList;



public class TourManager {





private static ArrayList destinationCities = new ArrayList<City>();





public static void addCity(City city) {

destinationCities.add(city);

}





public static City getCity( int index){

return (City)destinationCities.get(index);

}





public static int numberOfCities(){

return destinationCities.size();

}

}

Tour.java





package tsp;



import java.util.ArrayList;

import java.util.Collections;



public class Tour{





private ArrayList tour = new ArrayList<City>();



private double fitness = 0;

private int distance = 0;





public Tour(){

for ( int i = 0; i < TourManager.numberOfCities(); i++) {

tour.add(null);

}

}



public Tour(ArrayList tour){

this .tour = tour;

}





public void generateIndividual() {



for ( int cityIndex = 0; cityIndex < TourManager.numberOfCities(); cityIndex++) {

setCity(cityIndex, TourManager.getCity(cityIndex));

}



Collections.shuffle(tour);

}





public City getCity( int tourPosition) {

return (City)tour.get(tourPosition);

}





public void setCity( int tourPosition, City city) {

tour.set(tourPosition, city);



fitness = 0;

distance = 0;

}





public double getFitness() {

if (fitness == 0) {

fitness = 1/( double )getDistance();

}

return fitness;

}





public int getDistance(){

if (distance == 0) {

int tourDistance = 0;



for ( int cityIndex=0; cityIndex < tourSize(); cityIndex++) {



City fromCity = getCity(cityIndex);



City destinationCity;





if (cityIndex+1 < tourSize()){

destinationCity = getCity(cityIndex+1);

}

else {

destinationCity = getCity(0);

}



tourDistance += fromCity.distanceTo(destinationCity);

}

distance = tourDistance;

}

return distance;

}





public int tourSize() {

return tour.size();

}





public boolean containsCity(City city){

return tour.contains(city);

}



@Override

public String toString() {

String geneString = "|" ;

for ( int i = 0; i < tourSize(); i++) {

geneString += getCity(i)+ "|" ;

}

return geneString;

}

}

Population.java





package tsp;



public class Population {





Tour[] tours;





public Population( int populationSize, boolean initialise) {

tours = new Tour[populationSize];



if (initialise) {



for ( int i = 0; i < populationSize(); i++) {

Tour newTour = new Tour();

newTour.generateIndividual();

saveTour(i, newTour);

}

}

}





public void saveTour( int index, Tour tour) {

tours[index] = tour;

}





public Tour getTour( int index) {

return tours[index];

}





public Tour getFittest() {

Tour fittest = tours[0];



for ( int i = 1; i < populationSize(); i++) {

if (fittest.getFitness() <= getTour(i).getFitness()) {

fittest = getTour(i);

}

}

return fittest;

}





public int populationSize() {

return tours.length;

}

}

GA.java





package tsp;



public class GA {





private static final double mutationRate = 0.015;

private static final int tournamentSize = 5;

private static final boolean elitism = true ;





public static Population evolvePopulation(Population pop) {

Population newPopulation = new Population(pop.populationSize(), false );





int elitismOffset = 0;

if (elitism) {

newPopulation.saveTour(0, pop.getFittest());

elitismOffset = 1;

}









for ( int i = elitismOffset; i < newPopulation.populationSize(); i++) {



Tour parent1 = tournamentSelection(pop);

Tour parent2 = tournamentSelection(pop);



Tour child = crossover(parent1, parent2);



newPopulation.saveTour(i, child);

}





for ( int i = elitismOffset; i < newPopulation.populationSize(); i++) {

mutate(newPopulation.getTour(i));

}



return newPopulation;

}





public static Tour crossover(Tour parent1, Tour parent2) {



Tour child = new Tour();





int startPos = ( int ) (Math.random() * parent1.tourSize());

int endPos = ( int ) (Math.random() * parent1.tourSize());





for ( int i = 0; i < child.tourSize(); i++) {



if (startPos < endPos && i > startPos && i < endPos) {

child.setCity(i, parent1.getCity(i));

}

else if (startPos > endPos) {

if (!(i < startPos && i > endPos)) {

child.setCity(i, parent1.getCity(i));

}

}

}





for ( int i = 0; i < parent2.tourSize(); i++) {



if (!child.containsCity(parent2.getCity(i))) {



for ( int ii = 0; ii < child.tourSize(); ii++) {



if (child.getCity(ii) == null) {

child.setCity(ii, parent2.getCity(i));

break;

}

}

}

}

return child;

}





private static void mutate(Tour tour) {



for ( int tourPos1=0; tourPos1 < tour.tourSize(); tourPos1++){



if (Math.random() < mutationRate){



int tourPos2 = ( int ) (tour.tourSize() * Math.random());





City city1 = tour.getCity(tourPos1);

City city2 = tour.getCity(tourPos2);





tour.setCity(tourPos2, city1);

tour.setCity(tourPos1, city2);

}

}

}





private static Tour tournamentSelection(Population pop) {



Population tournament = new Population(tournamentSize, false );





for ( int i = 0; i < tournamentSize; i++) {

int randomId = ( int ) (Math.random() * pop.populationSize());

tournament.saveTour(i, pop.getTour(randomId));

}



Tour fittest = tournament.getFittest();

return fittest;

}

}

TSP_GA.java





package tsp;



public class TSP_GA {



public static void main(String[] args) {





City city = new City(60, 200);

TourManager.addCity(city);

City city2 = new City(180, 200);

TourManager.addCity(city2);

City city3 = new City(80, 180);

TourManager.addCity(city3);

City city4 = new City(140, 180);

TourManager.addCity(city4);

City city5 = new City(20, 160);

TourManager.addCity(city5);

City city6 = new City(100, 160);

TourManager.addCity(city6);

City city7 = new City(200, 160);

TourManager.addCity(city7);

City city8 = new City(140, 140);

TourManager.addCity(city8);

City city9 = new City(40, 120);

TourManager.addCity(city9);

City city10 = new City(100, 120);

TourManager.addCity(city10);

City city11 = new City(180, 100);

TourManager.addCity(city11);

City city12 = new City(60, 80);

TourManager.addCity(city12);

City city13 = new City(120, 80);

TourManager.addCity(city13);

City city14 = new City(180, 60);

TourManager.addCity(city14);

City city15 = new City(20, 40);

TourManager.addCity(city15);

City city16 = new City(100, 40);

TourManager.addCity(city16);

City city17 = new City(200, 40);

TourManager.addCity(city17);

City city18 = new City(20, 20);

TourManager.addCity(city18);

City city19 = new City(60, 20);

TourManager.addCity(city19);

City city20 = new City(160, 20);

TourManager.addCity(city20);





Population pop = new Population(50, true );

System.out.println( "Initial distance: " + pop.getFittest().getDistance());





pop = GA.evolvePopulation(pop);

for ( int i = 0; i < 100; i++) {

pop = GA.evolvePopulation(pop);

}





System.out.println( "Finished" );

System.out.println( "Final distance: " + pop.getFittest().getDistance());

System.out.println( "Solution:" );

System.out.println(pop.getFittest());

}

}

Output:

Initial distance: 1996

Finished

Final distance: 940

Solution:

|60, 200|20, 160|40, 120|60, 80|20, 40|20, 20|60, 20|100, 40|160, 20|200, 40|180, 60|120, 80|140, 140|180, 100|200, 160|180, 200|140, 180|100, 120|100, 160|80, 180|



Final Results:

Author



I'm a developer from the UK who loves technology and business. Here you'll find articles and tutorials about things that interest me. If you want to hire me or know more about me head over to my

Hello, I'm Lee.I'm a developer from the UK who loves technology and business. Here you'll find articles and tutorials about things that interest me. If you want to hire me or know more about me head over to my about me page

Social Links

To understand what the traveling salesman problem (TSP) is, and why it's so problematic, let's briefly go over a classic example of the problem.Imagine you're a salesman and you've been given a map like the one opposite. On it you see that the map contains a total of 20 locations and you're told it's your job to visit each of these locations to make a sell.Before you set off on your journey you'll probably want to plan a route so you can minimize your travel time. Fortunately, humans are pretty good at this, we can easily work out a reasonably good route without needing to do much more than glance at the map. However, when we've found a route that we believe is optimal, how can we test if it's really the optimal route?Well, in short, we can't - at least not practically.To understand why it's so difficult to prove the optimal route let's consider a similar map with just 3 locations instead of the original 20. To find a single route, we first have to choose a starting location from the three possible locations on the map. Next, we'd have a choice of 2 cities for the second location, then finally there is just 1 city left to pick to complete our route. This would mean there are 3 x 2 x 1 different routes to pick in total.That means, for this example, there are only 6 different routes to pick from. So for this case of just 3 locations it's reasonably trivial to calculate each of those 6 routes and find the shortest. If you're good at maths you may have already realized what the problem is here. The number of possible routes is a factorial of the number of locations to visit, and trouble with factorials is that they grow in size remarkably quick!For example, the factorial of 10 is 3628800, but the factorial of 20 is a gigantic, 2432902008176640000.So going back to our original problem, if we want to find the shortest route for our map of 20 locations we would have to evaluate 2432902008176640000 different routes! Even with modern computing power this is terribly impractical, and for even bigger problems, it's close to impossible.Although it may not be practical to find the best solution for a problem like ours, we do have algorithms that let us discover close to optimum solutions such as the nearest neighbor algorithm and swarm optimization. These algorithms are capable of finding a 'good-enough' solution to the travelling salesman problem surprisingly quickly. In this tutorial however, we will be using genetic algorithms as our optimization technique.If you're not already familiar with genetic algorithms and like to know how they work, then please have a look at the introductory tutorial below:Finding a solution to the travelling salesman problem requires we set up a genetic algorithm in a specialized way. For instance, a valid solution would need to represent a route where every location is included at least once and only once. If a route contain a single location more than once, or missed a location out completely it wouldn't be valid and we would be valuable computation time calculating it's distance.To ensure the genetic algorithm does indeed meet this requirement special types of mutation and crossover methods are needed.Firstly, the mutation method should only be capable of shuffling the route, it shouldn't ever add or remove a location from the route, otherwise it would risk creating an invalid solution. One type of mutation method we could use is swap mutation.With swap mutation two location in the route are selected at random then their positions are simply swapped. For example, if we apply swap mutation to the following list, [1,2,3,4,5] we might end up with, [1,2,5,4,3]. Here, positions 3 and 5 were switched creating a new list with exactly the same values, just a different order. Because swap mutation is only swapping pre-existing values, it will never create a list which has missing or duplicate values when compared to the original, and that's exactly what we want for the traveling salesman problem.Now we've dealt with the mutation method we need to pick a crossover method which can enforce the same constraint.One crossover method that's able to produce a valid route is ordered crossover. In this crossover method we select a subset from the first parent, and then add that subset to the offspring. Any missing values are then adding to the offspring from the second parent in order that they are found.To make this explanation a little clearer consider the following example:Here a subset of the route is taken from the first parent (6,7,8) and added to the offspring's route. Next, the missing route locations are adding in order from the second parent. The first location in the second parent's route is 9 which isn't in the offspring's route so it's added in the first available position. The next position in the parents route is 8 which is in the offspring's route so it's skipped. This process continues until the offspring has no remaining empty values. If implemented correctly the end result should be a route which contains all of the positions it's parents did with no positions missing or duplicated.In literature of the traveling salesman problem since locations are typically refereed to as cities, and routes are refereed to as tours, we will adopt the standard naming conventions in our code.To start, let's create a class that can encode the cities.Now we can create a class that holds all of our destination cities for our tourNext we need a class that can encode our routes, these are generally referred to as tours so we'll stick to the convention.We also need to create a class that can hold a population of candidate toursNext, the let's create a GA class which will handle the working of the genetic algorithm and evolve our population of solutions.Now we can create our main method, add our cities and evolve a route for our travelling salesman problem.As you can see in just 100 generations we were able to find a route just over twice as good as our original and probably pretty close to optimum.If you liked this tutorial you might also enjoy, Simulated Annealing for beginners