1

I was trying to solve a TSP-like problem which I have asked about here: A travelling salesman that has state

It seems that my problem is very likely NP-hard. Therefore, my options are:

  • An algorithm which tries every single strategy
  • An algorithm which produces an approximate solution

Since my n is going to be small, I think it makes sense to go with the first possibility. However, for various practical and personal reasons, I would prefer to implement my solution as a genetic algorithm, rather than a more conventional brute-force solution (such as Pham Trung's answer in the link).

I reason that a genetic algorithm, like all heuristics, examines a subset of possible solutions to the problem. Exactly which subset this happens to be depends on the specific parameters of a given GA.

My question: For a GA, must this be a strict subset? Or is it possible to set up the GA with such parameters that it ends up checking every possible solution, and consequently running in non-polynomial time?

If the answer is yes, it would be helpful if you can explain how I can determine the exact parameters of the GA which would accomplish this, such as:

  • Population size
  • Mating method
  • Selection method
  • Mutation method
  • Cross over method

Alternatively, if it is impossible to easily come up with a set of parameters that will cause a GA to end up checking every solution, and never check the same solution repeatedly too many times, I'll take this as a negative answer.

I intend to implement this in C# - but for this question, feel free to disregard the language. I am pointing this out in case someone knows a library that already does what I want.

Community
  • 1
  • 1
Superbest
  • 25,318
  • 14
  • 62
  • 134
  • @500-InternalServerError I do not believe this is a duplicate; after skimming that question, it does not appear to deal with producing the optimal solution, but a "good enough" solution which a GA is normally expected to produce. – Superbest Mar 17 '14 at 01:49
  • Also, I already know how a typical GA works, so I don't need help with that. I also know that by playing with the parameters, I can make it more or less likely that it will return a solution "close" to the exact solution. I am asking about making a GA always return the exact solution. – Superbest Mar 17 '14 at 01:51

2 Answers2

1

This is not possible. It has been proven for simulated annealing that it is able to find the global optimal solution, but it requires a logarithmic annealing curve and infinite time. For the genetic algorithm such a proof does not exist.

Andreas
  • 6,447
  • 2
  • 34
  • 46
0

You can use a recursive implementation of Djikstra's algorithm and weighted nodes. So for example, you can start with a basic shortest path algorithm like so...

static void Main()
{
    var nodes = new List<string> { "X", "A", "B", "C", "D", "E" };
    var points = new List<Matrix>
    {
        new Matrix { Origin = "X", Destination = "A", Distance = 2 },
        new Matrix { Origin = "X", Destination = "B", Distance = 12 },
        new Matrix { Origin = "X", Destination = "C", Distance = 7 },
        new Matrix { Origin = "X", Destination = "D", Distance = 19 },
        new Matrix { Origin = "X", Destination = "E", Distance = 16 },
        new Matrix { Origin = "A", Destination = "B", Distance = 10 },
        new Matrix { Origin = "A", Destination = "C", Distance = 8 },
        new Matrix { Origin = "A", Destination = "D", Distance = 15 },
        new Matrix { Origin = "A", Destination = "E", Distance = 17 },
        new Matrix { Origin = "B", Destination = "A", Distance = 10 },
        new Matrix { Origin = "B", Destination = "C", Distance = 11 },
        new Matrix { Origin = "B", Destination = "D", Distance = 18 },
        new Matrix { Origin = "B", Destination = "E", Distance = 21 },
        new Matrix { Origin = "C", Destination = "A", Distance = 8 },
        new Matrix { Origin = "C", Destination = "B", Distance = 11 },
        new Matrix { Origin = "C", Destination = "D", Distance = 7 },
        new Matrix { Origin = "C", Destination = "E", Distance = 9 },
        new Matrix { Origin = "D", Destination = "A", Distance = 15 },
        new Matrix { Origin = "D", Destination = "B", Distance = 18 },
        new Matrix { Origin = "D", Destination = "C", Distance = 7 },
        new Matrix { Origin = "D", Destination = "E", Distance = 5 },
        new Matrix { Origin = "E", Destination = "A", Distance = 17 },
        new Matrix { Origin = "E", Destination = "B", Distance = 21 },
        new Matrix { Origin = "E", Destination = "C", Distance = 9 },
        new Matrix { Origin = "E", Destination = "D", Distance = 5 }
    };

    var sequences = allocateNodes(nodes[0], 
                                  nodes.Count, 
                                  points, 
                                  new Dictionary<int, string>());        
}

static Dictionary<int, string> allocateNodes(string current, 
                                             int nodes, 
                                             List<Matrix> points, 
                                             Dictionary<int, string> sequences)
{
    if (sequences.Count == nodes) return sequences;

    var nextNode = getNextNode(current, points);

    sequences.Add(sequences.Count + 1, nextNode);

    points.RemoveAll(x => x.Origin == current);
    points.RemoveAll(x => x.Destination == current);

    return points.Count == 0 
        ? sequences 
        : allocateNodes(nextNode, nodes, points, sequences);
}

static string getNextNode(string origin, IEnumerable<Matrix> points)
{
    var destinations = points.Where(x => x.Origin == origin);
    var shortestPath = destinations.Min(x => x.Distance);
    var destination = destinations.First(x => x.Distance == shortestPath);

    return destination.Destination;
}


class Matrix
{
    public string Origin { get; set; }
    public string Destination { get; set; }
    public int Distance { get; set; }
}

And then expand the criteria for next path to prioritize nodes with the lowest or highest values with another property in your matrix. Of course this is not a genetic algorithm, but it's also not a brute force approach and may be something you might want to consider...

gfish3000
  • 1,557
  • 1
  • 11
  • 22