2

I am trying to solve Travelling Salesman Problem using Genetic Algorithym in C#. But in my app best values changes so slowly. I have tried with different Crossing-Over methods such as classic, greedy and pmx but I have never got what I want. What is the most effective reason that causes slow approximation to local minimum in Genetic Algorithyms? Isn't it Crossing-Over methods?

I think my method for CO is correct, isn't it?. My code:

        Tour ClassicCrossingOver(Tour mother, Tour father)
        {
            int pos = N / 2;
            City[] gens = new City[N];

            for (int i = 0; i < pos; i++)
            {
                gens[i] = mother.Cities[i];
            }
            List<int> nonPos = new List<int>(); //Handles duplicate city positions
            for (int i = pos; i < gens.Length; i++) 
            {
                if (gens.Contains(father.Cities[i]))
                    nonPos.Add(i); 
                gens[i] = father.Cities[i];
            }
            List<City> noneGenes = new List<City>(); //Handles cities that doesnt exists in the child
            foreach (City gene in map.Cities)
            {
                if (gens.Contains(gene)) continue;
                noneGenes.Add(gene);
            }
            for (int i = 0; i < noneGenes.Count; i++) 
            {
                int j = rnd.Next(nonPos.Count - 1);
                gens[nonPos[j]] = noneGenes[i];
                nonPos.RemoveAt(j);
            }
            Tour tur = new Tour(map) { Cities = gens };
            return tur;
        }
Ali Tor
  • 2,772
  • 2
  • 27
  • 58
  • I'm not sure if I understand your code well enough, but it seems you're always crossing-over halfway the mother and father. If you cross two children of the same mother and father this might result in the same father and mother. Adding randomness in the crossover point will fix this issue (so let pos a random number between 0 and N). A second improvement after that is adding a second crossover point. – Niels V Jan 01 '16 at 22:17
  • @Niels, I also tried it using randomness. But it doesnt works again and GA cannot fix long paths easily even 1 million child is produced. I can't understand why. I 've never tried with double crossover point but I will try it and give a feedback. Thanks. – Ali Tor Jan 01 '16 at 22:55

2 Answers2

1

A crossover often called 'Double Point Ordered' can be very useful here. This type of crossover creates a child from a single parent. Two parents are selected and two random points along the chromosome are selected. The genes between the points are passed to the child. The remaining genes are transferred from the same parent, but in the order that they appear in the second parent. The result is that the child contains all of the values from a single parent but includes ordering, and therefore traits, from both parents.

I have a couple of examples of the TSP here which may help

http://johnnewcombe.net/blog/gaf-part-4/ http://johnnewcombe.net/blog/gaf-part-7/

John Newcombe
  • 364
  • 2
  • 7
0

In my experience using GA, Ordered Crossover (OX1) is one of the best crossover operators to solve TSP.

OX1: A randomly selected portion of one parent is mapped to a portion of the other parent. From the replaced portion on, the rest is filled up by the remaining genes, where already present genes are omitted and the order is preserved.

Other operators can influence the speed that GA reach best values. I usually use the operators bellow in Traveling Salesman Problem:

  • Crossover: Ordered Crossover (OX1)
  • Mutation: Reverse Sequence Mutation
  • Selection: Elite Selection

This is a sample code to solve TSP, using GeneticSharp, that found a good solution in a few seconds:

var numberOfCities = 20;
var selection = new EliteSelection();
var crossover = new OrderedCrossover();
var mutation = new ReverseSequenceMutation();
var fitness = new TspFitness(numberOfCities, 0, 1000, 0, 1000);
var chromosome = new TspChromosme(numberOfCities);
var population = new Population (100, 200, chromosome);

var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
ga.Termination = new GenerationNumberTermination(100);

Console.WriteLine("GA running...");
ga.Start();

Console.WriteLine("Best solution found has {0} fitness.", ga.BestChromosome.Fitness);

You can see TspChromosome implementation at TspChromosome.cs and TspFitness at TspFitness.cs.

giacomelli
  • 7,287
  • 2
  • 27
  • 31