using System.Collections.Generic;
public static Random rand=new Random();
public Point (int id, int x, int y)
public double DistanceToPoint (Point p)
return Math.Sqrt(Math.Pow((X - p.X), 2)+ Math.Pow((Y - p.Y), 2));
const string file = ("NAME : att48 COMMENT : 48 capitals of the US (Padberg/Rinaldi) TYPE : TSP DIMENSION : 48 EDGE_WEIGHT_TYPE : ATT NODE_COORD_SECTION 1 6734 1453 2 2233 10 3 5530 1424 4 401 841 5 3082 1644 6 7608 4458 7 7573 3716 8 7265 1268 9 6898 1885 10 1112 2049 11 5468 2606 12 5989 2873 13 4706 2674 14 4612 2035 15 6347 2683 16 6107 669 17 7611 5184 18 7462 3590 19 7732 4723 20 5900 3561 21 4483 3369 22 6101 1110 23 5199 2182 24 1633 2809 25 4307 2322 26 675 1006 27 7555 4819 28 7541 3981 29 3177 756 30 7352 4506 31 7545 2801 32 3245 3305 33 6426 3173 34 4608 1198 35 23 2216 36 7248 3779 37 7762 4595 38 7392 2244 39 3484 2829 40 6271 2135 41 4985 140 42 1916 1569 43 7280 4899 44 7509 3239 45 10 2676 46 6807 2993 47 5185 3258 48 3023 1942 EOF");
public string [] parts = file.Split();
public List <Point> pointlist= new List <Point> ();
public List<Point> ReadPoints()
Point x = new Point(0,0,0);
while ( i < parts.Length-1)
Point p = new Point (Convert.ToInt32(parts[i]), Convert.ToInt32(parts[i+1]), Convert.ToInt32(parts[i+2]));
public class ResultingTour
public double DistanceOfTheTour {get;set;}
public List<Point> OriginalListOfPoints;
public void CalculateCosts()
for(int i=0; i<Tour.Count-1; i++)
DistanceOfTheTour+=OriginalListOfPoints[Tour[i]].DistanceToPoint(OriginalListOfPoints[Tour[i+1]]);
public ResultingTour (List <int> tour, List<Point> originalListOfPoints)
OriginalListOfPoints=originalListOfPoints;
for(int i=0; i<Tour.Count-1; i++)
DistanceOfTheTour+=OriginalListOfPoints[Tour[i]].DistanceToPoint(OriginalListOfPoints[Tour[i+1]]);
public ResultingTour (ResultingTour tour)
OriginalListOfPoints=tour.OriginalListOfPoints;
DistanceOfTheTour=tour.DistanceOfTheTour;
public ResultingTour InitialTourGeneration (List <Point> pointlist)
List<int> tour = new List<int>();
List<int> pointsToStillInsert = new List<int>();
for(int i=1; i<pointlist.Count; i++)
pointsToStillInsert.Add(pointlist[i].ID);
while(pointsToStillInsert.Count>0)
int r = rand.Next(pointsToStillInsert.Count);
int indexOfPointToInsert = r;
int pointToInsert = pointsToStillInsert[indexOfPointToInsert];
pointsToStillInsert.RemoveAt(indexOfPointToInsert);
ResultingTour resultingTour = new ResultingTour(tour, pointlist);
public List<double> FitnessEvaluation(List<ResultingTour> population)
List<double> weightedDistance= new List<double>();
for(int i=0; i<population.Count; i++)
if(population[i].DistanceOfTheTour>maxDistance)
maxDistance=population[i].DistanceOfTheTour;
for(int i=0; i<population.Count; i++)
fitnessSum+=maxDistance-population[i].DistanceOfTheTour;
List<double> fitnessList = new List <double>();
for(int i=0; i<population.Count; i++)
fitnessList.Add(((maxDistance-population[i].DistanceOfTheTour)/fitnessSum));
public ResultingTour SelectParent(List<ResultingTour> population, List<double> fitnessList)
double n = rand.NextDouble();
for(int i=0; i<population.Count; i++)
partialSum+=fitnessList[i];
return population[population.Count-1];
public ResultingTour Crossover(ResultingTour parentA, ResultingTour parentB, List<Point> originalpointlist)
List<int> tour= new List<int>();
ResultingTour child = new ResultingTour(tour, originalpointlist);
c = rand.Next(1,parentA.Tour.Count-1);
child.Tour.Insert(i,parentA.Tour[i]);
int counterMax=(parentA.Tour.Count-1)-c;
for(int p=c; p<parentB.Tour.Count; p++)
bool alreadyInTheChild=false;
if(parentB.Tour[p]==child.Tour[y])
if(alreadyInTheChild==false)
child.Tour.Insert(c+counter,parentB.Tour[p]);
bool alreadyInTheChild=false;
if(parentB.Tour[p]==child.Tour[y])
if(alreadyInTheChild==false)
child.Tour.Insert(c+counter,parentB.Tour[p]);
child.Tour.Add(child.Tour[0]);
public ResultingTour Mutation (ResultingTour tour)
bool alreadyMutated=false;
while(alreadyMutated==false)
int a = rand.Next(1,tour.Tour.Count-1);
int b = rand.Next(a+1,tour.Tour.Count-1);
tour.Tour[a] = tour.Tour[b];
public ResultingTour MutationAlternative (ResultingTour tour)
bool alreadyMutated=false;
while(alreadyMutated==false)
int a = rand.Next(1,tour.Tour.Count-1);
int b = rand.Next(1,tour.Tour.Count-1);
tour.Tour.Reverse(b,a-b+1);
tour.Tour.Reverse(a,b-a+1);
public List <ResultingTour> GeneticAlgorithm (List <Point> pointlist, double mutationRate)
List <ResultingTour> population = new List <ResultingTour>();
int numberOfGenerations=50;
for(int d=0; d<populationSize; d++)
ResultingTour resultingTour=InitialTourGeneration(pointlist);
population.Add(resultingTour);
for(int g=0; g<numberOfGenerations; g++)
List <ResultingTour> newPopulation = new List <ResultingTour>();
List<double> fitnessList=FitnessEvaluation(population);
double maxFitnessValue=0;
int indexOfMaxFitnessValue=-1;
for(int i=0; i<population.Count; i++)
if(fitnessList[i]>maxFitnessValue)
maxFitnessValue=fitnessList[i];
indexOfMaxFitnessValue=i;
newPopulation.Add(new ResultingTour(population[indexOfMaxFitnessValue]));
int maxMutations=(int)(populationSize*mutationRate);
for(int i =0; i<maxMutations; i++)
ResultingTour newTour=Mutation(SelectParent(population,fitnessList));
newPopulation.Add(newTour);
for(int i=0; i<populationSize-maxMutations-1; i++)
ResultingTour newTour=Crossover(SelectParent(population,fitnessList),SelectParent(population,fitnessList), pointlist);
newPopulation.Add(newTour);
population=newPopulation;
public static void Main()
FileHandler fl=new FileHandler();
List <Point> pointlist = fl.ReadPoints();
List <ResultingTour> population=p.GeneticAlgorithm(pointlist, 0.2);
List<double> fitnessList= p.FitnessEvaluation(population);
double maxFitnessValue=0;
int indexOfMaxFitnessValue=-1;
for(int i=0; i<population.Count; i++)
if(fitnessList[i]>maxFitnessValue)
maxFitnessValue=fitnessList[i];
indexOfMaxFitnessValue=i;
Console.WriteLine("The best tour after generating 50 generations is tour:" + indexOfMaxFitnessValue);
for(int y=0; y<population[indexOfMaxFitnessValue].Tour.Count; y++)
Console.Write(population[indexOfMaxFitnessValue].Tour[y] + " ");
Console.WriteLine("the corresponding distance is: " + population[indexOfMaxFitnessValue].DistanceOfTheTour);
p.MutationAlternative (population[2]);