@@ 5,11 5,38 @@ use itertools::Itertools;
use nalgebra::{allocator::Allocator, distance, Const, DefaultAllocator, Dim, Dyn, OMatrix, OVector, Point};
use plotters::prelude::*;
+use crate::graph::{minimal_spanning_tree_kruskal, Edge, GenericGraph, Graph, WeightedEdge};
+
#[derive(PartialEq, Clone, Debug)]
pub struct TSPCity {
point: Point<f64, 2>
}
+#[derive(Debug)]
+pub struct TSPEdge {
+ from: usize,
+ to: usize,
+ distance: f64
+}
+
+impl Edge for TSPEdge {
+ fn from_node(&self) -> usize {
+ self.from
+ }
+
+ fn to_node(&self) -> usize {
+ self.to
+ }
+}
+
+impl WeightedEdge for TSPEdge {
+ type Cost = f64;
+
+ fn cost(&self) -> Self::Cost {
+ self.distance
+ }
+}
+
#[derive(PartialEq, Clone, Debug)]
pub struct NodePermutation<D: Dim>
where
@@ 78,6 105,23 @@ where
distances
}
}
+
+ pub fn to_graph(self) -> GenericGraph<TSPCity, TSPEdge> {
+ let cities = self.cities.len();
+ let mut graph = GenericGraph::new(self.cities, false);
+
+ for i in 0..cities {
+ for j in i+1..cities {
+ graph.add_generic_edge(TSPEdge {
+ from: i,
+ to: j,
+ distance: self.distances[(i, j)]
+ });
+ }
+ }
+
+ graph
+ }
}
impl<D> TSPInstance<D>
@@ 435,7 479,6 @@ mod tests {
assert_eq!(vec![0usize, 1, 3, 4, 5, 2], offspring.permutation.into_iter().copied().collect::<Vec<_>>())
}
-
#[test]
fn test_reverse_subsequence_perturbation_behavior() {
let perturbation = ReverseSubsequencePerturbation::<Const<6>>::new();