0

I am working on GA for a project. I am trying to solve Travelling Salesman Problem using GA. I used array[] to store data, I think Arrays are much faster than List. But for any reason it takes too much time. e.g. With MaxPopulation = 100000, StartPopulation=1000 the program lasts to complete about 1 min. I want to know if this is a problem. If it is, how can I fix this?

A code part from my implementation:

        public void StartAsync()
        {
            Task.Run(() =>
            {
                CreatePopulation();
                currentPopSize = startPopNumber;

                while (currentPopSize < maxPopNumber) 
                {
                    Tour[] elits = ElitChromosoms();

                    for (int i = 0; i < maxCrossingOver; i++)
                    {
                        if (currentPopSize >= maxPopNumber)
                            break;
                        int x = rnd.Next(elits.Length - 1);
                        int y = rnd.Next(elits.Length - 1);

                        Tour parent1 = elits[x];
                        Tour parent2 = elits[y];

                        Tour child = CrossingOver(parent1, parent2);

                        int mut = rnd.Next(100);

                        if (mutPosibility >= mut)
                        {
                            child = Mutation(child);
                        }
                        population[currentPopSize] = child;
                        currentPopSize++;
                    }
                    progress = currentPopSize * 100 / population.Length;
                    this.Progress = progress;
                    GC.Collect();
                }
                if (GACompleted != null)
                    GACompleted(this, EventArgs.Empty);
            });
        }

In here "elits" are the chromosoms that have greater fit value than the average fit value of the population.

Ali Tor
  • 2,772
  • 2
  • 27
  • 58
  • How many cities do you have in your simulation? – giacomelli Dec 28 '15 at 23:44
  • Just 7 cities. The more interesting point is that. Am I wrong at adding all chromosoms to population with no matter it has good or bad value? Is it better to change child chromosoms with old and bad chromosoms in new population? With doing this, I can work with less sized arrays, I think. – Ali Tor Dec 28 '15 at 23:56
  • Why do you call GC.Collect? It should not be there, try to delete it. – Radin Gospodinov Dec 29 '15 at 09:12

2 Answers2

1

Scientific papers suggest smaller population. Maybe you should follow what is written by the other authors. Having big population does not give you any advantage.

TSP can be solved by GA, but maybe it is not the most efficient approach to attack this problem. Look at this visual representation of TSP-GA: http://www.obitko.com/tutorials/genetic-algorithms/tsp-example.php

Todor Balabanov
  • 376
  • 3
  • 6
  • 17
0

Ok. I have just found a solution. Instead of using an array with size of maxPopulation, change new generations with the old and bad one who has bad fitness. Now, I am working with a less sized array, which has length of 10000. The length was 1,000.000 before and it was taking too much time. Now, in every iteration, select best 1000 chromosomes and create new chromosomes using these as parent and replace to old and bad ones. This works perfect.

Code sample:

    public void StartAsync()
             {
                CreatePopulation(); //Creates chromosoms for starting
                currentProducedPopSize = popNumber; //produced chromosom number, starts with the length of the starting population

                while (currentProducedPopSize < maxPopNumber && !stopped) 
                {
                    Tour[] elits = ElitChromosoms();//Gets best 1000 chromosoms
                    Array.Reverse(population);//Orders by descending
                    this.Best = elits[0];
              //Create new chromosom as many as the number of bad chromosoms
                    for (int i = 0; i < population.Length - elits.Length; i++) 
                    {
                        if (currentProducedPopSize >= maxPopNumber || stopped)
                            break;
                        int x = rnd.Next(elits.Length - 1);
                        int y = rnd.Next(elits.Length - 1);

                        Tour parent1 = elits[x];
                        Tour parent2 = elits[y];

                        Tour child = CrossingOver(parent1, parent2);

                        int mut = rnd.Next(100);

                        if (mutPosibility <= mut)
                        {
                            child = Mutation(child);
                        }
                        population[i] = child;//Replace new chromosoms
                        currentProducedPopSize++;//Increase produced chromosom  number
                    }

                    progress = currentProducedPopSize * 100 / maxPopNumber;
                    this.Progress = progress;
                    GC.Collect();
                }
                stopped = false;
                this.Best = population[population.Length - 1];
                if (GACompleted != null)
                    GACompleted(this, EventArgs.Empty);
            }

            Tour[] ElitChromosoms()
            {
                Array.Sort(population);
                Tour[] elits = new Tour[popNumber / 10];

                Array.Copy(population, elits, elits.Length);
                return elits;

            }
giacomelli
  • 7,287
  • 2
  • 27
  • 31
Ali Tor
  • 2,772
  • 2
  • 27
  • 58