Skip to main content

Solver

Struct Solver 

Source
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>
where State: IterState, IterFn: Fn(State, &Problem) -> State, TermFn: Fn(&State, &Problem) -> bool,

Source

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.

Source

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.

Source

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.

Source

pub fn solve_with_timeout( &self, initial_point: State::Value, problem: &Problem, timeout: Duration, ) -> Result<State::Solution, TimeOut<State>>

Available on crate feature 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.

Source

pub fn solve_with_error<E, F>( &self, initial_point: State::Value, problem: &Problem, check_fn: F, ) -> Result<State::Solution, E>
where F: Fn(&State, &Problem) -> Result<(), 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 customizationE 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"
Source

pub fn with_term_cond<NewTermCond>( self, new_cond: NewTermCond, ) -> Solver<State, Problem, IterFn, NewTermCond>
where NewTermCond: Fn(&State, &Problem) -> bool,

Consumes self and returns a new Solver with the given new termination condition.

Trait Implementations§

Source§

impl<State, Problem, IterFn, TermFn> Clone for Solver<State, Problem, IterFn, TermFn>
where State: IterState, IterFn: Fn(State, &Problem) -> State + Clone, TermFn: Fn(&State, &Problem) -> bool + Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<State, Problem: Debug, IterFn, TermFn> Debug for Solver<State, Problem, IterFn, TermFn>
where State: IterState + Debug, IterFn: Fn(State, &Problem) -> State + Debug, TermFn: Fn(&State, &Problem) -> bool + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<State, Problem, IterFn, TermFn> Freeze for Solver<State, Problem, IterFn, TermFn>
where IterFn: Freeze, TermFn: Freeze,

§

impl<State, Problem, IterFn, TermFn> RefUnwindSafe for Solver<State, Problem, IterFn, TermFn>
where IterFn: RefUnwindSafe, TermFn: RefUnwindSafe, State: RefUnwindSafe, Problem: RefUnwindSafe,

§

impl<State, Problem, IterFn, TermFn> Send for Solver<State, Problem, IterFn, TermFn>
where IterFn: Send, TermFn: Send, State: Send, Problem: Send,

§

impl<State, Problem, IterFn, TermFn> Sync for Solver<State, Problem, IterFn, TermFn>
where IterFn: Sync, TermFn: Sync, State: Sync, Problem: Sync,

§

impl<State, Problem, IterFn, TermFn> Unpin for Solver<State, Problem, IterFn, TermFn>
where IterFn: Unpin, TermFn: Unpin, State: Unpin, Problem: Unpin,

§

impl<State, Problem, IterFn, TermFn> UnsafeUnpin for Solver<State, Problem, IterFn, TermFn>
where IterFn: UnsafeUnpin, TermFn: UnsafeUnpin,

§

impl<State, Problem, IterFn, TermFn> UnwindSafe for Solver<State, Problem, IterFn, TermFn>
where IterFn: UnwindSafe, TermFn: UnwindSafe, State: UnwindSafe, Problem: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.