pub struct RankBasedSelection { /* private fields */ }Expand description
A selection strategy that selects individuals based on their rank in the population.
Rank-based selection assigns a selection probability to each individual based on its rank in the population, rather than its absolute fitness value. This helps prevent premature convergence when there are a few individuals with much higher fitness than the rest of the population.
This strategy works well for both maximization and minimization problems, and can handle negative fitness values.
§Examples
use genalg::selection::rank::RankBasedSelection;
use genalg::selection::SelectionStrategy;
use genalg::phenotype::Phenotype;
use genalg::rng::RandomNumberGenerator;
use genalg::error::Result;
#[derive(Clone, Debug)]
struct MyPhenotype {
value: f64,
}
impl Phenotype for MyPhenotype {
fn crossover(&mut self, other: &Self) {
self.value = (self.value + other.value) / 2.0;
}
fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {
self.value += 0.1;
}
}
fn main() -> Result<()> {
let population = vec![
MyPhenotype { value: 1.0 },
MyPhenotype { value: 2.0 },
MyPhenotype { value: 3.0 },
MyPhenotype { value: 4.0 },
MyPhenotype { value: 5.0 },
];
let fitness = vec![0.5, 0.8, 0.3, 0.9, 0.1];
// Create a rank-based selection with default parameters
let selection = RankBasedSelection::new(1.5, false, true)?;
let selected = selection.select(&population, &fitness, 3)?;
assert_eq!(selected.len(), 3);
Ok(())
}Implementations§
Source§impl RankBasedSelection
impl RankBasedSelection
Sourcepub fn new(
selection_pressure: f64,
allow_duplicates: bool,
higher_is_better: bool,
) -> Result<Self>
pub fn new( selection_pressure: f64, allow_duplicates: bool, higher_is_better: bool, ) -> Result<Self>
Creates a new RankBasedSelection strategy with default parameters.
By default:
- Selection pressure is set to 1.5 (a balanced middle ground between 1.0 and 2.0)
- Duplicates are not allowed in the selected individuals
- Higher fitness is considered better
Sourcepub fn with_pressure(self, selection_pressure: f64) -> Result<Self>
pub fn with_pressure(self, selection_pressure: f64) -> Result<Self>
Creates a new RankBasedSelection strategy with the specified selection pressure.
§Arguments
selection_pressure- The selection pressure parameter. Must be in the range [1.0, 2.0]. Higher values increase selection pressure.- At 1.0, all individuals have equal selection probability (no selection pressure)
- At 2.0, selection pressure is at its maximum
- The default value of 1.5 provides a balanced selection pressure
§Returns
A Result containing the RankBasedSelection instance, or an error if the selection pressure is outside the valid range.
§Errors
Returns a GeneticError::Configuration error if selection_pressure is not in the range [1.0, 2.0].
pub fn with_duplicates(self) -> Self
pub fn with_lower_is_better(self) -> Self
Trait Implementations§
Source§impl Clone for RankBasedSelection
impl Clone for RankBasedSelection
Source§fn clone(&self) -> RankBasedSelection
fn clone(&self) -> RankBasedSelection
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RankBasedSelection
impl Debug for RankBasedSelection
Source§impl Default for RankBasedSelection
impl Default for RankBasedSelection
Auto Trait Implementations§
impl Freeze for RankBasedSelection
impl RefUnwindSafe for RankBasedSelection
impl Send for RankBasedSelection
impl Sync for RankBasedSelection
impl Unpin for RankBasedSelection
impl UnwindSafe for RankBasedSelection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more