use super::{
super::utilities::{parameters::Parameters, Solution},
agent::{Agent, AgentMethods},
team::{Team, TeamMethods},
};
use crate::problems::Ackley;
use rayon::prelude::*;
use std::marker::PhantomData;
#[derive(Clone, Debug)]
pub struct Cohort<S = Ackley, A = Agent<S>, T = Team<S, A>>
where
S: Solution,
A: AgentMethods<S>,
T: TeamMethods<S, A>,
{
parameters: Parameters,
team_list: Vec<T>,
solution_type: PhantomData<S>,
agent_type: PhantomData<A>,
}
impl<S, A, T> Cohort<S, A, T>
where
S: Solution,
A: AgentMethods<S>,
T: TeamMethods<S, A>,
{
pub fn new(parameters: Parameters) -> Cohort<S, A, T> {
Cohort {
team_list: (0..parameters.number_of_teams)
.map(|_| T::new(parameters.clone()))
.collect(),
solution_type: Default::default(),
parameters,
agent_type: Default::default(),
}
}
pub fn solve(&mut self) {
self.team_list.par_iter_mut().for_each(|x| x.solve());
}
pub fn iterate(&mut self) {
self.team_list.iter_mut().for_each(|x| x.iterate());
}
pub fn get_best_solution_so_far(&mut self) -> f64 {
(0..self.parameters.number_of_teams)
.map(|i| self.team_list[i].get_best_solution_so_far())
.collect::<Vec<S>>()
.into_iter()
.max()
.unwrap()
.get_quality_scalar()
}
}
impl Default for Cohort {
fn default() -> Self {
Cohort::new(Default::default())
}
}