pub struct SolverManagerBuilder<S, C>{ /* 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>
impl<S, C> SolverManagerBuilder<S, C>
Sourcepub fn new(score_calculator: C) -> Self
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)
});Sourcepub fn with_construction_heuristic(self) -> Self
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();Sourcepub fn with_construction_heuristic_type(
self,
construction_type: ConstructionType,
) -> Self
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);Sourcepub fn with_local_search(self, search_type: LocalSearchType) -> Self
pub fn with_local_search(self, search_type: LocalSearchType) -> Self
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 });Sourcepub fn with_local_search_steps(
self,
search_type: LocalSearchType,
step_limit: u64,
) -> Self
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);Sourcepub fn with_time_limit(self, duration: Duration) -> Self
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));Sourcepub fn with_step_limit(self, steps: u64) -> Self
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);Sourcepub fn build(self) -> Result<SolverManager<S, C>, SolverForgeError>
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();