feat(traits): add Initializer, Variation, and Optimizer traits
The three operator-level traits the algorithms consume, plus the single trait users implement to add a new optimizer (`Optimizer<P>`). All take `&mut Rng` directly rather than being generic over the RNG (spec §7.8).
This commit is contained in:
@@ -3,3 +3,4 @@
|
|||||||
//! full design.
|
//! full design.
|
||||||
|
|
||||||
pub mod core;
|
pub mod core;
|
||||||
|
pub mod traits;
|
||||||
|
|||||||
@@ -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<D> {
|
||||||
|
/// Produce `size` initial decisions using the supplied RNG.
|
||||||
|
fn initialize(&mut self, size: usize, rng: &mut Rng) -> Vec<D>;
|
||||||
|
}
|
||||||
@@ -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::*;
|
||||||
@@ -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<P>
|
||||||
|
where
|
||||||
|
P: Problem,
|
||||||
|
{
|
||||||
|
/// Run the optimizer to completion against `problem`.
|
||||||
|
fn run(&mut self, problem: &P) -> OptimizationResult<P::Decision>;
|
||||||
|
}
|
||||||
@@ -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<D> {
|
||||||
|
/// Produce children from the given parents.
|
||||||
|
fn vary(&mut self, parents: &[D], rng: &mut Rng) -> Vec<D>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user