Opt2Tsp implements the 2-opt heuristic for solving symmetric TSP.
This algorithm starts with an initial tour and iteratively improves it. At each step, it removes two edges and the reconnects the created two paths in the other way if the resulting tour is shorter. The algorithm finishes when no such 2-opt move can be applied, and so the tour is 2-optimal.
If no starting tour is given to the run() function, then the algorithm uses the node sequence determined by the node IDs. Oherwise, it starts with the given tour.
This is a rather slow but effective method. Its typical usage is the improvement of the result of a fast tour construction heuristic (e.g. the InsertionTsp algorithm).
CM | Type of the cost map. |
#include <lemon/opt2_tsp.h>
Public Types | |
typedef CM | CostMap |
Type of the cost map. | |
typedef CM::Value | Cost |
Type of the edge costs. | |
Public Member Functions | |
Opt2Tsp (const FullGraph &gr, const CostMap &cost) | |
Constructor. More... | |
Execution Control | |
Cost | run () |
Runs the algorithm from scratch. More... | |
template<typename Path > | |
Cost | run (const Path &tour) |
Runs the algorithm starting from the given tour. More... | |
Cost | run (const std::vector< Node > &tour) |
Runs the algorithm starting from the given tour. More... | |
Query Functions | |
Cost | tourCost () const |
The total cost of the found tour. More... | |
const std::vector< Node > & | tourNodes () const |
Returns a const reference to the node sequence of the found tour. More... | |
template<typename Iterator > | |
void | tourNodes (Iterator out) const |
Gives back the node sequence of the found tour. More... | |
template<typename Path > | |
void | tour (Path &path) const |
Gives back the found tour as a path. More... | |
Constructor.
gr | The full graph the algorithm runs on. |
cost | The cost map. |
|
inline |
This function runs the algorithm starting from the tour that is determined by the node ID sequence.
This function runs the algorithm starting from the given tour.
tour | The tour as a path structure. It must be a valid path containing excactly n arcs. |
|
inline |
This function runs the algorithm starting from the given tour (node sequence).
tour | A vector that stores all Node s of the graph in the desired order. |
|
inline |
This function returns the total cost of the found tour.
|
inline |
This function returns a const reference to a vector that stores the node sequence of the found tour.
|
inline |
This function copies the node sequence of the found tour into an STL container through the given output iterator. The value_type
of the container must be FullGraph::Node
. For example,
or
|
inline |
This function copies the found tour as a list of arcs/edges into the given path structure.