View source: R/10-grafos-TSP.R
compute_tour_distance | R Documentation |
It computes the distance covered by a tour in a Traveling Salesman Problem
compute_tour_distance(h, d)
h |
A tour specified by a vertex sequence |
d |
Distance matrix to use |
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.
The tour distance
d(\{v_1,...,v_n\}) = \sum_{j=1}^n
d(v_j,v_{(j mod n) + 1}).
Cesar Asensio
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.
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.