Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve generational replacement and add proper comma replacement #199

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/components/replacement/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,50 @@ impl<P: SingleObjectiveProblem> Component<P> for MuPlusLambda {
}
}

/// Keeps the `max_population_size` fittest individuals from children.
#[derive(Clone, Serialize, Deserialize)]
pub struct MuCommaLambda {
/// Maximal allowed population size.
pub max_population_size: u32,
}

impl MuCommaLambda {
pub fn from_params(max_population_size: u32) -> Self {
Self {
max_population_size,
}
}

pub fn new<P: SingleObjectiveProblem>(max_population_size: u32) -> Box<dyn Component<P>> {
Box::new(Self::from_params(max_population_size))
}
}

impl<P: SingleObjectiveProblem> Replacement<P> for MuCommaLambda {
fn replace(
&self,
_parents: Vec<Individual<P>>,
offspring: Vec<Individual<P>>,
_rng: &mut Random,
) -> ExecResult<Vec<Individual<P>>> {
let mut population = offspring.clone();
population.sort_unstable_by_key(|i| *i.objective());
population.truncate(self.max_population_size as usize);
Ok(population)
}
}

impl<P: SingleObjectiveProblem> Component<P> for MuCommaLambda {
fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
replacement(self, problem, state)
}
}

/// Discards all individuals in the parent population, keeping the children unchanged.
///
/// The opposite of this is [`DiscardOffspring`].
///
/// If 'max_population_size' is given, the new population is simply truncated.
#[derive(Clone, Serialize, Deserialize)]
pub struct Generational {
/// Maximal allowed population size.
Expand All @@ -146,7 +187,13 @@ impl<P: Problem> Replacement<P> for Generational {
offspring: Vec<Individual<P>>,
_rng: &mut Random,
) -> ExecResult<Vec<Individual<P>>> {
Ok(offspring)
if offspring.len() > self.max_population_size as usize {
let mut population = offspring.clone();
population.truncate(self.max_population_size as usize);
Ok(population)
} else {
Ok(offspring)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/replacement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub mod common;
pub mod sa;

pub use common::{
DiscardOffspring, Generational, KeepBetterAtIndex, Merge, MuPlusLambda, RandomReplacement,
DiscardOffspring, Generational, KeepBetterAtIndex, Merge, MuCommaLambda, MuPlusLambda,
RandomReplacement,
};

/// Trait for representing a component that replaces a parent population with its child population.
Expand Down
Loading