~ruther/ctu-fee-eoa

0ef7b7f5f8bdcdf492b1fc419279e7a7ea3fe666 — Rutherther a month ago 8534d90
feat(tsp): add TSPInstance convertor to GenericGraph
1 files changed, 44 insertions(+), 1 deletions(-)

M codes/tsp_hw01/src/tsp.rs
M codes/tsp_hw01/src/tsp.rs => codes/tsp_hw01/src/tsp.rs +44 -1
@@ 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();