mop-adapters 0.0.1

Solver adapters for MOP
Documentation
use std::{fmt::Debug, ops::AddAssign};

use mop_facades::{opt_problem::OptProblem, prelude::*};
use mop_solvers::{genetic_algorithm::spea2::Spea2, Solver};
use num_traits::Float;

use genetic_algorithm::{
    operators::{
        crossover::Crossover, mating_selection::MatingSelection, mutation::Mutation,
        survivor_selection::SurvivorSelection,
    },
    GeneticAlgorithmParams,
};

#[derive(Clone, Debug)]
pub struct Spea2Adapter<'a, C, CR, MA, MU, N, S, V> {
    spea2: Spea2<'a, C, CR, MA, MU, N, S, V>,
}

impl<'a, C, CR, MA, MU, N, S, V> OptFacadeSolver<'a, C, N, V>
    for Spea2Adapter<'a, C, CR, MA, MU, N, S, V>
where
    C: Send + Sync,
    CR: Crossover<N, V>,
    MA: MatingSelection<N, V>,
    MU: Mutation<C, N, V>,
    N: AddAssign + Debug + Float + Send + Sync,
    S: SurvivorSelection,
    V: Copy + Debug + Send + Sync,
{}

impl<'a, C, CR, MA, MU, N, S, V> Solver<N, OptProblem<'a, C, N, V>, V>
    for Spea2Adapter<'a, C, CR, MA, MU, N, S, V>
where
    C: Send + Sync,
    CR: Crossover<N, V>,
    MA: MatingSelection<N, V>,
    MU: Mutation<C, N, V>,
    N: AddAssign + Debug + Float + Send + Sync,
    S: SurvivorSelection,
    V: Copy + Debug + Send + Sync,
{
    fn best_result_objs(&self) -> &[N] {
        self.spea2.best_result_objs()
    }

    fn best_result_objs_avg(&self) -> N {
        self.spea2.best_result_objs_avg()
    }

    fn do_work_after(&mut self) {
        self.spea2.do_work_after();
    }

    fn do_work_before(&mut self) {
        self.spea2.do_work_before();
    }

    fn initialize(&mut self) {
        self.spea2.initialize();
    }

    fn into_result(self) -> OptProblem<'a, C, N, V> {
        self.spea2.into_result()
    }

    fn terminate(&mut self) {
        self.spea2.terminate()
    }
}

#[derive(Clone, Debug)]
pub struct Spea2Builder<'a, C, CR, MA, MU, N, S, V> {
    archive_size: f64,
    gap: GeneticAlgorithmParams<CR, MA, MU, S>,
    op: Option<OptProblem<'a, C, N, V>>,
}

impl<'a, C, CR, MA, MU, N, S, V> Spea2Builder<'a, C, CR, MA, MU, N, S, V> {
    pub fn new(archive_size: f64, gap: GeneticAlgorithmParams<CR, MA, MU, S>) -> Self {
        Spea2Builder {
            archive_size,
            gap,
            op: None,
        }
    }
}

impl<'a, C, CR, MA, MU, N: 'a, S, V: 'a>
    OptSolverBuilder<'a, C, N, Spea2Adapter<'a, C, CR, MA, MU, N, S, V>, V>
    for Spea2Builder<'a, C, CR, MA, MU, N, S, V>
where
    C: Debug,
    CR: Clone + Debug,
    MA: Clone + Debug,
    MU: Clone + Debug,
    N: AddAssign + Debug + Float + Send + Sync,
    S: Clone + Debug,
    V: Copy + Debug + Send + Sync,
{
    fn build(self: Box<Self>) -> Spea2Adapter<'a, C, CR, MA, MU, N, S, V> {
        let this = *self;
        Spea2Adapter {
            spea2: Spea2::new(this.archive_size, this.gap, this.op.unwrap()),
        }
    }

    fn set_opt_problem(&mut self, op: OptProblem<'a, C, N, V>) {
        self.op = Some(op);
    }
}