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

Add behaviour measures #197

Merged
merged 28 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
67862b0
Add measure variations for step size
HeleNoir Oct 19, 2023
0254631
Add variance to step size measures
HeleNoir Nov 8, 2023
5757b50
Add improvement measure and population lenses
HeleNoir Nov 24, 2023
71576ab
Fix improvement measure
HeleNoir Dec 4, 2023
c553016
Add contribution guide
Saethox Sep 7, 2023
9c7bd17
Fix formatting
Saethox Sep 7, 2023
6eadd36
Implement firefly algorithm
HeleNoir Aug 31, 2023
739889c
Rebase
HeleNoir Sep 14, 2023
44a3594
Add suggestions and make mahf compile again
HeleNoir Sep 19, 2023
68ffab7
Simplify FA
HeleNoir Sep 27, 2023
6154697
Try to accomplish evaluation
HeleNoir Sep 28, 2023
5799786
Fix FA and improve black hole algorithm
HeleNoir Sep 29, 2023
46b0b3b
Fix black hole algorithm
HeleNoir Oct 12, 2023
c905af7
Fix formatting
HeleNoir Oct 12, 2023
961d3bf
Fix documentation
HeleNoir Oct 12, 2023
78bb32e
Fix formatting
HeleNoir Oct 13, 2023
a88249a
Improve according to review comments
HeleNoir Dec 4, 2023
6efd78f
Fix fmt
HeleNoir Dec 4, 2023
31de8da
Fix format and include last comment
HeleNoir Dec 4, 2023
6bb3047
Add variance to step size measures
HeleNoir Nov 8, 2023
106990a
Merge branch 'master' into add-behaviour-measures
HeleNoir Dec 7, 2023
82e0ccd
Add note to improvement measure
HeleNoir Jan 10, 2024
29c02a8
Add convergence behaviour measures
HeleNoir May 23, 2024
d4f45e9
Cleanup and format
HeleNoir May 24, 2024
4a042da
Update Rust version for CI
HeleNoir May 28, 2024
d000da9
Fix fmt and clippy
HeleNoir May 28, 2024
dbda6b5
Fix fmt again
HeleNoir May 28, 2024
27061da
Allow non_canonical_clone_impl for clippy in utils
HeleNoir May 28, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- master

env:
RUST_VERSION: 1.70.0
RUST_VERSION: 1.78.0
CARGO_TERM_COLOR: always

jobs:
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ test-case = "3.1.0"
float_eq = "1.0.1"
contracts = "0.6.3"
itertools = "0.10.5"
ron = "0.8.0"
ron = "=0.8.0"
indicatif = { version = "0.17.4", features = ["rayon"] }
statrs = "0.16"
Saethox marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
criterion = "0.5.1"
Expand Down
130 changes: 128 additions & 2 deletions src/components/archive.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Elitist archive.
//! Archive for specified parts of population.

use std::cell::Ref;

use better_any::{Tid, TidAble};
use serde::{Deserialize, Serialize};

use crate::{
component::ExecResult, components::Component, problems::SingleObjectiveProblem,
state::StateReq, CustomState, Individual, State,
state::StateReq, CustomState, Individual, Problem, State,
};

/// An archive for storing elitist individuals.
Expand Down Expand Up @@ -118,3 +120,127 @@ where
Ok(())
}
}

/// An archive for storing individuals between operators, e.g. for subsequent calculation of measures.
#[derive(Default, Tid)]
pub struct IntermediateArchive<P: Problem + 'static>(Vec<Individual<P>>);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would maybe be nicer to add a Archive<I: Identifier> struct that is used by all archive components that require a Vec<Individual>, but I think this is fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remember this for a later improvement PR.

impl<P: Problem> CustomState<'_> for IntermediateArchive<P> {}

impl<P: Problem> IntermediateArchive<P> {
/// Creates a new, empty `IntermediateArchive`.
fn new() -> Self {
Self(Vec::new())
}

/// Updates the archive using the `population`, keeping all individuals at the current step of the algorithm.
fn update(&mut self, population: &[Individual<P>]) {
self.0 = Vec::from(population);
}

/// Returns a reference to the archived population.
pub fn archived_population(&self) -> &[Individual<P>] {
&self.0
}

/// Returns a mutable reference to the archived population.
pub fn archived_population_mut(&mut self) -> &mut [Individual<P>] {
&mut self.0
}
}

/// Updates the [`IntermediateArchive`] with the current population.
#[derive(Clone, Serialize, Deserialize)]
pub struct IntermediateArchiveUpdate;

impl IntermediateArchiveUpdate {
pub fn from_params() -> Self {
Self {}
}

pub fn new<P>() -> Box<dyn Component<P>>
where
P: Problem,
{
Box::new(Self::from_params())
}
}

impl<P> Component<P> for IntermediateArchiveUpdate
where
P: Problem,
{
fn init(&self, _problem: &P, state: &mut State<P>) -> ExecResult<()> {
state.insert(IntermediateArchive::<P>::new());
Ok(())
}

fn execute(&self, _problem: &P, state: &mut State<P>) -> ExecResult<()> {
state
.borrow_mut::<IntermediateArchive<P>>()
.update(state.populations().current());
Ok(())
}
}

/// An archive for storing all best individual yet, e.g. for subsequent calculation of measures.
#[derive(Default, Tid)]
pub struct BestIndividualsArchive<P: Problem + 'static>(Vec<Individual<P>>);

impl<P: Problem> CustomState<'_> for BestIndividualsArchive<P> {}

impl<P: Problem> BestIndividualsArchive<P> {
/// Creates a new, empty `BestIndividualsArchive`.
fn new() -> Self {
Self(Vec::new())
}

/// Updates the archive using the `BestIndividual`, adding it to a vector of previously found best individuals.
fn update(&mut self, best_individual: Option<Ref<Individual<P>>>) {
self.0.push(best_individual.unwrap().clone());
}

/// Returns a reference to the archived individuals.
pub fn archived_best_individuals(&self) -> &[Individual<P>] {
&self.0
}

/// Returns a mutable reference to the archived individuals.
pub fn archived_best_individuals_mut(&mut self) -> &mut [Individual<P>] {
&mut self.0
}
}

/// Updates the [`BestIndividualsArchive`] with the current best individual.
#[derive(Clone, Serialize, Deserialize)]
pub struct BestIndividualsArchiveUpdate;

impl BestIndividualsArchiveUpdate {
pub fn from_params() -> Self {
Self {}
}

pub fn new<P>() -> Box<dyn Component<P>>
where
P: Problem + SingleObjectiveProblem,
{
Box::new(Self::from_params())
}
}

impl<P> Component<P> for BestIndividualsArchiveUpdate
where
P: Problem + SingleObjectiveProblem,
{
fn init(&self, _problem: &P, state: &mut State<P>) -> ExecResult<()> {
state.insert(BestIndividualsArchive::<P>::new());
Ok(())
}

fn execute(&self, _problem: &P, state: &mut State<P>) -> ExecResult<()> {
state
.borrow_mut::<BestIndividualsArchive<P>>()
.update(state.best_individual());
Ok(())
}
}
Loading
Loading