CobylaState

Struct CobylaState 

Source
pub struct CobylaState {
Show 20 fields pub param: Option<Vec<f64>>, pub prev_param: Option<Vec<f64>>, pub best_param: Option<Vec<f64>>, pub prev_best_param: Option<Vec<f64>>, pub cost: Option<Vec<f64>>, pub prev_cost: Option<Vec<f64>>, pub best_cost: Option<Vec<f64>>, pub prev_best_cost: Option<Vec<f64>>, pub target_cost: f64, pub iter: u64, pub last_best_iter: u64, pub max_iters: u64, pub counts: HashMap<String, u64>, pub time: Option<Duration>, pub termination_status: TerminationStatus, pub rhobeg: f64, pub rhoend: f64, pub iprint: i32, pub maxfun: i32, pub cobyla_context: Option<ManuallyDrop<*mut _cobyla_context>>,
}
Expand description

Maintains the state from iteration to iteration of the crate::CobylaSolver.

This struct is passed from one iteration of an algorithm to the next.

Keeps track of

  • parameter vector of current and previous iteration
  • best parameter vector of current and previous iteration
  • cost function value (objective and constraint functions values) of current and previous iteration
  • current and previous best cost function value
  • target cost function value
  • current iteration number
  • iteration number where the last best parameter vector was found
  • maximum number of iterations that will be executed
  • problem function evaluation counts
  • elapsed time
  • termination status
  • COBYLA specific parameters: rhobeg, rhoend, iprint, maxfun

Fields§

§param: Option<Vec<f64>>

Current parameter vector

§prev_param: Option<Vec<f64>>

Previous parameter vector

§best_param: Option<Vec<f64>>

Current best parameter vector

§prev_best_param: Option<Vec<f64>>

Previous best parameter vector

§cost: Option<Vec<f64>>

Current cost function value

§prev_cost: Option<Vec<f64>>

Previous cost function value

§best_cost: Option<Vec<f64>>

Current best cost function value

§prev_best_cost: Option<Vec<f64>>

Previous best cost function value

§target_cost: f64

Target cost function value

§iter: u64

Current iteration

§last_best_iter: u64

Iteration number of last best cost

§max_iters: u64

Maximum number of iterations

§counts: HashMap<String, u64>

Evaluation counts

§time: Option<Duration>

Time required so far

§termination_status: TerminationStatus

Status of optimization execution

§rhobeg: f64

Rho start value

§rhoend: f64

Rho end value

§iprint: i32

Control of traces

§maxfun: i32

Cost function calls budget

§cobyla_context: Option<ManuallyDrop<*mut _cobyla_context>>

Implementations§

Source§

impl CobylaState
where Self: State<Float = f64>,

Source

pub fn param(self, param: Vec<f64>) -> Self

Set parameter vector. This shifts the stored parameter vector to the previous parameter vector.

§Example
let state = state.param(param);
Source

pub fn target_cost(self, target_cost: f64) -> Self

Set target cost.

When this cost is reached, the algorithm will stop. The default is Self::Float::NEG_INFINITY.

§Example
let state = state.target_cost(0.0);
Source

pub fn max_iters(self, iters: u64) -> Self

Set maximum number of iterations

§Example
let state = state.max_iters(1000);
Source

pub fn maxfun(self, maxfun: u64) -> Self

Alias for max_iters using historic cobyla terminology

Source

pub fn iprint(self, iprint: i32) -> Self

Set maximum number of iterations

§Example
let state = state.iprint(0);
Source

pub fn cost(self, cost: Vec<f64>) -> Self

Set the current cost function value. This shifts the stored cost function value to the previous cost function value.

§Example
let state = state.cost(vec![cost]);
Source

pub fn get_full_cost(&self) -> Option<&Vec<f64>>

Returns current cost (ie objective) function and constraint values.

§Example
let cost = state.get_full_cost();
Source

pub fn get_full_best_cost(&self) -> Option<&Vec<f64>>

Returns current cost (ie objective) function and constraint values.

§Example
let cost = state.get_full_best_cost();
Source

pub fn rhobeg(&self) -> f64

Returns the rho start value

Source

pub fn get_rhoend(&self) -> f64

Returns the rho end value

Source

pub fn get_iprint(&self) -> i32

Returns the level of printing

Source

pub fn get_maxfun(&self) -> i32

Returns cost function calls budget

Trait Implementations§

Source§

impl Clone for CobylaState

Source§

fn clone(&self) -> CobylaState

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 Debug for CobylaState

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for CobylaState

Source§

fn default() -> CobylaState

Returns the “default value” for a type. Read more
Source§

impl<O> Solver<O, CobylaState> for CobylaSolver
where O: CostFunction<Param = Vec<f64>, Output = Vec<f64>>,

Source§

fn init( &mut self, problem: &mut Problem<O>, state: CobylaState, ) -> Result<(CobylaState, Option<KV>), Error>

Initializes the algorithm.

Executed before any iterations are performed and has access to the optimization problem definition and the internal state of the solver. Returns an updated state and optionally a KV which holds key-value pairs used in Observers. The default implementation returns the unaltered state and no KV.

Source§

fn next_iter( &mut self, problem: &mut Problem<O>, state: CobylaState, ) -> Result<(CobylaState, Option<KV>), Error>

Computes a single iteration of the algorithm and has access to the optimization problem definition and the internal state of the solver. Returns an updated state and optionally a KV which holds key-value pairs used in Observers.

Source§

fn terminate(&mut self, state: &CobylaState) -> TerminationStatus

Used to implement stopping criteria, in particular criteria which are not covered by (terminate_internal.

This method has access to the internal state and returns an TerminationReason.

Source§

fn name(&self) -> &str

Name of the solver. Mainly used in Observers.
Source§

fn terminate_internal(&mut self, state: &I) -> TerminationStatus

Checks whether basic termination reasons apply. Read more
Source§

impl State for CobylaState

Source§

type Param = Vec<f64>

Type of parameter vector

Source§

type Float = f64

Floating point precision

Source§

fn new() -> Self

Create new CobylaState instance

§Example
use cobyla_argmin::CobylaState;
let state: CobylaState = CobylaState::new();
Source§

fn update(&mut self)

Checks if the current parameter vector is better than the previous best parameter value. If a new best parameter vector was found, the state is updated accordingly.

§Example

let mut state: CobylaState = CobylaState::new();

// Simulating a new parameter vector
state.param = Some(vec![2.0f64]);
state.cost = Some(vec![5.0]);

// Calling update
state.update();

// Check if update was successful
assert_eq!(state.best_param.as_ref().unwrap()[0], 2.0f64);
assert_eq!(state.best_cost.as_ref().unwrap()[0], 5.0);
assert!(state.is_best());
Source§

fn get_param(&self) -> Option<&Vec<f64>>

Returns a reference to the current parameter vector

§Example
let param = state.get_param();  // Option<&P>
Source§

fn get_best_param(&self) -> Option<&Vec<f64>>

Returns a reference to the current best parameter vector

§Example


let best_param = state.get_best_param();  // Option<&P>
Source§

fn terminate_with(self, reason: TerminationReason) -> Self

Sets the termination reason

§Example
let state = state.terminate_with(TerminationReason::SolverConverged);
Source§

fn time(&mut self, time: Option<Duration>) -> &mut Self

Sets the time required so far.

§Example
let state = state.time(Some(web_time::Duration::new(0, 12)));
Source§

fn get_cost(&self) -> Self::Float

Returns current cost function value.

§Example
let cost = state.get_cost();
Source§

fn get_best_cost(&self) -> Self::Float

Returns current best cost function value.

§Example
let best_cost = state.get_best_cost();
Source§

fn get_target_cost(&self) -> Self::Float

Returns target cost function value.

§Example
let target_cost = state.get_target_cost();
Source§

fn get_iter(&self) -> u64

Returns current number of iterations.

§Example
let iter = state.get_iter();
Source§

fn get_last_best_iter(&self) -> u64

Returns iteration number of last best parameter vector.

§Example
let last_best_iter = state.get_last_best_iter();
Source§

fn get_max_iters(&self) -> u64

Returns the maximum number of iterations.

§Example
let max_iters = state.get_max_iters();
Source§

fn get_termination_status(&self) -> &TerminationStatus

Returns the termination status.

§Example
let termination_status = state.get_termination_status();
Source§

fn get_termination_reason(&self) -> Option<&TerminationReason>

Returns the termination reason if terminated, otherwise None.

§Example
let termination_reason = state.get_termination_reason();
Source§

fn get_time(&self) -> Option<Duration>

Returns the time elapsed since the start of the optimization.

§Example
let time = state.get_time();
Source§

fn increment_iter(&mut self)

Increments the number of iterations by one

§Example
state.increment_iter();
Source§

fn func_counts<O>(&mut self, problem: &Problem<O>)

Set all function evaluation counts to the evaluation counts of another Problem.

state.func_counts(&problem);
Source§

fn get_func_counts(&self) -> &HashMap<String, u64>

Returns function evaluation counts

§Example
let counts = state.get_func_counts();
Source§

fn is_best(&self) -> bool

Returns whether the current parameter vector is also the best parameter vector found so far.

§Example
let is_best = state.is_best();
Source§

fn terminated(&self) -> bool

Return whether the algorithm has terminated or not

Auto Trait Implementations§

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> SendAlias for T

Source§

impl<T> SyncAlias for T