Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on Genetic Algorithyms to solve Travelling Salesman Problem. My chromosom representation is like 32415 as the order of the cities. I have tried 3 Crossing-Over methods.These are Classic, OX1 and Greedy. But any of them couldnt help me to solve TSP efficiently. The basic problem is that the algorithym always leaves an unfixed crossed path. This means it doesnt works good.

To be clearly understanded, please see this image. The circled paths can be fixed to get a better solution as you see.But GA doesn't do that. How can I fix this problem?

I am using tournament with group of 5 to select parents, generally %3 mutation odds and 10000 fixed population size.
My code for Crossing Over, Mutation and GA Procedure:

Tour OrderingCrossingOver(Tour mother, Tour father)
{
   //Selected number of gens for CO
    int numberOfGens = rnd.Next(1, N - 2);
    //Selected position to start for CO
    int pos = rnd.Next(0, N - numberOfGens);
    City[] gens = new City[N];

    List<City> noneGens = new List<City>(father.Cities);
    List<int> nonePos = new List<int>();
    for (int i = 0; i < N; i++)
    {
        if (i < pos || i >= pos + numberOfGens)
        {
            //Get gen positions to be empty
            nonePos.Add(i);
            continue;
        }
        gens[i] = mother.Cities[i];
        noneGens.Remove(gens[i]);
    }
    for (int i = 0; i < noneGens.Count; i++)
    {
        //Fill empty gens using father
        gens[nonePos[i]] = noneGens[i];
    }
    noneGens.Clear();
    nonePos.Clear();
    return new Tour(map) { Cities = gens };
}
void Mutation(Tour tour)
{
    int i = rnd.Next(N - 1);
    int j = rnd.Next(N - 1);

    City temp = tour.Cities[i];
    tour.Cities[i] = tour.Cities[j];
    tour.Cities[j] = temp;

    mut++;
}
public void StartAsync()
{
    Task.Run(() =>
    {
        isRunning = true;
        CreatePopulation();
        int st = 0;
        while (currentProducedPopSize < maxPopNumber && !stopped)
        {
            //Get parents ascending
            int[] nums = selectParents();

            //Get best two chromosomes
            Tour parent1 = population[nums[0]];
            Tour parent2 = population[nums[1]];

            #region First Child

            Tour child = OrderingCrossingOver(parent1, parent2);
            Tour old = new Tour(map) { Cities = child.Cities };
            if (mutPosibility >= rnd.Next(1, 100))
                Mutation(child);

            population[nums[nums.Length - 1]] = child;

            if (this.Best.Value > child.Value)
                this.Best = child;

            #endregion

            #region Second Child

            child = OrderingCrossingOver(parent2, parent1);

            if (mutPosibility >= rnd.Next(1, 100))
                Mutation(child);

            population[nums[nums.Length - 2]] = child;

            if (this.Best.Value > child.Value)
                this.Best = child;

            #endregion

            currentProducedPopSize += 2;
            st++;
            this.Progress = double.Parse((currentProducedPopSize * 1.0 / maxPopNumber * 100).ToString("00.00"));

        }

        isRunning = false;
        stopped = false;
        this.Best = population[population.Length - 1];
        if (GACompleted != null)
            GACompleted(this, EventArgs.Empty);
    });
}
Posted
Updated 4-Jan-16 15:00pm
v2
Comments
j4rey89 5-May-16 7:08am    
Hi Amt-Coder, I have come across similar problem,here's my full code (http://www.codeproject.com/Questions/1097913/Genetic-algorithm-for-TSP-getting-stuck-with-ineff), have you solved it? And would you mind sharing your project?
Amt-Coder 6-May-16 0:12am    
@j4rey89, Yes I solved it a long time ago. The reason for my problem was to use wrong Crossing Over type in wrong way and the wrong selection method. Now I am using TwoPoint method for crossing over and tour selection method for parent selection. I can share my project with you but I must edit it to run again before. Because I changed a lot code in the project for development and now it's not working.

1 solution

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Select a chromosome that hexibit your problem and run (step by step) your program in debugger to see what append.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900