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
//! Defines a `MutationStrategy` that applies stochastic jitter to amplitude and phase.
use crate::field::FractalField;
use crate::graphedge::GraphEdge;
use crate::traits::MutationStrategy;
use num_complex::Complex;
use rand::Rng;
/// A mutation strategy that perturbs the amplitude and phase of each edge's
/// complex data by a random amount.
pub struct StochasticAmplitudePhase {
/// The maximum random change to apply to the amplitude.
pub amplitude_jitter: f32,
/// The maximum random change to apply to the phase (in radians).
pub phase_jitter: f32,
}
impl MutationStrategy for StochasticAmplitudePhase {
fn mutate(&self, field: &FractalField) -> FractalField {
let mut rng = rand::rng();
let edges = field
.edges
.iter()
.map(|e| {
// Deconstruct the complex number into polar coordinates (amplitude and phase)
let (amp, phase) = e.data.to_polar();
// Apply random jitter to each component
let amp_mut = amp + rng.random_range(-self.amplitude_jitter..self.amplitude_jitter);
let phase_mut = phase + rng.random_range(-self.phase_jitter..self.phase_jitter);
GraphEdge {
// Reconstruct the complex data from the new polar coordinates
data: Complex::from_polar(amp_mut.max(0.0), phase_mut),
..*e // Copy the rest of the fields from the original edge
}
})
.collect();
FractalField { edges }
}
}