traveling salesman problem, 2-opt algorithm c# implementation

后端 未结 3 930
深忆病人
深忆病人 2021-01-30 15:27

Can someone give me a code sample of 2-opt algorithm for traveling salesman problem. For now im using nearest neighbour to find the path but this method is far from perfect, and

3条回答
  •  不思量自难忘°
    2021-01-30 15:36

    Well, your solution to TSP is always going to be far from perfect. No code, but here's how to go about 2-Opt. It's not too bad:

    1. You need a class called Stop that has a Next, Prev, and City property, and probably a Stops property that just returns the array containing Next and Prev.
    2. When you link them together, we'll call that a Tour. Tour has a Stop property (any of the stops will do), and an AllStops property, whose getter just walks the stops and returns them
    3. You need a method that takes a tour and returns its cost. Let's call that Tour.Cost().
    4. You need Tour.Clone(), which just walks the stops and clones them individually
    5. You need a method that generates the set of tours with two edges switched. Call this Tour.PossibleMutations()
    6. Start with your NN solution
    7. Call PossibleMutations() on it
    8. Call Cost() on all of them and take the one with the lowest result
    9. Repeat until the cost doesn't go down

提交回复
热议问题