~ruther/ctu-fee-eoa

ref: a5d7436a642358e39c1d3f3280c77fbc8ee73558 ctu-fee-eoa/codes/tsp_hw01/src/tsp.rs -rw-r--r-- 8.1 KiB
a5d7436a — Rutherther chore: update report 5 days ago
                                                                                
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::convert::Infallible;

use eoa_lib::fitness::FitnessFunction;
use itertools::Itertools;
use nalgebra::{allocator::Allocator, distance, Const, DefaultAllocator, Dim, Dyn, OMatrix, OVector, Point};
use plotters::prelude::*;

use crate::graph::{Edge, GenericGraph, 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
    DefaultAllocator: Allocator<D>
{
    pub permutation: OVector<usize, D>
}

/// An instance of TSP, a fully connected graph
/// with cities that connect to each other.
/// The D parameter represents the number of cities.
#[derive(PartialEq, Clone, Debug)]
pub struct TSPInstance<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D, D>
{
    pub cities: Vec<TSPCity>,
    pub distances: OMatrix<f64, D, D>
}

impl TSPInstance<Dyn>
where
{
    pub fn new_dyn(cities: Vec<(f64, f64)>) -> Self {
        let dim = Dyn(cities.len());

        let cities = OMatrix::<f64, Dyn, Const<2>>::from_fn_generic(dim, Const::<2>, |i, j| if j == 0 { cities[i].0 } else { cities[i].1 });
        TSPInstance::new(cities)
    }
}

impl<const D: usize> TSPInstance<Const<D>>
where
{
    pub fn new_const(cities: Vec<(f64, f64)>) -> Self {
        let cities = OMatrix::<f64, Const<D>, Const<2>>::from_fn(|i, j| if j == 0 { cities[i].0 } else { cities[i].1 });
        TSPInstance::new(cities)
    }
}

impl<D> TSPInstance<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D, D>,
    DefaultAllocator: Allocator<D>,
    DefaultAllocator: Allocator<D, Const<2>>,
{
    pub fn new(cities: OMatrix<f64, D, Const<2>>) -> Self {
        let dim = cities.shape_generic().0;

        let cities = cities.row_iter()
                .map(|position|
                     TSPCity { point: Point::<f64, 2>::new(position[0], position[1])  }
                )
                .collect::<Vec<_>>();

        let distances = OMatrix::from_fn_generic(
            dim,
            dim,
            |i, j| distance(&cities[i].point, &cities[j].point)
        );

        Self {
            cities,
            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>
where
    D: Dim,
    DefaultAllocator: Allocator<D, D>,
    DefaultAllocator: Allocator<D>,
{
    pub fn dimension(&self) -> D {
        self.distances.shape_generic().0
    }

    pub fn verify_solution(solution: &NodePermutation<D>) -> bool {
        let mut seen_vertices = OVector::from_element_generic(
            solution.permutation.shape_generic().0,
            solution.permutation.shape_generic().1,
            false
        );

        for &vertex in solution.permutation.iter() {
            // This vertex index is out of bounds
            if vertex >= solution.permutation.len() {
                return false;
            }

            // A node is repeating
            if seen_vertices[vertex] {
                return false;
            }

            seen_vertices[vertex] = true;
        }

        true
    }

    pub fn solution_cost(&self, solution: &NodePermutation<D>) -> f64 {
        solution.permutation
            .iter()
            .circular_tuple_windows()
            .map(|(&node1, &node2): (&usize, &usize)| self.distances(node1, node2))
            .sum()
    }

    pub fn distances(&self, city_a: usize, city_b: usize) -> f64 {
        self.distances[(city_a, city_b)]
    }

    fn plot_internal(&self, solution: Option<&NodePermutation<D>>, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
        let root = BitMapBackend::new(filename, (800, 600)).into_drawing_area();
        root.fill(&WHITE)?;

        let x_coords: Vec<f64> = self.cities.iter().map(|city| city.point.x).collect();
        let y_coords: Vec<f64> = self.cities.iter().map(|city| city.point.y).collect();

        let x_min = x_coords.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let x_max = x_coords.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let y_min = y_coords.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let y_max = y_coords.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        let x_padding = (x_max - x_min) * 0.1;
        let y_padding = (y_max - y_min) * 0.1;

        let x_range = (x_min - x_padding)..(x_max + x_padding);
        let y_range = (y_min - y_padding)..(y_max + y_padding);

        let title = if let Some(sol) = solution {
            format!("TSP Solution (Cost: {:.2})", self.solution_cost(sol))
        } else {
            "TSP Instance".to_string()
        };

        let mut chart = ChartBuilder::on(&root)
            .caption(&title, ("sans-serif", 40))
            .margin(10)
            .x_label_area_size(40)
            .y_label_area_size(40)
            .build_cartesian_2d(x_range, y_range)?;

        chart.configure_mesh().draw()?;

        if let Some(sol) = solution {
            chart.draw_series(
                sol.permutation.iter().circular_tuple_windows().map(|(&city1_idx, &city2_idx)| {
                    let city1 = &self.cities[city1_idx];
                    let city2 = &self.cities[city2_idx];
                    PathElement::new(vec![(city1.point.x, city1.point.y), (city2.point.x, city2.point.y)], BLUE)
                })
            )?;
        }

        chart.draw_series(
            self.cities.iter().map(|city| {
                Circle::new((city.point.x, city.point.y), 5, RED.filled())
            })
        )?;

        root.present()?;
        Ok(())
    }

    pub fn plot(&self, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
        self.plot_internal(None, filename)
    }

    pub fn draw_solution(&self, solution: &NodePermutation<D>, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
        self.plot_internal(Some(solution), filename)
    }
}

impl<D> FitnessFunction for TSPInstance<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D, D>,
    DefaultAllocator: Allocator<D>,
{
    type In = NodePermutation<D>;
    type Out = f64;
    type Err = Infallible;

    fn fit(self: &Self, inp: &Self::In) -> Result<Self::Out, Self::Err> {
        assert_eq!(inp.permutation.len(), self.cities.len());
        assert!(TSPInstance::verify_solution(inp));
        Ok(self.solution_cost(inp))
    }
}

#[cfg(test)]
mod tests {
    use nalgebra::{Const, SVector};
    use rand::seq::SliceRandom;

    use super::{NodePermutation, TSPInstance};

    #[test]
    fn test_verify_solution() {
        let mut rng = rand::rng();
        let rng = &mut rng;
        let mut chromosome = NodePermutation::<Const<6>> {
            permutation: SVector::from_vec(vec![0, 1, 2, 3, 4, 5])
        };

        for _ in 0..100 {
            chromosome.permutation.as_mut_slice().shuffle(rng);
            assert!(TSPInstance::verify_solution(&chromosome));
        }

        // Out of bounds
        chromosome.permutation[0] = 6;
        assert!(!TSPInstance::verify_solution(&chromosome));
        chromosome.permutation[0] = 7;
        assert!(!TSPInstance::verify_solution(&chromosome));
        chromosome.permutation[0] = 8;
        assert!(!TSPInstance::verify_solution(&chromosome));

        // Repeating
        chromosome.permutation[0] = 5;
        chromosome.permutation[1] = 5;
        assert!(!TSPInstance::verify_solution(&chromosome));

        let chromosome = NodePermutation::<Const<6>> {
            permutation: SVector::from_vec(vec![0, 1, 2, 3, 1, 5])
        };
        assert!(!TSPInstance::verify_solution(&chromosome));
    }


}