CrossEntropy

Struct CrossEntropy 

Source
pub struct CrossEntropy<T, D, F>
where T: Float + Scalar, D: Dim, F: Fn(Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>) -> T, DefaultAllocator: Allocator<D>,
{ /* private fields */ }
Expand description

§Cross-Entropy Optimiser

This struct approximates x where F(x) <= F(y) for all y in the domain S in Rn of F, where F: S -> R is an objective or cost function to be minimised. This is done using the Cross-Entropy Method where the random values are sampled from a multivariate normal distribution. More specifically it is a vector of independent normally distributed random variables, which means the covariance matrix is diagonal, which is given to the optimiser as a vector of (sample) standard deviations.

Default Tolerance: 1e-6

Default Standard Deviation: [1.0, 1.0,..., 1.0]

Default Max Iterations: 50

Default Sample Size: 100

Default Importance Selection Size: 10

§Examples

use eqsolver::global_optimisers::CrossEntropy;
use nalgebra::SVector;

const SIZE: usize = 16;
let rastrigin = |v: SVector<f64, SIZE>| {
    let mut total = 10. * SIZE as f64;

    for &w in v.iter() {
        total += w * w - 10. * (2. * std::f64::consts::PI * w).cos();
    }

    total
};


let guess = SVector::repeat(80.);

let optimised_position = CrossEntropy::new(rastrigin)
    .solve(guess)
    .unwrap();

Implementations§

Source§

impl<T, D, F> CrossEntropy<T, D, F>
where T: Float + Scalar + ComplexField<RealField = T>, D: Dim, F: Fn(Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>) -> T, DefaultAllocator: Allocator<D>, StandardNormal: Distribution<T>,

Source

pub fn new(f: F) -> Self

Source

pub fn with_tol(&mut self, tolerance: T) -> &mut Self

Set the tolerance of the optimiser.

Default Tolerance: 1e-6

§Examples
let optimised_position = CrossEntropy::new(f)
    .with_tol(1e-12)
    .solve(guess)
    .unwrap();
Source

pub fn with_iter_max(&mut self, iter_max: usize) -> &mut Self

Set the maximum number of iterations of the optimiser. After that number of iterations is reached, the current mean vector (best value) is returned.

Default Max Iterations: 50

§Examples
let optimised_position = CrossEntropy::new(f)
    .with_iter_max(100)
    .solve(guess)
    .unwrap();
Source

pub fn with_sample_size(&mut self, sample_size: usize) -> &mut Self

Sets the number of samples drawn each iteration. This value should be greater than the importance selection size

Default Sample Size: 100

§Examples
let optimised_position = CrossEntropy::new(f)
    .with_sample_size(30)
    .solve(guess)
    .unwrap();
Source

pub fn with_importance_selection_size( &mut self, importance_selection_size: usize, ) -> &mut Self

Sets how many of the sampled values are selected in order of smallest objective function value. This value should be less than sample size.

Default Importance Selection Size: 10

§Examples
let optimised_position = CrossEntropy::new(f)
    .with_importance_selection_size(3)
    .solve(guess)
    .unwrap();
Source

pub fn with_std_dev( &mut self, std_dev: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>, ) -> &mut Self

Sets the initial standard deviation vector used in the optimiser. The vector should contain positive values which should be large when the uncertainty is high.

Default Standard Deviations: [1.0, 1.0,... 1.0]

§Examples
let std_dev = SVector::repeat(100.);
let optimised_position = CrossEntropy::new(f)
    .with_std_dev(std_dev)
    .solve(guess)
    .unwrap();
Source

pub fn solve( &self, x0: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>, ) -> SolverResult<Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>>

Optimises the function using a given initial value (or guess) by returning an approximation of the global minimum of the objective function.

§Examples
let optimised_position = CrossEntropy::new(f)
    .solve(guess)
    .unwrap();

Auto Trait Implementations§

§

impl<T, D, F> !Freeze for CrossEntropy<T, D, F>

§

impl<T, D, F> !RefUnwindSafe for CrossEntropy<T, D, F>

§

impl<T, D, F> !Send for CrossEntropy<T, D, F>

§

impl<T, D, F> !Sync for CrossEntropy<T, D, F>

§

impl<T, D, F> !Unpin for CrossEntropy<T, D, F>

§

impl<T, D, F> !UnwindSafe for CrossEntropy<T, D, F>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

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