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_iteration or Solver::solve_with_timeout.

Source

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());
Source

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());
Source

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;
} 
Source

pub fn change_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> 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.