EngineExt

Trait EngineExt 

Source
pub trait EngineExt<E: Engine> {
    // Required method
    fn run<F>(&mut self, limit: F) -> E::Epoch
       where F: Fn(&E::Epoch) -> bool;
}
Expand description

Extension trait providing convenient methods for running engines with custom logic.

EngineExt provides additional functionality for engines without requiring changes to the core Engine trait. This follows the Rust pattern of using extension traits to add functionality to existing types.

§Generic Parameters

  • E: The engine type that this extension applies to

§Design Benefits

  • Separation of Concerns: Core engine logic is separate from execution control
  • Flexibility: Different termination conditions can be easily implemented
  • Reusability: The same engine can be run with different stopping criteria
  • Testability: Termination logic can be tested independently of engine logic

Required Methods§

Source

fn run<F>(&mut self, limit: F) -> E::Epoch
where F: Fn(&E::Epoch) -> bool,

Runs the engine until the specified termination condition is met.

This method continuously calls engine.next() until the provided closure returns true, indicating that the termination condition has been satisfied. The final epoch is returned, allowing you to inspect the final state of the evolutionary process.

§Arguments
  • limit - A closure that takes the current epoch and returns true when the engine should stop, false to continue
§Returns

The epoch that satisfied the termination condition

§Termination Conditions

Common termination conditions include:

  • Generation Limit: Stop after a fixed number of generations
  • Fitness Threshold: Stop when best fitness reaches a target value
  • Convergence: Stop when population diversity or fitness improvement is minimal
  • Time Limit: Stop after a certain amount of computation time
  • Solution Quality: Stop when a satisfactory solution is found
§Performance Considerations
  • The termination condition is checked after every epoch, so keep it lightweight
  • Avoid expensive computations in the termination closure
  • Consider using early termination for conditions that can be checked incrementally
§Infinite Loops

Be careful to ensure that your termination condition will eventually be met, especially when using complex logic. An infinite loop will cause the program to hang indefinitely.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<E> EngineExt<E> for E
where E: Engine,

Blanket implementation of EngineExt for all types that implement Engine.

This implementation provides the run method to any type that implements the Engine trait, without requiring manual implementation.

§Implementation Details

The run method implements a simple loop that:

  1. Calls self.next() to advance the engine
  2. Checks the termination condition using the provided closure
  3. Breaks and returns the final epoch when the condition is met