tour_length: Calculate the Total Length of a TSP Tour

View source: R/TSP_tour_length.R View source: R/TSP_length.R

tour_lengthR Documentation

Calculate the Total Length of a TSP Tour

Description

This function computes the total length of a given TSP tour by calculating the Euclidean distance between each consecutive pair of cities in the 'tour' vector.

Usage

tour_length(tour, villes)

Arguments

tour

An integer vector representing the order of cities in the TSP solution. Each element of 'tour' corresponds to the index of a city in 'villes'.

villes

A numeric matrix or data frame containing the coordinates of the cities. The first column should represent the x-coordinates, and the second column should represent the y-coordinates of the cities.

Details

The function first calculates the pairwise Euclidean distance between each city in the tour and sums up all the distances to return the total length of the tour.

The distance between two cities \((x_1, y_1)\) and \((x_2, y_2)\) is computed as: \[ D = sqrt ((x_2 - x_1)^2 + (y_2 - y_1)^2) \]

Value

A numeric value representing the total length of the TSP tour, which is the sum of the Euclidean distances between consecutive cities in the tour.

Examples

# Example usage
tour <- c(1, 3, 2, 4)
villes <- data.frame(x = c(0, 1, 3, 5), y = c(0, 2, 3, 1))
total_length <- tour_length(tour, villes)
print(total_length)


vrunge/M2algorithmique documentation built on April 5, 2025, 4:06 a.m.