Learn R Programming

gor (version 2.0)

compute_tour_distance: Compute the distance of a TSP tour

Description

It computes the distance covered by a tour in a Traveling Salesman Problem

Usage

compute_tour_distance(h, d)

Value

The tour distance $$d(\{v_1,...,v_n\}) = \sum_{j=1}^n d(v_j,v_{(j mod n) + 1}).$$

Arguments

h

A tour specified by a vertex sequence

d

Distance matrix to use

Author

Cesar Asensio

Details

This function simply add the distances in a distance matrix indicated by a vertex sequence defining a tour. It takes into account that, in a tour, the last vertex is joined to the first one by an edge, and adds its distance to the result, unlike compute_path_distance.

See Also

build_tour_nn nearest neighbor heuristic with a single starting point, build_tour_nn_best repeats the previous algorithm with all possible starting points, compute_distance_matrix computes a distance matrix, compute_path_distance computes path distances, plot_tour plots a tour.

Examples

Run this code
set.seed(1)
n <- 25
z <- cbind(runif(n,min=1,max=10),runif(n,min=1,max=10))
d <- compute_distance_matrix(z)
h <- sample(1:n)              # A random tour
compute_tour_distance(h, d)   # 114.58

Run the code above in your browser using DataLab