Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Scientific Calculations with Julia/Traveling Salesman

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Scientific Calculations with Julia Traveling Salesman Shortest Path Mortgages and Loans In the traveling salesman problem given a set of cities and knowing the distances between each pair of cities, you need to find the shortest route the salesman must take to visit each city exactly once and return to the starting city. To solve the problem with Julia, you must first download the geographic coordinates (latitude, longitude) of the cities you want to visit. In Google Maps, search for the city or place you want to visit. Right-click on it and then click on the geographic coordinates, which will be copied to the clipboard so you can paste them into the code: city_coordinates = [ [38.12044401205735, 13.344912286949409], # Palermo [37.50787101641852, 15.080264582092187],# Catania [37.07558088824639, 15.286173206794285], # Siracusa [37.3084415245004, 13.585830724530107], # Agrigento [38.016020896565806, 12.535508424272644], # Trapani [38.19149527666429, 15.558410966147036], # Messina ] Create a function to convert geographic coordinates to Cartesian coordinates according to an approximate formula found online: function to_euclidean_coordinates(lat, lon) R = 6371000 x = R * cos(lat) * cos(lon) y= R * cos(lat) * sin(lon) return [x, y] end Convert geographic coordinates to Cartesian coordinates: cities =[] for coordinate in city_coordinates push!(cities,to_euclidean_coordinates(coord[1],coord[2])) end println(cities) Any[[4.1390843138109976e6, 4.082710768009927e6], [-5.062558793864646e6, 3.673371982848222e6], [-4.7188196445483435e6, 2.117434951611853e6], [3.08584312433141e6, 5.018078076055259e6], [6.050861735871778e6, -186802.1569603486], [-5.551514153391771e6, 836487.2998629898]] Install Julia's TravelingSalesmanExact and GLPK libraries and calculate the optimal traveling salesman tour: using TravelingSalesmanExact, GLPK set_default_optimizer!(GLPK.Optimizer) tour, cost = get_optimal_tour(cities, distance=TravelingSalesmanExact.euclidean_distance) plot_cities(cities[tour]) Display the optimal tour: citta = ["Palermo","Catania","Siracusa","Agrigento","Trapani","Messina"] for c in tour println(citta[c]) end Trapani Palermo Agrigento Catania Siracusa Messina ​