pub struct Configuration<P: Problem>(/* private fields */);Expand description
A (meta)heuristic configuration.
A grouping of components is called a metaheuristic configuration, and the Configuration struct
provides useful abstractions for dealing with them.
A configuration therefore fully specifies a metaheuristic algorithm, with all its components and parameters.
§Application
Applying a Configuration to a problem is as simple as calling its optimize method with
the problem and an evaluator as argument.
The method returns the State after execution, enabling inspection of any custom state,
including the final population, or the best individual found.
It is also possible to pre-initialize the State before execution using the optimize_with
method, e.g. to set the random number seed or customize what state should be logged at runtime.
§Building Configurations
Because an object-oriented approach to specifying control flow is not very intuitive,
Configuration also exposes a simple and idiomatic way to construct metaheuristics
through Rust’s builder pattern.
See Configuration::builder for more information.
§Serialization
For the purpose of easily identifying which experiment was done with which components and parameters, it is serializable (but not deserializable).
Implementations§
Source§impl<P: Problem> Configuration<P>
impl<P: Problem> Configuration<P>
Sourcepub fn new(heuristic: Box<dyn Component<P>>) -> Self
pub fn new(heuristic: Box<dyn Component<P>>) -> Self
Constructs a Configuration from some heuristic components.
Use Configuration::builder for a more convenient construction.
Sourcepub fn builder() -> ConfigurationBuilder<P>
pub fn builder() -> ConfigurationBuilder<P>
Creates a builder for constructing a Configuration.
The builder exposes familiar control flow methods like do_, while_, if_,
and if_else_ along with shortcut methods to construct other often-used components.
§Examples
Constructing a simple genetic algorithm using the builder syntax:
use mahf::prelude::*;
let ga = Configuration::builder()
.do_(initialization::RandomSpread::new(population_size))
.evaluate()
.update_best_individual()
.while_(conditions::LessThanN::iterations(n), |builder| {
builder
.do_(selection::Tournament::new(num_selected, size))
.do_(recombination::ArithmeticCrossover::new_insert_both(1.))
.do_(mutation::NormalMutation::new(std_dev, rm))
.evaluate()
.update_best_individual()
.do_(replacement::MuPlusLambda::new(max_population_size))
})
.build();Sourcepub fn into_inner(self) -> Box<dyn Component<P>>
pub fn into_inner(self) -> Box<dyn Component<P>>
Consumes the Configuration, returning the root Component.
Sourcepub fn into_builder(self) -> ConfigurationBuilder<P>
pub fn into_builder(self) -> ConfigurationBuilder<P>
Creates a builder pre-initialized with the root Component.
Sourcepub fn run(&self, problem: &P, state: &mut State<'_, P>) -> ExecResult<()>
pub fn run(&self, problem: &P, state: &mut State<'_, P>) -> ExecResult<()>
Runs the Configuration on the problem using a given State.
Note that the caller is responsible for initializing state properly.
Sourcepub fn optimize<'a, T>(
&self,
problem: &P,
evaluator: T,
) -> ExecResult<State<'a, P>>where
T: Evaluate<Problem = P> + 'a,
pub fn optimize<'a, T>(
&self,
problem: &P,
evaluator: T,
) -> ExecResult<State<'a, P>>where
T: Evaluate<Problem = P> + 'a,
Runs the heuristic Configuration on the given problem, returning the final State
after execution of the heuristic.
§Initialization
The state is pre-initialized with Populations and Log.
The random generator defaults to a randomly seeded RNG (Random::default).
The evaluator is inserted wrapped inside an Evaluator with the Global identifier.
For initializing the state with custom state, e.g. a fixed random seed,
see optimize_with.
§Examples
Optimizing some problem with a sequential evaluator:
use mahf::{problems::Sequential, Configuration};
let config = Configuration::builder()
/* configuration definition */
.build();
let state = config.optimize(&problem, Sequential::new());Sourcepub fn optimize_with<'a>(
&self,
problem: &P,
init_state: impl FnOnce(&mut State<'a, P>) -> ExecResult<()>,
) -> ExecResult<State<'a, P>>
pub fn optimize_with<'a>( &self, problem: &P, init_state: impl FnOnce(&mut State<'a, P>) -> ExecResult<()>, ) -> ExecResult<State<'a, P>>
Runs the heuristic Configuration on the given problem, initializing the State
beforehand with a custom function, returning the final State after execution
of the heuristic.
§Initialization
The state is pre-initialized with Populations and Log.
If no random generator is inserted in init_state, it will default
to a randomly seeded RNG (Random::default).
Note that the evaluator has to be inserted manually into the State, using e.g. {State::insert_evaluator, State::insert_evaluator_as}.
§Examples
Optimizing some problem with a sequential evaluator and a random seed of 42:
use mahf::{identifier::Global, problems::Sequential, Configuration, Random};
let config = Configuration::builder()/* ... */.build();
let state = config.optimize_with(&problem, |state| {
state.insert_evaluator(Sequential::new());
state.insert(Random::new(42));
Ok(())
});Trait Implementations§
Source§impl<P: Clone + Problem> Clone for Configuration<P>
impl<P: Clone + Problem> Clone for Configuration<P>
Source§fn clone(&self) -> Configuration<P>
fn clone(&self) -> Configuration<P>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<P> Freeze for Configuration<P>
impl<P> !RefUnwindSafe for Configuration<P>
impl<P> Send for Configuration<P>
impl<P> Sync for Configuration<P>
impl<P> Unpin for Configuration<P>
impl<P> !UnwindSafe for Configuration<P>
Blanket Implementations§
Source§impl<T> AnyExt for T
impl<T> AnyExt for T
Source§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T behind referenceSource§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T behind mutable referenceSource§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T behind Rc pointerSource§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T behind Arc pointerSource§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 moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more