ConstructionPhaseFactory

Struct ConstructionPhaseFactory 

Source
pub struct ConstructionPhaseFactory<S, M, F>
where S: PlanningSolution, M: Move<S> + Clone + Send + Sync + 'static, F: Fn() -> Box<dyn EntityPlacer<S, M>> + Send + Sync,
{ /* private fields */ }
Expand description

Factory for creating construction heuristic phases.

Construction heuristic phases build an initial solution by assigning values to uninitialized planning variables. The factory provides fresh phase instances for each solve.

§Type Parameters

  • S - The planning solution type
  • M - The move type (typically ChangeMove)
  • F - The closure type that creates entity placers

§Example

use solverforge_solver::manager::{ConstructionPhaseFactory, SolverPhaseFactory};
use solverforge_solver::heuristic::r#move::ChangeMove;
use solverforge_solver::heuristic::selector::{
    FromSolutionEntitySelector, StaticTypedValueSelector,
};
use solverforge_solver::phase::construction::QueuedEntityPlacer;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

#[derive(Clone)]
struct Sol { values: Vec<Option<i32>>, score: Option<SimpleScore> }
impl PlanningSolution for Sol {
    type Score = SimpleScore;
    fn score(&self) -> Option<Self::Score> { self.score }
    fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
}

fn get_v(s: &Sol, idx: usize) -> Option<i32> { s.values.get(idx).copied().flatten() }
fn set_v(s: &mut Sol, idx: usize, v: Option<i32>) { if let Some(x) = s.values.get_mut(idx) { *x = v; } }

type M = ChangeMove<Sol, i32>;

// Create a first-fit construction phase factory
let factory = ConstructionPhaseFactory::<Sol, M, _>::first_fit(|| {
    let entity_sel = Box::new(FromSolutionEntitySelector::new(0));
    let value_sel = Box::new(StaticTypedValueSelector::new(vec![1, 2, 3]));
    Box::new(QueuedEntityPlacer::new(entity_sel, value_sel, get_v, set_v, 0, "value"))
});

// Create phase when solving
let phase = factory.create_phase();
assert_eq!(phase.phase_type_name(), "ConstructionHeuristic");

§Forager Types

Implementations§

Source§

impl<S, M, F> ConstructionPhaseFactory<S, M, F>
where S: PlanningSolution, M: Move<S> + Clone + Send + Sync + 'static, F: Fn() -> Box<dyn EntityPlacer<S, M>> + Send + Sync,

Source

pub fn new(forager_type: ForagerType, placer_factory: F) -> Self

Creates a new construction phase factory with the specified forager type.

§Arguments
  • forager_type - The type of forager to use (FirstFit or BestFit)
  • placer_factory - A closure that creates entity placers
§Example
use solverforge_solver::manager::ConstructionPhaseFactory;
use solverforge_solver::phase::construction::{ForagerType, QueuedEntityPlacer};
use solverforge_solver::heuristic::r#move::ChangeMove;
use solverforge_solver::heuristic::selector::{FromSolutionEntitySelector, StaticTypedValueSelector};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

type M = ChangeMove<S, i32>;

let factory = ConstructionPhaseFactory::<S, M, _>::new(ForagerType::BestFit, || {
    let es = Box::new(FromSolutionEntitySelector::new(0));
    let vs = Box::new(StaticTypedValueSelector::new(vec![1, 2]));
    Box::new(QueuedEntityPlacer::new(es, vs, get_v, set_v, 0, "v"))
});
Source

pub fn first_fit(placer_factory: F) -> Self

Creates a factory with FirstFit forager.

FirstFit accepts the first valid assignment for each entity, making it fast but potentially producing lower quality initial solutions.

§Example
use solverforge_solver::manager::ConstructionPhaseFactory;
use solverforge_solver::phase::construction::QueuedEntityPlacer;
use solverforge_solver::heuristic::r#move::ChangeMove;
use solverforge_solver::heuristic::selector::{FromSolutionEntitySelector, StaticTypedValueSelector};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

type M = ChangeMove<S, i32>;

let factory = ConstructionPhaseFactory::<S, M, _>::first_fit(|| {
    let es = Box::new(FromSolutionEntitySelector::new(0));
    let vs = Box::new(StaticTypedValueSelector::new(vec![1, 2, 3]));
    Box::new(QueuedEntityPlacer::new(es, vs, get_v, set_v, 0, "v"))
});
Source

pub fn best_fit(placer_factory: F) -> Self

Creates a factory with BestFit forager.

BestFit evaluates all possible values for each entity and selects the one that produces the best score. Slower but produces better initial solutions.

§Example
use solverforge_solver::manager::ConstructionPhaseFactory;
use solverforge_solver::phase::construction::QueuedEntityPlacer;
use solverforge_solver::heuristic::r#move::ChangeMove;
use solverforge_solver::heuristic::selector::{FromSolutionEntitySelector, StaticTypedValueSelector};
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;

type M = ChangeMove<S, i32>;

let factory = ConstructionPhaseFactory::<S, M, _>::best_fit(|| {
    let es = Box::new(FromSolutionEntitySelector::new(0));
    let vs = Box::new(StaticTypedValueSelector::new(vec![1, 2, 3]));
    Box::new(QueuedEntityPlacer::new(es, vs, get_v, set_v, 0, "v"))
});

Trait Implementations§

Source§

impl<S, M, F> SolverPhaseFactory<S> for ConstructionPhaseFactory<S, M, F>
where S: PlanningSolution + 'static, M: Move<S> + Clone + Send + Sync + 'static, F: Fn() -> Box<dyn EntityPlacer<S, M>> + Send + Sync,

Source§

fn create_phase(&self) -> Box<dyn Phase<S>>

Creates a new phase instance. Read more

Auto Trait Implementations§

§

impl<S, M, F> Freeze for ConstructionPhaseFactory<S, M, F>
where F: Freeze,

§

impl<S, M, F> RefUnwindSafe for ConstructionPhaseFactory<S, M, F>

§

impl<S, M, F> Send for ConstructionPhaseFactory<S, M, F>

§

impl<S, M, F> Sync for ConstructionPhaseFactory<S, M, F>

§

impl<S, M, F> Unpin for ConstructionPhaseFactory<S, M, F>
where F: Unpin, S: Unpin, M: Unpin,

§

impl<S, M, F> UnwindSafe for ConstructionPhaseFactory<S, M, F>
where F: UnwindSafe, S: UnwindSafe, M: 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