Loading...
Searching...
No Matches
Opt2Tsp< CM > Class Template Reference
Algorithms » Traveling Salesman Problem

## Detailed Description

template<typename CM>
class lemon::Opt2Tsp< CM >

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).

Template Parameters

  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.
Execution Control
Cost | run ()
| Runs the algorithm from scratch.
template<typename Path>
Cost | run (const Path &tour)
| Runs the algorithm starting from the given tour.
Cost | run (const std::vector< Node > &tour)
| Runs the algorithm starting from the given tour.
Query Functions
Cost | tourCost () const
| The total cost of the found tour.
const std::vector< Node > & | tourNodes () const
| Returns a const reference to the node sequence of the found tour.
template<typename Iterator>
void | tourNodes (Iterator out) const
| Gives back the node sequence of the found tour.
template<typename Path>
void | tour (Path &path) const
| Gives back the found tour as a path.

## Constructor & Destructor Documentation

## Opt2Tsp()

template<typename CM>

Opt2Tsp | ( | const FullGraph & | gr,
|  | const CostMap & | cost )

inline

Constructor.

Parameters

  gr | The full graph the algorithm runs on.
  cost | The cost map.

## Member Function Documentation

## run() [1/3]

template<typename CM>

Cost run | ( |  | ) |

inline

This function runs the algorithm starting from the tour that is determined by the node ID sequence.

Returns
  The total cost of the found tour.

## run() [2/3]

template<typename CM>
template<typename Path>

Cost run | ( | const Path & | tour | ) |

inline

This function runs the algorithm starting from the given tour.

Parameters

  tour | The tour as a path structure. It must be a valid path containing excactly n arcs.

Returns
  The total cost of the found tour.

## run() [3/3]

template<typename CM>

Cost run | ( | const std::vector< Node > & | tour | ) |

inline

This function runs the algorithm starting from the given tour (node sequence).

Parameters

  tour | A vector that stores all Nodes of the graph in the desired order.

Returns
  The total cost of the found tour.

## tourCost()

template<typename CM>

Cost tourCost | ( |  | ) | const

inline

This function returns the total cost of the found tour.

Precondition
  run() must be called before using this function.

## tourNodes() [1/2]

template<typename CM>

const std::vector< Node > & tourNodes | ( |  | ) | const

inline

This function returns a const reference to a vector that stores the node sequence of the found tour.

Precondition
  run() must be called before using this function.

## tourNodes() [2/2]

template<typename CM>
template<typename Iterator>

void tourNodes | ( | Iterator | out | ) | const

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,

std::vector<FullGraph::Node> nodes(countNodes(graph));
tsp.tourNodes(nodes.begin());
lemon::countNodes
int countNodes(const Graph &g)
Function to count the nodes in the graph.
Definition core.h:252

or

std::list<FullGraph::Node> nodes;
tsp.tourNodes(std::back_inserter(nodes));

Precondition
  run() must be called before using this function.

## tour()

template<typename CM>
template<typename Path>

void tour | ( | Path & | path | ) | const

inline

This function copies the found tour as a list of arcs/edges into the given path structure.

Precondition
  run() must be called before using this function.