Skip to content

Commit

Permalink
Keep state related items in their own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
luleyleo committed Jul 22, 2022
1 parent acf6a06 commit 58e2396
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 55 deletions.
24 changes: 13 additions & 11 deletions src/framework/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Framework components.
use crate::{
framework::{common_state, Fitness, State},
framework::{
state::{common, State},
Fitness,
},
problems::Problem,
};
use serde::Serialize;
Expand Down Expand Up @@ -118,7 +121,7 @@ where
P: Problem + 'static,
{
fn initialize(&self, problem: &P, state: &mut State) {
state.insert(common_state::Iterations(0));
state.insert(common::Iterations(0));

self.condition.initialize(problem, state);
self.body.initialize(problem, state);
Expand All @@ -128,7 +131,7 @@ where
self.condition.initialize(problem, state);
while self.condition.evaluate(problem, state) {
self.body.execute(problem, state);
*state.get_value_mut::<common_state::Iterations>() += 1;
*state.get_value_mut::<common::Iterations>() += 1;
}
}
}
Expand Down Expand Up @@ -211,10 +214,10 @@ impl SimpleEvaluator {

impl<P: Problem> Component<P> for SimpleEvaluator {
fn initialize(&self, _problem: &P, state: &mut State) {
state.require::<common_state::Population>();
state.insert(common_state::Evaluations(0));
state.insert(common_state::BestFitness(Fitness::default()));
state.insert(common_state::BestIndividual(None));
state.require::<common::Population>();
state.insert(common::Evaluations(0));
state.insert(common::BestFitness(Fitness::default()));
state.insert(common::BestIndividual(None));
}

fn execute(&self, problem: &P, state: &mut State) {
Expand All @@ -237,15 +240,14 @@ impl<P: Problem> Component<P> for SimpleEvaluator {
let best_individual = population.best();

if mut_state
.get_mut::<common_state::BestIndividual>()
.get_mut::<common::BestIndividual>()
.replace_if_better(best_individual)
{
mut_state.set_value::<common_state::BestFitness>(best_individual.fitness());
mut_state.set_value::<common::BestFitness>(best_individual.fitness());
}

// Update evaluations
*mut_state.get_value_mut::<common_state::Evaluations>() +=
population.current().len() as u32;
*mut_state.get_value_mut::<common::Evaluations>() += population.current().len() as u32;
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
#![doc = include_str!("../../docs/heuristic.md")]

pub mod components;
pub mod state;

mod configuration;
pub use configuration::{Configuration, ConfigurationBuilder};

mod fitness;
pub use fitness::{Fitness, IllegalFitness};

mod state;
pub use state::common as common_state;
pub use state::{CustomState, MultiStateTuple, MutState, State};

mod individual;
pub use individual::Individual;

Expand All @@ -26,12 +23,12 @@ pub fn run<P: Problem + 'static>(
problem: &P,
config: &Configuration<P>,
rng: Option<Random>,
) -> State {
let mut state = State::new_root();
) -> state::State {
let mut state = state::State::new_root();

state.insert(Log::new());
state.insert(rng.unwrap_or_default());
state.insert(common_state::Population::new());
state.insert(state::common::Population::new());

config.initialize(problem, &mut state);
config.execute(problem, &mut state);
Expand Down
2 changes: 1 addition & 1 deletion src/framework/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand::{RngCore, SeedableRng};
use serde::Serialize;
use std::any::type_name;

use crate::framework::CustomState;
use crate::framework::state::CustomState;

/// A random number generator.
///
Expand Down
2 changes: 1 addition & 1 deletion src/framework/state/many.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::{Deref, DerefMut};
use std::{any::TypeId, collections::HashSet};

use crate::framework::{CustomState, State};
use crate::framework::state::{CustomState, State};

/// Allows borrowing multiple [CustomState]'s mutable from [State] at the same time.
/// It is meant to significantly simplify the definition of [Component][crate::framework::components::Component]'s
Expand Down
2 changes: 1 addition & 1 deletion src/framework/state/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::BTreeMap,
};

use crate::framework::CustomState;
use crate::framework::state::CustomState;

/// Utility trait to upcast [CustomState](CustomState) to [Any].
pub trait AsAny: Any {
Expand Down
4 changes: 2 additions & 2 deletions src/heuristics/aco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use serde::Serialize;

use crate::{
framework::{components, Configuration, ConfigurationBuilder, CustomState},
framework::{components, state::CustomState, Configuration, ConfigurationBuilder},
operators::*,
problems::tsp::SymmetricTsp,
};
Expand Down Expand Up @@ -128,7 +128,7 @@ mod ant_ops {
use rand::distributions::{Distribution, WeightedIndex};

use crate::{
framework::{components::*, Fitness, Individual, Random, State},
framework::{components::*, state::State, Fitness, Individual, Random},
problems::tsp::{Route, SymmetricTsp},
};

Expand Down
2 changes: 1 addition & 1 deletion src/heuristics/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where
#[allow(clippy::new_ret_no_self)]
mod pso_ops {
use crate::{
framework::{components::*, Individual, State},
framework::{components::*, state::State, Individual},
operators::custom_state::PsoState,
problems::{LimitedVectorProblem, Problem},
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/archive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Archiving methods
use crate::{
framework::{components::*, State},
framework::{components::*, state::State},
operators::custom_state::ElitistArchiveState,
problems::Problem,
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/custom_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Custom states required for specific metaheuristics and evaluation procedures
use crate::framework::{CustomState, Fitness, Individual};
use crate::framework::{state::CustomState, Fitness, Individual};

// Custom States for Specific Metaheuristics //

Expand Down
5 changes: 3 additions & 2 deletions src/operators/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use crate::{
framework::{
components::{AnyComponent, Component},
Individual, State,
state::State,
Individual,
},
problems::{LimitedVectorProblem, Problem, VectorProblem},
};
Expand Down Expand Up @@ -159,7 +160,7 @@ pub mod swarm {
use rand::Rng;

use crate::{
framework::{components::*, Individual, Random, State},
framework::{components::*, state::State, Individual, Random},
operators::custom_state::PsoState,
problems::Problem,
};
Expand Down
5 changes: 4 additions & 1 deletion src/operators/generation/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use rand_distr::Distribution;
use serde::{Deserialize, Serialize};

use crate::{
framework::{common_state::Progress, components::*, State},
framework::{
components::*,
state::{common::Progress, State},
},
problems::{LimitedVectorProblem, Problem},
};

Expand Down
2 changes: 1 addition & 1 deletion src/operators/generation/recombination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rand::{seq::IteratorRandom, Rng};
use serde::{Deserialize, Serialize};

use crate::{
framework::{components::*, State},
framework::{components::*, state::State},
problems::Problem,
};

Expand Down
2 changes: 1 addition & 1 deletion src/operators/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rand::{distributions::uniform::SampleUniform, Rng};
use serde::{Deserialize, Serialize};

use crate::{
framework::{components::*, Individual, Random, State},
framework::{components::*, state::State, Individual, Random},
problems::{LimitedVectorProblem, Problem, VectorProblem},
};

Expand Down
2 changes: 1 addition & 1 deletion src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use serde::Serialize;

use crate::{
framework::{components::Component, State},
framework::{components::Component, state::State},
problems::Problem,
};

Expand Down
5 changes: 4 additions & 1 deletion src/operators/postprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//!
use crate::{
framework::{common_state::Population, components::*, State},
framework::{
components::*,
state::{common::Population, State},
},
operators::custom_state::DiversityState,
problems::{Problem, VectorProblem},
};
Expand Down
9 changes: 6 additions & 3 deletions src/operators/replacement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Replacement methods
use crate::framework::common_state::Population;
use crate::{
framework::{components::*, Individual, State},
framework::{
components::*,
state::{common::Population, State},
Individual,
},
problems::Problem,
};
use rand::seq::SliceRandom;
Expand Down Expand Up @@ -87,7 +90,7 @@ impl Replacement for MuPlusLambda {

#[cfg(test)]
mod mupluslambda {
use crate::framework::State;
use crate::framework::state::State;
use crate::operators::testing::{collect_population_fitness, new_test_population};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/operators/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::{
use serde::{Deserialize, Serialize};

use crate::{
framework::{components::*, Fitness, Individual, State},
framework::{components::*, state::State, Fitness, Individual},
problems::Problem,
};

Expand Down
7 changes: 5 additions & 2 deletions src/operators/termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
use crate::{
framework::{
common_state::{BestFitness, Evaluations, Iterations, Progress},
components::Condition,
Fitness, State,
state::{
common::{BestFitness, Evaluations, Iterations, Progress},
State,
},
Fitness,
},
operators::custom_state::FitnessImprovementState,
problems::{HasKnownOptimum, HasKnownTarget, Problem},
Expand Down
10 changes: 5 additions & 5 deletions src/tracking/functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
framework::{common_state, CustomState, State},
framework::state::{common, CustomState, State},
problems::Problem,
tracking::log::Entry,
};
Expand Down Expand Up @@ -28,19 +28,19 @@ where
P::Encoding: Clone + Serialize + Sized + 'static,
{
debug_assert!(
state.has::<common_state::BestIndividual>(),
state.has::<common::BestIndividual>(),
"missing state: {}",
type_name::<common_state::BestIndividual>()
type_name::<common::BestIndividual>()
);

let instance = state.get::<common_state::BestIndividual>();
let instance = state.get::<common::BestIndividual>();
let value = Box::new(if let Some(instance) = instance.deref() {
let individual = instance.solution::<P::Encoding>().clone();
Some(individual)
} else {
None
});

let name = std::any::type_name::<common_state::BestIndividual>();
let name = std::any::type_name::<common::BestIndividual>();
Entry { name, value }
}
4 changes: 2 additions & 2 deletions src/tracking/log.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::framework::{common_state, CustomState, State};
use crate::framework::state::{common, CustomState, State};
use erased_serde::Serialize as DynSerialize;
use serde::Serialize;
use std::any::type_name;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Step {
///
/// Will also ensure that the iteration is at index 0.
pub(crate) fn push_iteration(&mut self, state: &State) {
let name = type_name::<common_state::Iterations>();
let name = type_name::<common::Iterations>();

if !self.contains(name) {
let value = Box::new(state.iterations());
Expand Down
17 changes: 10 additions & 7 deletions src/tracking/logger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::ops::{Deref, Sub};

use crate::{
framework::{common_state, components::Component, CustomState, State},
framework::{
components::Component,
state::{common, CustomState, State},
},
problems::Problem,
tracking::{log::Step, set::LogSet, trigger, Log},
};
Expand Down Expand Up @@ -50,16 +53,16 @@ impl<P: Problem + 'static> Logger<P> {
/// Add the most common sets.
///
/// Currently encompases:
/// - [common_state::Evaluations]
/// - [common_state::Progress]
/// - [common_state::BestFitness]
/// - [common::Evaluations]
/// - [common::Progress]
/// - [common::BestFitness]
pub fn log_common_sets(self) -> Self {
self.log_set(
LogSet::new()
.with_trigger(trigger::Iteration::new(10))
.with_auto_logger::<common_state::Evaluations>()
.with_auto_logger::<common_state::Progress>()
.with_auto_logger::<common_state::BestFitness>(),
.with_auto_logger::<common::Evaluations>()
.with_auto_logger::<common::Progress>()
.with_auto_logger::<common::BestFitness>(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/tracking/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
framework::{CustomState, State},
framework::state::{CustomState, State},
problems::Problem,
tracking::{
functions::{self, LogFn},
Expand Down
2 changes: 1 addition & 1 deletion src/tracking/trigger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::framework::{common_state::Iterations, CustomState, State};
use crate::framework::state::{common::Iterations, CustomState, State};
use derive_deref::Deref;
use std::{
any::Any,
Expand Down

0 comments on commit 58e2396

Please sign in to comment.