use crate::aco::FMatrix;
pub trait LocalUpdate {
fn apply(&mut self, pheromone: &mut FMatrix, partial_paths: &[Vec<usize>]);
}
pub struct Decay {
decay_rate: f64,
}
impl Decay {
pub fn new(decay_rate: f64) -> Self {
assert!(
(0.0..1.0).contains(&decay_rate),
"Decay rate must be in range (0.0..1.0)"
);
Self { decay_rate }
}
}
impl LocalUpdate for Decay {
fn apply(&mut self, pheromone: &mut FMatrix, partial_paths: &[Vec<usize>]) {
for p_path in partial_paths {
let l = p_path.len();
assert!(l > 1);
let s = p_path[l - 2];
let r = p_path[l - 1];
pheromone[(s, r)] *= self.decay_rate;
}
}
}
pub struct DecayTo {
decay_rate: f64,
stable_constant: f64,
}
impl DecayTo {
pub fn new(decay_rate: f64, stable_constant: f64) -> Self {
assert!(
(0.0..1.0).contains(&decay_rate),
"Decay rate must be in range (0.0..1.0)"
);
Self {
decay_rate,
stable_constant,
}
}
}
impl LocalUpdate for DecayTo {
fn apply(&mut self, pheromone: &mut FMatrix, partial_paths: &[Vec<usize>]) {
for p_path in partial_paths {
let l = p_path.len();
let s = p_path[l - 2];
let r = p_path[l - 1];
pheromone[(s, r)] *= self.decay_rate;
pheromone[(s, r)] += self.stable_constant * (1.0 - self.decay_rate);
}
}
}