1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
//! Travelling Salesman Problem Solvers. //! //! The aim of this crate is to host various Travelling Salesman Problem solvers. Patches //! implementing useful algorithms most welcome. //! //! For more information, please see the [Travelling Salesman //! Problem](https://en.wikipedia.org/wiki/Travelling_salesman_problem) Wikipedia article, and [In //! Pursuit of the Traveling Salesman: Mathematics at the Limits of //! Computation](http://www.amazon.com/Pursuit-Traveling-Salesman-Mathematics-Computation/dp/0691152705) //! //!# Examples //! //!``` //!extern crate time; //!extern crate travelling_salesman; //! //!fn main() { //! let tour = travelling_salesman::simulated_annealing::solve( //! &[ //! (27.0, 78.0), //! (18.0, 24.0), //! (48.0, 62.0), //! (83.0, 77.0), //! (55.0, 56.0), //! ], //! time::Duration::seconds(1), //! ); //! //! println!("Tour distance: {}, route: {:?}", tour.distance, tour.route); //!} //!``` //! //!# Support //! //! Please report any bugs or feature requests at: //! //! * [https://github.com/alfiedotwtf/travelling_salesman/issues](https://github.com/alfiedotwtf/travelling_salesman/issues) //! //! Watch the repository and keep up with the latest changes: //! //! * [https://github.com/alfiedotwtf/travelling_salesman/subscription](https://github.com/alfiedotwtf/travelling_salesman/subscription) //! //! Feel free to fork the repository and submit pull requests :) //! //!# Author //! //! [Alfie John](https://www.alfie.wtf) <[alfie@alfie.wtf](mailto:alfie@alfie.wtf)> //! //!# Warranty //! //! IT COMES WITHOUT WARRANTY OF ANY KIND. //! //!# Copyright and License //! //! Copyright (C) 2015 by Alfie John //! //! This program is free software: you can redistribute it and/or modify it under the terms of the //! GNU General Public License as published by the Free Software Foundation, either version 3 of //! the License, or (at your option) any later version. //! //! This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; //! without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See //! the GNU General Public License for more details. //! //! You should have received a copy of the GNU General Public License along with this program. If //! not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/). pub mod brute_force; pub mod hill_climbing; pub mod random_search; pub mod simulated_annealing; extern crate metaheuristics; extern crate rand; extern crate time; use metaheuristics::Metaheuristics; use rand::{ThreadRng, Rng}; struct TravellingSalesman<'a> { distance_matrix: &'a Vec<Vec<f64>>, rng: &'a mut ThreadRng, } struct Candidate { route: Vec<usize>, } impl<'a> Metaheuristics<Candidate> for TravellingSalesman<'a> { fn clone_candidate(&mut self, candidate: &Candidate) -> Candidate { Candidate { route: candidate.route.clone(), } } fn generate_candidate(&mut self) -> Candidate { let mut route: Vec<usize> = self.distance_matrix.iter().enumerate().map(|(i,_)| i).collect(); self.rng.shuffle(&mut route); let home_city = route[0]; route.push(home_city); Candidate { route: route, } } fn rank_candidate(&mut self, candidate: &Candidate) -> f64 { 0.0 - get_route_distance(self.distance_matrix, &candidate.route) } fn tweak_candidate(&mut self, candidate: &Candidate) -> Candidate { if candidate.route.len() <= 3 { return self.clone_candidate(candidate); } let mut old_route = candidate.route.clone(); old_route.pop(); // get two cities to work with let start = self.rng.gen::<usize>() % old_route.len(); let end = self.rng.gen::<usize>() % old_route.len(); let (start, end) = if start < end { (start, end) } else { (end, start) }; // straight swap of the cities let mut swapped_route = old_route.clone(); swapped_route.swap(start, end); // swap cities, then reverse the cities between them let split_route = old_route.clone(); let safe_offset = if old_route.len() <= (end + 1) { old_route.len() } else { end + 1 }; let (left, right) = split_route.split_at(safe_offset); let (left, middle) = left.split_at(start); let mut middle = middle.to_vec(); middle.reverse(); let mut reordered_route = Vec::new(); reordered_route.extend(left.into_iter()); reordered_route.extend(middle.into_iter()); reordered_route.extend(right.into_iter()); // return shortest route let swapped_distance = get_route_distance(self.distance_matrix, &swapped_route); let reordered_distance = get_route_distance(self.distance_matrix, &reordered_route); let mut shortest_route = if swapped_distance < reordered_distance { swapped_route } else { reordered_route }; let home_city = shortest_route[0]; shortest_route.push(home_city); return Candidate { route: shortest_route, } } } /// Represents a tour of the travelling salesman pub struct Tour { /// the total distance travelled following this tour pub distance: f64, /// the ordered route for this tour pub route: Vec<usize>, } /// Utility function to convert city coordinates to a distance matrix /// /// `cities` is an array slice, containing `(x,y)` tuple coordinates for each city. /// /// Returns a `Vec<Vec<f64>>`, containing the distance matrix. /// ///# Examples /// ///``` ///extern crate travelling_salesman; /// ///fn main() { /// let cities = [ /// (27.0, 78.0), /// (18.0, 24.0), /// (48.0, 62.0), /// (83.0, 77.0), /// (55.0, 56.0), /// ]; /// /// let distance_matrix = travelling_salesman::get_distance_matrix(&cities); /// /// println!("The distance between 1 and 2 is: {}", distance_matrix[1][2]); ///} ///``` pub fn get_distance_matrix(cities: &[(f64, f64)]) -> Vec<Vec<f64>> { cities.iter().map(|row| { cities.iter().map(|column| { ((column.0 - row.0).powi(2) + (column.1 - row.1).powi(2)).sqrt() }).collect::<Vec<f64>>() }).collect::<Vec<Vec<f64>>>() } /// Utility function to calculate the distance travelled following the specified route /// /// `distance_matrix` is a `&Vec<Vec<f64>>` containing the distance matrix. /// /// `route` is a `&Vec<usize>`, containing the route of the travelling salesman. /// /// Returns an `f64`, representing the distance of the route travelled. /// ///# Examples /// ///``` ///extern crate travelling_salesman; /// ///fn main() { /// let cities = [ /// (27.0, 78.0), /// (18.0, 24.0), /// (48.0, 62.0), /// (83.0, 77.0), /// (55.0, 56.0), /// ]; /// /// let route_distance = travelling_salesman::get_route_distance( /// &travelling_salesman::get_distance_matrix(&cities), /// &vec![0, 2, 3, 4, 1, 0] /// ); /// /// println!("The route distance for the tour [0, 2, 3, 4, 1, 0] is {}", route_distance); ///} ///``` pub fn get_route_distance(distance_matrix: &Vec<Vec<f64>>, route: &Vec<usize>) -> f64 { let mut route_iter = route.iter(); let mut current_city = match route_iter.next() { None => return 0.0, Some(v) => *v, }; route_iter.fold(0.0, |mut total_distance, &next_city| { total_distance += distance_matrix[current_city as usize][next_city as usize]; current_city = next_city; total_distance }) }