View source: R/TSP_2_cheapest.R
TSP_cheapest | R Documentation |
This function solves the Traveling Salesman Problem (TSP) using the cheapest insertion heuristic. It generates a tour starting from either one city or all cities (iteratively) as possible starting points, and progressively inserts the remaining cities at the position that results in the least increase in distance.
TSP_cheapest(data, type = "one")
data |
A numeric matrix or data frame of coordinates where each row represents a city and each column represents the x and y coordinates (for a 2D TSP). |
type |
A character string specifying the type of starting city selection. If 'type = "one"', the starting city is randomly chosen. If 'type = "all"', the algorithm tests all possible starting cities and selects the best one. |
A numeric vector representing the order of cities in the optimal tour, with the class attribute set to "TSP".
# Generate a random set of 5 cities
cities <- matrix(runif(10), ncol = 2)
# Solve TSP using cheapest insertion starting from one city
best_tour <- TSP_cheapest(cities, type = "one")
# Solve TSP using cheapest insertion testing all starting cities
best_tour_all <- TSP_cheapest(cities, type = "all")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.