SolverManager

Struct SolverManager 

Source
pub struct SolverManager<S: PlanningSolution, C = fn(&S) -> <S as PlanningSolution>::Score>
where C: Fn(&S) -> S::Score + Send + Sync,
{ /* private fields */ }
Expand description

High-level solver manager for ergonomic solving.

SolverManager stores solver configuration and can create solvers on demand. For solving, use create_solver() to get a configured Solver instance, then provide your own ScoreDirector.

§Creating a SolverManager

Use the builder pattern via SolverManager::builder():

use solverforge_solver::manager::{SolverManager, LocalSearchType};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;
use std::time::Duration;

#[derive(Clone)]
struct Schedule {
    tasks: Vec<i64>,
    score: Option<SimpleScore>,
}

impl PlanningSolution for Schedule {
    type Score = SimpleScore;
    fn score(&self) -> Option<Self::Score> { self.score }
    fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
}

// Build a manager with hill climbing and 30-second time limit
let manager = SolverManager::<Schedule>::builder(|s| {
    // Simple scoring: sum of tasks
    SimpleScore::of(s.tasks.iter().sum())
})
    .with_time_limit(Duration::from_secs(30))
    .build()
    .expect("Failed to build manager");

// Score calculation is available without solving
let schedule = Schedule { tasks: vec![1, 2, 3], score: None };
let score = manager.calculate_score(&schedule);
assert_eq!(score, SimpleScore::of(6));

§Creating Solvers

The manager creates fresh Solver instances for each solve:

use solverforge_solver::manager::SolverManager;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

// Create a solver for this problem instance
let solver = manager.create_solver();

// Each call creates a fresh solver with clean state
let solver2 = manager.create_solver();

§Zero-Erasure Design

The score calculator is stored as a concrete generic type parameter C, not as Arc<dyn Fn>. This eliminates virtual dispatch overhead for the hot path (score calculation is called millions of times per solve).

The default C = fn(&S) -> S::Score allows writing SolverManager::<T>::builder(...) without specifying the calculator type (it’s inferred from the builder).

Implementations§

Source§

impl<S: PlanningSolution> SolverManager<S, fn(&S) -> S::Score>

Source

pub fn builder<F>(score_calculator: F) -> SolverManagerBuilder<S, F>
where F: Fn(&S) -> S::Score + Send + Sync + 'static,

Creates a new SolverManagerBuilder with the given score calculator.

The score calculator is a function that computes the score for a solution. This is the entry point for building a SolverManager.

§Example
use solverforge_solver::manager::SolverManager;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

#[derive(Clone)]
struct Problem { value: i64, score: Option<SimpleScore> }

impl PlanningSolution for Problem {
    type Score = SimpleScore;
    fn score(&self) -> Option<Self::Score> { self.score }
    fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
}

let builder = SolverManager::<Problem>::builder(|p| {
    SimpleScore::of(-p.value.abs()) // Minimize absolute value
});
Source§

impl<S, C> SolverManager<S, C>
where S: PlanningSolution, C: Fn(&S) -> S::Score + Send + Sync,

Source

pub fn create_solver(&self) -> Solver<S>

Creates a fresh Solver instance with configured phases.

Each call returns a new solver with clean state, suitable for solving a new problem instance. The solver is configured with termination conditions and phases from this manager.

§Example
use solverforge_solver::manager::SolverManager;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;
use std::time::Duration;

let manager = SolverManager::<Problem>::builder(|_| SimpleScore::of(0))
    .with_step_limit(100)
    .build()
    .unwrap();

// Create solver - each call gives a fresh instance
let solver = manager.create_solver();
Source

pub fn score_calculator(&self) -> &C

Returns a reference to the score calculator function.

§Example
use solverforge_solver::manager::SolverManager;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

let manager = SolverManager::<Problem>::builder(|p| SimpleScore::of(p.value))
    .build()
    .unwrap();

let calculator = manager.score_calculator();
let problem = Problem { value: 42, score: None };
let score = calculator(&problem);
assert_eq!(score, SimpleScore::of(42));
Source

pub fn calculate_score(&self, solution: &S) -> S::Score

Calculates the score for a solution using the configured calculator.

This is a convenience method equivalent to calling the score calculator directly.

§Example
use solverforge_solver::manager::SolverManager;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

let manager = SolverManager::<Problem>::builder(|p| {
    SimpleScore::of(-p.value) // Negate for minimization
})
    .build()
    .unwrap();

let problem = Problem { value: 10, score: None };
let score = manager.calculate_score(&problem);
assert_eq!(score, SimpleScore::of(-10));

Auto Trait Implementations§

§

impl<S, C> Freeze for SolverManager<S, C>
where C: Freeze,

§

impl<S, C = fn(&S) -> <S as PlanningSolution>::Score> !RefUnwindSafe for SolverManager<S, C>

§

impl<S, C> Send for SolverManager<S, C>

§

impl<S, C> Sync for SolverManager<S, C>

§

impl<S, C> Unpin for SolverManager<S, C>
where C: Unpin,

§

impl<S, C = fn(&S) -> <S as PlanningSolution>::Score> !UnwindSafe for SolverManager<S, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more