Skip to main content

ScalarGradientState

Struct ScalarGradientState 

Source
pub struct ScalarGradientState<F = f64> { /* private fields */ }
Expand description

State for one-dimensional solvers that carry a first derivative (BrentDerivative): a scalar param, an optional cached cost, an optional cached gradient f'(x), and cost/gradient evaluation counters. Param and Float are the same scalar F.

It is the gradient-carrying sibling of ScalarState: same scalar single-iterate shape, but it does impl GradientState (its gradient is the scalar f'(x)), so first-order termination criteria such as GradientTolerance work on a 1D solver — the natural “stop when |f'(x)| ≤ tol” test. A scalar gradient satisfies the criteria’s norm bounds directly (f64::norm_squared() is f' · f').

best_param tracks the lowest-cost probe, which for a bracketing search can differ from the final iterate.

The scalar F defaults to f64 so ScalarGradientState call sites resolve unchanged.

Implementations§

Source§

impl<F: Scalar> ScalarGradientState<F>

Source

pub fn new(param: F) -> Self

Build a state at the given starting point. The cost and gradient are filled in by Solver::init.

Trait Implementations§

Source§

impl<F> CountsMirror for ScalarGradientState<F>

Source§

fn mirror(&mut self, delta: &EvalCounts)

Overwrite the state’s counters from the per-run wrapper delta. Called by the executor after every successful Solver::init / Solver::next_iter.
Source§

impl<F: Scalar> GradientState for ScalarGradientState<F>

Source§

fn gradient(&self) -> Option<&F>

Gradient at the current param, if populated.
Source§

fn gradient_evals(&self) -> u64

Cumulative count of gradient evaluations performed so far. Lives on GradientState rather than State so derivative-free states don’t carry a counter they can never increment. Read more
Source§

fn best_gradient_evals(&self) -> u64

Cumulative gradient evaluations at the moment the current best was found — companion to State::best_cost_evals. Useful for benchmarking first-order solvers (“how many gradient calls until the solver hit its best?”).
Source§

impl<P, F> Solver<P, ScalarGradientState<F>> for BrentDerivative<F>
where F: Scalar, P: CostFunction<Param = F, Output = F> + Gradient<Gradient = F> + BoxConstraints,

Source§

type Error = <P as CostFunction>::Error

Hard-abort error type — mirrors the underlying problem’s type Error. See the trait docs.
Source§

fn init( &mut self, problem: &mut Problem<P>, state: ScalarGradientState<F>, ) -> Result<ScalarGradientState<F>, Self::Error>

One-time setup before the iteration loop. Read more
Source§

fn next_iter( &mut self, problem: &mut Problem<P>, state: ScalarGradientState<F>, ) -> Result<(ScalarGradientState<F>, Option<TerminationReason>), Self::Error>

Advance one iteration. Read more
Source§

fn terminate( &self, _state: &ScalarGradientState<F>, ) -> Option<TerminationReason>

Optional pre-iteration solver-specific termination test. Read more
Source§

impl<F: Scalar> State for ScalarGradientState<F>

Source§

fn cost(&self) -> F

Reads the cost cached at the current param.

§Panics

Panics if accessed before Solver::init has populated the cached cost. By contract, Executor calls init before any termination-criterion check, so reads from inside criteria and from OptimizationResult are safe.

Source§

type Param = F

The parameter type the solver iterates over (e.g. Vec<f64>, nalgebra::DVector<f64>).
Source§

type Float = F

The scalar type of the objective. In practice always f64 (see the module docs).
Source§

fn iter(&self) -> u64

Number of fully completed iterations. A Solver::next_iter that bails mid-iteration with Some(reason) does not increment this counter — see the executor module for the exact ordering.
Source§

fn increment_iter(&mut self)

Increment iter by one. Called by the executor after a successful Solver::next_iter.
Source§

fn cost_evals(&self) -> u64

Cumulative count of cost-function evaluations performed so far. Diverges from iter() whenever a single iteration evaluates the cost more than once (line searches, Nelder-Mead shrinks, etc.) — this is what users actually budget against. Read more
Source§

fn param(&self) -> &F

Current iterate. Stable between Solver::next_iter calls; safe to read at any iteration including iter 0.
Source§

fn best_param(&self) -> &F

Best param ever observed by the executor’s best-tracking on this state. Read more
Source§

fn best_cost(&self) -> F

Cost at best_param — the lowest cost ever observed on this state.
Source§

fn best_iter(&self) -> u64

Iteration at which the current best was found. 0 before the first update_best call; thereafter, the value of iter at the moment of the last strict improvement in best_cost().
Source§

fn best_cost_evals(&self) -> u64

Cumulative cost evaluations at the moment the current best was found — useful for benchmarking (“how many evals until the solver hit its best?”).
Source§

fn update_best(&mut self)

Refresh the best-so-far slots from the current iterate, if cost strictly improves on best_cost. Read more
Source§

fn reset_best(&mut self)

Reset the best-so-far slots to their pre-init defaults (best_cost = +∞, all best counters zero). Read more

Auto Trait Implementations§

§

impl<F> Freeze for ScalarGradientState<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for ScalarGradientState<F>
where F: RefUnwindSafe,

§

impl<F> Send for ScalarGradientState<F>
where F: Send,

§

impl<F> Sync for ScalarGradientState<F>
where F: Sync,

§

impl<F> Unpin for ScalarGradientState<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for ScalarGradientState<F>
where F: UnsafeUnpin,

§

impl<F> UnwindSafe for ScalarGradientState<F>
where F: 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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V