~ruther/ctu-fee-eoa

b999449b832d3f3b48f1f4fe448528f721a9999b — Rutherther a month ago 99f0582
refactor: generalize apply_to_mutations
1 files changed, 25 insertions(+), 23 deletions(-)

M codes/eoa_lib/src/perturbation/mod.rs
M codes/eoa_lib/src/perturbation/mod.rs => codes/eoa_lib/src/perturbation/mod.rs +25 -23
@@ 251,14 251,6 @@ impl<const LEN: usize, T: PerturbationOperator<Chromosome = SVector<f64, LEN>>> 
        }
    }

    pub fn inner(&self) -> &T {
        &self.perturbation
    }

    pub fn inner_mut(&mut self) -> &mut T {
        &mut self.perturbation
    }

    fn within_bounds(&self, chromosome: &SVector<f64, LEN>) -> bool {
        chromosome.iter()
            .zip(self.min_max.iter())


@@ 327,23 319,33 @@ impl<T: 'static> MutationPerturbation<T> {
        }
    }

    pub fn apply_to_mutations(base_perturbation: &mut dyn PerturbationOperator<Chromosome = T>, apply: &mut dyn FnMut(&mut Self)) {
        if let Some(mutation) = base_perturbation.as_any_mut().downcast_mut::<MutationPerturbation<T>>() {
            apply(mutation);
        }
    pub fn apply_to_mutations(
        base_perturbation: &mut dyn PerturbationOperator<Chromosome = T>,
        apply: &mut dyn FnMut(&mut MutationPerturbation<T>)
    ) {
        apply_to_perturbations(base_perturbation, apply);
    }
}

        match base_perturbation.wrapped_mut() {
            WrappedMut::Single => (),
            WrappedMut::Wrapped(wrapped) => {
                Self::apply_to_mutations(wrapped, apply);
            },
            WrappedMut::ListWrapped(wrapped) => {
                for wrapped in wrapped {
                    Self::apply_to_mutations(wrapped, apply);
                }
            }
        };
pub fn apply_to_perturbations<T: 'static, U: PerturbationOperator<Chromosome = T>>(
    base_perturbation: &mut dyn PerturbationOperator<Chromosome = T>,
    apply: &mut dyn FnMut(&mut U)
) {
    if let Some(mutation) = base_perturbation.as_any_mut().downcast_mut::<U>() {
        apply(mutation);
    }

    match base_perturbation.wrapped_mut() {
        WrappedMut::Single => (),
        WrappedMut::Wrapped(wrapped) => {
            apply_to_perturbations(wrapped, apply);
        },
        WrappedMut::ListWrapped(wrapped) => {
            for wrapped in wrapped {
                apply_to_perturbations(wrapped, apply);
            }
        }
    };
}

impl<T: 'static> PerturbationOperator for MutationPerturbation<T> {