V
- the graph vertex typeE
- the graph edge typepublic class TwoOptHeuristicTSP<V,E> extends Object implements TSPAlgorithm<V,E>
The travelling salesman problem (TSP) asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?".
This is an implementation of the 2-opt improvement heuristic algorithm. The algorithm generates k random initial tours and then iteratively improves the tours until a local minimum is reached. In each iteration it applies the best possible 2-opt move which means to find the best pair of edges $(i,i+1)$ and $(j,j+1)$ such that replacing them with $(i,j)$ and $(i+1,j+1)$ minimizes the tour length.
See wikipedia for more details.
This implementation can also be used in order to try to improve an existing tour. See method
improveTour(GraphPath)
.
Constructor and Description |
---|
TwoOptHeuristicTSP()
Constructor.
|
TwoOptHeuristicTSP(int k)
Constructor
|
TwoOptHeuristicTSP(int k,
long seed)
Constructor
|
TwoOptHeuristicTSP(int k,
Random rng)
Constructor
|
public TwoOptHeuristicTSP()
public TwoOptHeuristicTSP(int k)
k
- how many initial random tours to checkpublic TwoOptHeuristicTSP(int k, long seed)
k
- how many initial random tours to checkseed
- seed for the random number generatorpublic TwoOptHeuristicTSP(int k, Random rng)
k
- how many initial random tours to checkrng
- random number generatorpublic GraphPath<V,E> getTour(Graph<V,E> graph)
getTour
in interface HamiltonianCycleAlgorithm<V,E>
getTour
in interface TSPAlgorithm<V,E>
graph
- the input graphIllegalArgumentException
- if the graph is not undirectedIllegalArgumentException
- if the graph is not completeIllegalArgumentException
- if the graph contains no verticesCopyright © 2018. All rights reserved.