pub struct Solver<State, Problem, IterFn, TermFn>{ /* 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_iterations or Solver::solve_with_timeout. If you need more flexible error handling, try Solver::solve_with_error.
Sourcepub fn solve_with_max_iterations(
&self,
initial_point: State::Value,
problem: &Problem,
max_iteration: u64,
) -> Result<State::Solution, MaxIterationReached<State>>
pub fn solve_with_max_iterations( &self, initial_point: State::Value, problem: &Problem, max_iteration: u64, ) -> Result<State::Solution, MaxIterationReached<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::MaxIterationReached.
§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_iterations(0.0, &(), 10);
assert!(try_solve.is_err());If you need more flexible error handling, try Solver::solve_with_error.
Sourcepub fn solve_with_timeout(
&self,
initial_point: State::Value,
problem: &Problem,
timeout: Duration,
) -> Result<State::Solution, TimeOut<State>>
Available on crate feature std only.
pub fn solve_with_timeout( &self, initial_point: State::Value, problem: &Problem, timeout: Duration, ) -> Result<State::Solution, TimeOut<State>>
std only.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());If you need more flexible error handling, try Solver::solve_with_error.
Sourcepub fn solve_with_error<E, F>(
&self,
initial_point: State::Value,
problem: &Problem,
check_fn: F,
) -> Result<State::Solution, E>
pub fn solve_with_error<E, F>( &self, initial_point: State::Value, problem: &Problem, check_fn: F, ) -> Result<State::Solution, E>
Performs iterative solving with custom error handling, allowing early termination.
This method executes an iterative solving process, but before each iteration,
it invokes the provided check_fn with the current state and the problem reference.
If check_fn returns Ok(()), the iteration continues until the stopping criteria
are met, returning Ok with the final solution. If check_fn returns Err(e), the
iteration stops immediately and returns Err(e).
The key feature is flexible error type customization – E can be any type
that suits your error-handling needs (e.g., a simple &'static str, a custom enum,
or a structured error type). This allows you to:
- Embed domain-specific failure semantics directly into the solving flow.
- Propagate rich error information without boxing or trait objects.
- Maintain full control over error kinds and context.
§Example
use iter_solver::Solver;
let check_fn = |float: &f64, _: &()| {
if float.is_infinite() {
return Err("Inf Error");
} else if float.is_nan() {
return Err("NaN Error");
}
Ok(())
};
let solver = Solver::new(
|f, _| f * 2.0, // 2^n -> Inf
|_,_| {false} // never stop
);
let result = solver.solve_with_error(1.0, &(), check_fn);
assert!(result.is_err());
println!("{}", result.unwrap_err()) // print "Inf Error"