pub struct SolverHandle<S: PlanningSolution> { /* private fields */ }Expand description
Handle for interacting with a running solver.
The solver handle allows submitting problem changes to a solver while it is running. Changes are queued and processed at step boundaries.
§Example
use solverforge_solver::realtime::{SolverHandle, ProblemChange, ProblemChangeResult};
use solverforge_scoring::ScoreDirector;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;
#[derive(Clone, Debug)]
struct Task { id: usize }
#[derive(Clone, Debug)]
struct Solution {
tasks: Vec<Task>,
score: Option<SimpleScore>,
}
impl PlanningSolution for Solution {
type Score = SimpleScore;
fn score(&self) -> Option<Self::Score> { self.score }
fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
}
#[derive(Debug)]
struct AddTask { id: usize }
impl ProblemChange<Solution> for AddTask {
fn apply(&self, sd: &mut dyn ScoreDirector<Solution>) {
sd.working_solution_mut().tasks.push(Task { id: self.id });
sd.trigger_variable_listeners();
}
}
// Create a handle (normally obtained from RealtimeSolver)
let (handle, _rx) = SolverHandle::<Solution>::new();
// Submit a change while solver is "running"
handle.set_solving(true);
let result = handle.add_problem_change(AddTask { id: 42 });
assert_eq!(result, ProblemChangeResult::Queued);
// When solver stops, changes are rejected
handle.set_solving(false);
let result = handle.add_problem_change(AddTask { id: 43 });
assert_eq!(result, ProblemChangeResult::SolverNotRunning);Implementations§
Source§impl<S: PlanningSolution> SolverHandle<S>
impl<S: PlanningSolution> SolverHandle<S>
Sourcepub fn new() -> (Self, ProblemChangeReceiver<S>)
pub fn new() -> (Self, ProblemChangeReceiver<S>)
Creates a new solver handle and its corresponding receiver.
The receiver should be passed to the solver to receive changes.
Sourcepub fn add_problem_change<P: ProblemChange<S> + 'static>(
&self,
change: P,
) -> ProblemChangeResult
pub fn add_problem_change<P: ProblemChange<S> + 'static>( &self, change: P, ) -> ProblemChangeResult
Submits a problem change to the solver.
The change is queued and will be processed at the next step boundary. Returns the result of the submission.
Sourcepub fn add_problem_change_boxed(
&self,
change: BoxedProblemChange<S>,
) -> ProblemChangeResult
pub fn add_problem_change_boxed( &self, change: BoxedProblemChange<S>, ) -> ProblemChangeResult
Submits a boxed problem change to the solver.
Sourcepub fn is_solving(&self) -> bool
pub fn is_solving(&self) -> bool
Returns true if the solver is currently running.
Sourcepub fn terminate_early(&self)
pub fn terminate_early(&self)
Requests early termination of the solver.
The solver will stop at the next step boundary.
Sourcepub fn set_solving(&self, solving: bool)
pub fn set_solving(&self, solving: bool)
Sets the solving flag (used internally by the solver).
Trait Implementations§
Source§impl<S: PlanningSolution> Clone for SolverHandle<S>
impl<S: PlanningSolution> Clone for SolverHandle<S>
Auto Trait Implementations§
impl<S> Freeze for SolverHandle<S>
impl<S> RefUnwindSafe for SolverHandle<S>
impl<S> Send for SolverHandle<S>
impl<S> Sync for SolverHandle<S>
impl<S> Unpin for SolverHandle<S>
impl<S> UnwindSafe for SolverHandle<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more