Skip to content

Commit

Permalink
feat!: allow for using adaptive values with operators by introducing …
Browse files Browse the repository at this point in the history
…ValueProvider trait (#482)

- **Add ValueProvider trait**
- **Migrate RouletteWheel**
- **Migrate Random**
- **Migrate Rank**
- **Migrate RankR**
- **Migrate Tournament**
- **Migrate SUS**
- **Migrate Boltzmann**
- **Update docs**
- **Fix tests in selection impls module**
- **Fix leftovers**

<!-- If applicable - remember to add the PR to the EA Rust project (ONLY
IF THERE IS NO LINKED ISSUE) -->

## Description

## Linked issues <!-- Please use "Resolves #<issue_no> syntax in case
this PR should be linked to an issue -->

Closes #193 

## Important implementation details <!-- if any, optional section -->
  • Loading branch information
kkafar authored May 4, 2024
1 parent c6b1bb3 commit 78b8de6
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 151 deletions.
4 changes: 2 additions & 2 deletions coco/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn ecrs_ga_search(problem: &mut Problem, _max_budget: usize, _random_generator:
RealValueIndividual,
Reversing,
Uniform,
Tournament,
Tournament<usize>,
WeakParent,
RandomPoints,
adapter::CocoFitness,
Expand All @@ -118,7 +118,7 @@ fn ecrs_ga_search(problem: &mut Problem, _max_budget: usize, _random_generator:
dimension,
constraints,
))
.set_selection_operator(selection::Tournament::new(0.2))
.set_selection_operator(selection::Tournament::new(0.2, population_size))
.set_crossover_operator(crossover::Uniform::new())
.set_mutation_operator(mutation::Reversing::new(0.05))
.set_replacement_operator(replacement::WeakParent::new())
Expand Down
6 changes: 4 additions & 2 deletions examples/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
RealValueIndividual,
Identity,
SinglePoint,
Boltzmann,
Boltzmann<usize>,
WeakParent,
RandomPoints,
FnBasedFitness<RealValueIndividual>,
Expand All @@ -36,7 +36,9 @@ fn main() {
3,
vec![-5.12..5.12, -5.12..5.12, -5.12..5.12],
))
.set_selection_operator(ga::operators::selection::Boltzmann::new(0.05, 80.0, 500, false))
.set_selection_operator(ga::operators::selection::Boltzmann::new(
100, 0.05, 80.0, 500, false,
))
.set_probe(
ga::probe::AggregatedProbe::new()
.add_probe(ga::probe::PolicyDrivenProbe::new(
Expand Down
2 changes: 1 addition & 1 deletion examples/jssp/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn run_jssp_solver(instance: JsspInstance, config: Config) {
// }

ga::Builder::new()
.set_selection_operator(selection::Rank::new())
.set_selection_operator(selection::Rank::new(pop_size))
.set_crossover_operator(JsspCrossover::new())
.set_mutation_operator(mutation::Identity::new())
.set_population_generator(JsspPopProvider::new(instance.clone()))
Expand Down
1 change: 0 additions & 1 deletion examples/jssp/problem/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ impl SelectionOperator<JsspIndividual> for EmptySelection {
&mut self,
_metrics: &ecrs::ga::Metrics,
_population: &'a [JsspIndividual],
_count: usize,
) -> Vec<&'a JsspIndividual> {
Vec::new()
}
Expand Down
9 changes: 4 additions & 5 deletions src/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
//! ga::individual::RealValueIndividual,
//! mutation::Identity,
//! crossover::SinglePoint,
//! selection::Boltzmann,
//! selection::Boltzmann<usize>,
//! replacement::WeakParent,
//! population::RandomPoints,
//! fitness::FnBasedFitness<ga::individual::RealValueIndividual>,
Expand All @@ -81,7 +81,7 @@
//! 3,
//! vec![-5.12..5.12, -5.12..5.12, -5.12..5.12],
//! ))
//! .set_selection_operator(ga::operators::selection::Boltzmann::new(0.05, 80.0, 500, false))
//! .set_selection_operator(ga::operators::selection::Boltzmann::new(100, 0.05, 80.0, 500, false))
//! .set_probe(
//! ga::probe::AggregatedProbe::new()
//! .add_probe(ga::probe::PolicyDrivenProbe::new(
Expand Down Expand Up @@ -122,6 +122,7 @@ pub mod operators;
pub mod population;
pub mod probe;
pub(crate) mod timer;
pub mod value_provider;

use crate::ga::operators::fitness::Fitness;
pub use builder::*;
Expand Down Expand Up @@ -308,9 +309,7 @@ where
// 4. Create mating pool by applying selection operator.
self.timer.start();
let mating_pool: Vec<&IndividualT> =
self.config
.selection_operator
.apply(&self.metrics, &population, population.len());
self.config.selection_operator.apply(&self.metrics, &population);
self.metrics.selection_dur = Some(self.timer.elapsed());

// 5. From mating pool create new generation (apply crossover & mutation).
Expand Down
6 changes: 3 additions & 3 deletions src/ga/builder/bitstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct BitStringBuilder<F: Fitness<BitStringIndividual>> {
Individual<Bsc>,
FlipBit<rand::rngs::ThreadRng>,
SinglePoint<rand::rngs::ThreadRng>,
Tournament<rand::rngs::ThreadRng>,
Tournament<usize, rand::rngs::ThreadRng>,
BothParents,
BitStrings<rand::rngs::ThreadRng>,
F,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<F: Fitness<BitStringIndividual>> BitStringBuilder<F> {
Individual<Bsc>,
FlipBit<rand::rngs::ThreadRng>,
SinglePoint<rand::rngs::ThreadRng>,
Tournament<rand::rngs::ThreadRng>,
Tournament<usize, rand::rngs::ThreadRng>,
BothParents,
BitStrings<rand::rngs::ThreadRng>,
F,
Expand All @@ -150,7 +150,7 @@ impl<F: Fitness<BitStringIndividual>> BitStringBuilder<F> {
.get_or_insert_with(|| FlipBit::new(0.05));
self.config
.selection_operator
.get_or_insert_with(|| Tournament::new(0.2));
.get_or_insert_with(|| Tournament::new(0.2, self.config.params.population_size.unwrap()));
self.config
.replacement_operator
.get_or_insert_with(BothParents::new);
Expand Down
6 changes: 3 additions & 3 deletions src/ga/builder/realvalued.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct RealValuedBuilder<F: Fitness<RealValueIndividual>> {
Individual<Rvc>,
Interchange<rand::rngs::ThreadRng>,
SinglePoint<rand::rngs::ThreadRng>,
Tournament<rand::rngs::ThreadRng>,
Tournament<usize, rand::rngs::ThreadRng>,
BothParents,
RandomPoints<rand::rngs::ThreadRng>,
F,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<F: Fitness<RealValueIndividual>> RealValuedBuilder<F> {
Individual<Rvc>,
Interchange<rand::rngs::ThreadRng>,
SinglePoint<rand::rngs::ThreadRng>,
Tournament<rand::rngs::ThreadRng>,
Tournament<usize, rand::rngs::ThreadRng>,
BothParents,
RandomPoints<rand::rngs::ThreadRng>,
F,
Expand All @@ -149,7 +149,7 @@ impl<F: Fitness<RealValueIndividual>> RealValuedBuilder<F> {
.get_or_insert_with(|| Interchange::new(0.05));
self.config
.selection_operator
.get_or_insert_with(|| Tournament::new(0.2));
.get_or_insert_with(|| Tournament::new(0.2, self.config.params.population_size.unwrap()));
self.config
.replacement_operator
.get_or_insert_with(BothParents::new);
Expand Down
8 changes: 1 addition & 7 deletions src/ga/operators/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,5 @@ pub trait SelectionOperator<IndividualT: IndividualTrait> {
///
/// * `metrics` - [crate::ga::Metrics] information on current stage of the algorithm (iteration, elapsed time, etc.)
/// * `population` - individuals to choose mating pool from
/// * `count` - target number of individuals in mating pool
fn apply<'a>(
&mut self,
metrics: &Metrics,
population: &'a [IndividualT],
count: usize,
) -> Vec<&'a IndividualT>;
fn apply<'a>(&mut self, metrics: &Metrics, population: &'a [IndividualT]) -> Vec<&'a IndividualT>;
}
Loading

0 comments on commit 78b8de6

Please sign in to comment.