diff --git a/src/lib.rs b/src/lib.rs index d288c43..f8d3e55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,4 @@ //! full design. pub mod core; +pub mod traits; diff --git a/src/traits/initializer.rs b/src/traits/initializer.rs new file mode 100644 index 0000000..2734f5e --- /dev/null +++ b/src/traits/initializer.rs @@ -0,0 +1,12 @@ +//! Trait for sampling initial decisions. + +use crate::core::rng::Rng; + +/// Generates initial decisions for a population. +/// +/// Implementations should return exactly `size` decisions; if that is +/// impossible, panic with a clear message in v1. +pub trait Initializer { + /// Produce `size` initial decisions using the supplied RNG. + fn initialize(&mut self, size: usize, rng: &mut Rng) -> Vec; +} diff --git a/src/traits/mod.rs b/src/traits/mod.rs new file mode 100644 index 0000000..faeb203 --- /dev/null +++ b/src/traits/mod.rs @@ -0,0 +1,9 @@ +//! The small set of traits that user code and built-in algorithms implement. + +pub mod initializer; +pub mod optimizer; +pub mod variation; + +pub use initializer::*; +pub use optimizer::*; +pub use variation::*; diff --git a/src/traits/optimizer.rs b/src/traits/optimizer.rs new file mode 100644 index 0000000..2a8ac76 --- /dev/null +++ b/src/traits/optimizer.rs @@ -0,0 +1,18 @@ +//! The single trait users implement to add a new optimizer. + +use crate::core::problem::Problem; +use crate::core::result::OptimizationResult; + +/// An optimizer that runs to completion in a single call. +/// +/// Implementations own their main loop, manage their own state, and return an +/// [`OptimizationResult`]. v1 deliberately does not expose a step-by-step API +/// or an associated error type — invalid configuration may panic with a clear +/// message. +pub trait Optimizer

+where + P: Problem, +{ + /// Run the optimizer to completion against `problem`. + fn run(&mut self, problem: &P) -> OptimizationResult; +} diff --git a/src/traits/variation.rs b/src/traits/variation.rs new file mode 100644 index 0000000..42530c7 --- /dev/null +++ b/src/traits/variation.rs @@ -0,0 +1,13 @@ +//! Trait for producing child decisions from parents. + +use crate::core::rng::Rng; + +/// Generates child decisions from parent decisions. +/// +/// The optimizer chooses how many parents to pass. Implementations may return +/// any number of children; algorithms that require a specific count should +/// panic with a clear message if they do not get it. +pub trait Variation { + /// Produce children from the given parents. + fn vary(&mut self, parents: &[D], rng: &mut Rng) -> Vec; +}