SolverManagerBuilder

Struct SolverManagerBuilder 

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

Builder for creating a SolverManager with fluent configuration.

The builder pattern allows configuring phases, termination conditions, and other solver settings before creating the manager.

§Basic Usage

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

#[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 manager = SolverManagerBuilder::new(|p: &Problem| SimpleScore::of(-p.value))
    .with_construction_heuristic()
    .with_local_search(LocalSearchType::HillClimbing)
    .with_time_limit(Duration::from_secs(30))
    .build()
    .expect("Failed to build manager");

§Configuration Options

The builder supports:

  • Construction heuristic phases (first fit, best fit)
  • Local search phases (hill climbing, tabu search, simulated annealing, late acceptance)
  • Time limits
  • Step limits

§Multi-Phase Configuration

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

let manager = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    // First phase: construct initial solution
    .with_construction_heuristic_type(ConstructionType::BestFit)
    // Second phase: improve with tabu search
    .with_local_search_steps(LocalSearchType::TabuSearch { tabu_size: 7 }, 1000)
    // Third phase: fine-tune with hill climbing
    .with_local_search(LocalSearchType::HillClimbing)
    // Global termination
    .with_time_limit(Duration::from_secs(60))
    .build()
    .unwrap();

§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.

Implementations§

Source§

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

Source

pub fn new(score_calculator: C) -> Self

Creates a new builder with the given score calculator (zero-erasure).

The score calculator is a function that computes the score for a solution. Higher scores are better (for minimization, use negative values).

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

// For minimization, negate the cost
let builder = SolverManagerBuilder::new(|p: &Problem| {
    SimpleScore::of(-p.cost)
});
Source

pub fn with_construction_heuristic(self) -> Self

Adds a construction heuristic phase with default (FirstFit) configuration.

This phase will build an initial solution by assigning values to uninitialized planning variables using the first valid value found.

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

let builder = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    .with_construction_heuristic();
Source

pub fn with_construction_heuristic_type( self, construction_type: ConstructionType, ) -> Self

Adds a construction heuristic phase with specific configuration.

Use this to choose between ConstructionType::FirstFit (fast) and ConstructionType::BestFit (better quality initial solution).

§Example
use solverforge_solver::manager::{SolverManagerBuilder, ConstructionType};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

let builder = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    .with_construction_heuristic_type(ConstructionType::BestFit);

Adds a local search phase.

Local search improves an existing solution by exploring neighboring solutions. The search type determines the acceptance criteria.

§Example
use solverforge_solver::manager::{SolverManagerBuilder, LocalSearchType};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

let builder = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    .with_local_search(LocalSearchType::TabuSearch { tabu_size: 7 });
Source

pub fn with_local_search_steps( self, search_type: LocalSearchType, step_limit: u64, ) -> Self

Adds a local search phase with a step limit.

The phase will terminate after the specified number of steps, allowing for multi-phase configurations where different search strategies are used in sequence.

§Example
use solverforge_solver::manager::{SolverManagerBuilder, LocalSearchType};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

let builder = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    // First, use simulated annealing for 500 steps
    .with_local_search_steps(
        LocalSearchType::SimulatedAnnealing {
            starting_temp: 1.0,
            decay_rate: 0.99,
        },
        500,
    )
    // Then switch to hill climbing
    .with_local_search(LocalSearchType::HillClimbing);
Source

pub fn with_time_limit(self, duration: Duration) -> Self

Sets the global time limit for solving.

The solver will terminate after this duration, regardless of which phase is currently executing.

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

let builder = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    .with_time_limit(Duration::from_secs(60));
Source

pub fn with_step_limit(self, steps: u64) -> Self

Sets the global step limit for solving.

The solver will terminate after this many steps total across all phases.

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

let builder = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    .with_step_limit(10000);
Source

pub fn build(self) -> Result<SolverManager<S, C>, SolverForgeError>

Builds the SolverManager.

This creates a basic SolverManager with the configured termination conditions. For full functionality with phases, use the typed phase factories from [super::phase_factory].

§Errors

Currently this method always succeeds, but returns a Result for forward compatibility with validation.

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

let manager = SolverManagerBuilder::new(|_: &Problem| SimpleScore::of(0))
    .with_construction_heuristic()
    .with_local_search(LocalSearchType::HillClimbing)
    .with_time_limit(Duration::from_secs(30))
    .build()
    .expect("Failed to build manager");

// Manager is ready to create solvers
let solver = manager.create_solver();

Auto Trait Implementations§

§

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

§

impl<S, C> RefUnwindSafe for SolverManagerBuilder<S, C>

§

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

§

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

§

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

§

impl<S, C> UnwindSafe for SolverManagerBuilder<S, C>
where C: UnwindSafe, S: UnwindSafe,

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