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>,
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>,
pub fn new(f: F) -> Self
Sourcepub fn with_tol(&mut self, tolerance: T) -> &mut Self
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();Sourcepub fn with_iter_max(&mut self, iter_max: usize) -> &mut Self
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();Sourcepub fn with_sample_size(&mut self, sample_size: usize) -> &mut Self
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();Sourcepub fn with_importance_selection_size(
&mut self,
importance_selection_size: usize,
) -> &mut Self
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();Sourcepub fn with_std_dev(
&mut self,
std_dev: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>,
) -> &mut Self
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();Sourcepub fn solve(
&self,
x0: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>,
) -> SolverResult<Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.