Skip to content

Commit

Permalink
Merge pull request #197 from mahf-opt/add-behaviour-measures
Browse files Browse the repository at this point in the history
Add behaviour measures
  • Loading branch information
HeleNoir authored May 28, 2024
2 parents 30a1274 + 27061da commit 3a4ef1e
Show file tree
Hide file tree
Showing 13 changed files with 1,315 additions and 12 deletions.
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"

[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>>);

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

0 comments on commit 3a4ef1e

Please sign in to comment.