pub struct Solver<State, Problem, IterFn, TermFn>where
State: IterState,
IterFn: Fn(&State, &Problem) -> State,
TermFn: Fn(&State, &Problem) -> bool,{ /* private fields */ }
Expand description
Solver type.
Note: This type does not provide specific algorithms but allows you to customize iteration methods and stopping conditions.
The Problem
generic parameter has no constraints, allowing you to define any kind of interface in the Problem generic for use by iteration methods and stopping conditions.
Implementations§
Source§impl<State, Problem, IterFn, TermFn> Solver<State, Problem, IterFn, TermFn>
impl<State, Problem, IterFn, TermFn> Solver<State, Problem, IterFn, TermFn>
Sourcepub fn new(iter_fn: IterFn, term_cond: TermFn) -> Self
pub fn new(iter_fn: IterFn, term_cond: TermFn) -> Self
Creates a Solver instance with the specified methods.
Parameter iter_fn
defines the state iteration rule for the Solver.
Parameter term_cond
specifies that the Solver stops iterating if and only if
the condition is met: term_cond
returns true
in the current state.
Sourcepub fn solve(
&self,
initial_point: State::Value,
problem: &Problem,
) -> State::Solution
pub fn solve( &self, initial_point: State::Value, problem: &Problem, ) -> State::Solution
Solves the problem by executing iterative logic with the given initial value and specific problem.
§Note
If the algorithm defined by the solver contains logical errors,
the solve function may enter an infinite loop.
To avoid this, try Solver::solve_with_max_iteration
or Solver::solve_with_timeout
.
Sourcepub fn solve_with_max_iteration(
&self,
initial_point: State::Value,
problem: &Problem,
max_iteration: u64,
) -> Result<State::Solution, ReachMaxIteration<State>>
pub fn solve_with_max_iteration( &self, initial_point: State::Value, problem: &Problem, max_iteration: u64, ) -> Result<State::Solution, ReachMaxIteration<State>>
A solution method with a maximum iteration limit.
If the termination condition is met before reaching the maximum number of iterations, it returns an Ok
value with the type IterState::Solution
.
Otherwise, it returns an Err
with error::ReachMaxIteration
.
§Example
use iter_solver::Solver;
// define a never stop solver
let loop_solver = Solver::new(|_state: &f64, _: &()| {*_state}, |_: &f64, _: &()| {false});
let try_solve = loop_solver.solve_with_max_iteration(0.0, &(), 10);
assert!(try_solve.is_err());
Sourcepub fn solve_with_timeout(
&self,
initial_point: State::Value,
problem: &Problem,
timeout: Duration,
) -> Result<State::Solution, TimeOut<State>>
pub fn solve_with_timeout( &self, initial_point: State::Value, problem: &Problem, timeout: Duration, ) -> Result<State::Solution, TimeOut<State>>
A solution method with a time limit.
If the termination condition is met before reaching the timeout duration elapses, it returns an Ok
value with the type IterState::Solution
.
Otherwise, it returns an Err
with error::TimeOut
.
§Example
use iter_solver::Solver;
use std::time::Duration;
// define a never stop solver
let loop_solver = Solver::new(|_state: &f64, _: &()| {*_state}, |_: &f64, _: &()| {false});
let try_solve = loop_solver.solve_with_timeout(0.0, &(), Duration::from_secs(1));
assert!(try_solve.is_err());
Sourcepub fn into_iter<'prob>(
self,
initial_point: State::Value,
problem: &'prob Problem,
) -> SolverIterater<'prob, State, Problem, IterFn, TermFn> ⓘ
pub fn into_iter<'prob>( self, initial_point: State::Value, problem: &'prob Problem, ) -> SolverIterater<'prob, State, Problem, IterFn, TermFn> ⓘ
Consumes the self
and generates an iterator that outputs the current State at each step based on the given initial value and problem.
Use this method when you want to manipulate the state at each step in detail.
§Example
use iter_solver::Solver;
let solver: Solver<f64, _, _, _> = Solver::new(iter_fn, term_cond);
let mut iteration = 1usize;
for state_float in solver.into_iter(2.0, &problem) {
println!("the solution after {} iteration(s): {}", iteration, state_float);
iteration += 1;
}