rlevo_evolution/algorithms/metaheuristic/
aco_perm.rs1use std::marker::PhantomData;
16
17use burn::tensor::{backend::Backend, Int, Tensor};
18use rand::Rng;
19
20use crate::strategy::{Strategy, StrategyMetrics};
21
22#[derive(Debug, Clone)]
24pub struct AcoPermConfig {
25 pub pop_size: usize,
27 pub n_nodes: usize,
29 pub rho: f32,
31 pub alpha: f32,
33 pub beta: f32,
35}
36
37impl AcoPermConfig {
38 #[must_use]
40 pub fn default_for(pop_size: usize, n_nodes: usize) -> Self {
41 Self {
42 pop_size,
43 n_nodes,
44 rho: 0.5,
45 alpha: 1.0,
46 beta: 2.0,
47 }
48 }
49}
50
51#[derive(Debug, Clone)]
53pub struct AcoPermState<B: Backend> {
54 _backend: PhantomData<fn() -> B>,
55}
56
57#[derive(Debug, Clone, Copy, Default)]
78pub struct AntColonyPermutation<B: Backend> {
79 _backend: PhantomData<fn() -> B>,
80}
81
82impl<B: Backend> AntColonyPermutation<B> {
83 #[must_use]
85 pub fn new() -> Self {
86 Self {
87 _backend: PhantomData,
88 }
89 }
90}
91
92impl<B: Backend> Strategy<B> for AntColonyPermutation<B> {
93 type Params = AcoPermConfig;
94 type State = AcoPermState<B>;
95 type Genome = Tensor<B, 2, Int>;
96
97 fn init(
98 &self,
99 _params: &AcoPermConfig,
100 _rng: &mut dyn Rng,
101 _device: &B::Device,
102 ) -> AcoPermState<B> {
103 todo!(
104 "permutation ACO is not yet implemented; \
105 use AntColonyReal for continuous problems in the meantime"
106 )
107 }
108
109 fn ask(
110 &self,
111 _params: &AcoPermConfig,
112 _state: &AcoPermState<B>,
113 _rng: &mut dyn Rng,
114 _device: &B::Device,
115 ) -> (Self::Genome, AcoPermState<B>) {
116 todo!("permutation ACO is not yet implemented")
117 }
118
119 fn tell(
120 &self,
121 _params: &AcoPermConfig,
122 _population: Self::Genome,
123 _fitness: Tensor<B, 1>,
124 _state: AcoPermState<B>,
125 _rng: &mut dyn Rng,
126 ) -> (AcoPermState<B>, StrategyMetrics) {
127 todo!("permutation ACO is not yet implemented")
128 }
129
130 fn best(&self, _state: &AcoPermState<B>) -> Option<(Self::Genome, f32)> {
131 None
132 }
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138 use burn::backend::NdArray;
139
140 type TestBackend = NdArray;
141
142 #[test]
143 fn stub_is_constructible() {
144 let _strategy = AntColonyPermutation::<TestBackend>::new();
145 let _params = AcoPermConfig::default_for(32, 20);
146 }
147
148 #[test]
149 #[should_panic(expected = "permutation ACO is not yet implemented")]
150 fn init_panics_with_clear_message() {
151 use rand::SeedableRng;
152 let strategy = AntColonyPermutation::<TestBackend>::new();
153 let params = AcoPermConfig::default_for(4, 5);
154 let mut rng = rand::rngs::StdRng::seed_from_u64(0);
155 let _ = strategy.init(¶ms, &mut rng, &Default::default());
156 }
157}