Expand description
Differential Evolution optimizer for rust.
Simple and powerful global optimization using a Self-Adapting Differential Evolution for Rust. See Wikipedia’s article on Differential Evolution for more information.
§Usage
Add this to your Cargo.toml
:
[dependencies]
differential-evolution = "*"
and this to your crate root:
extern crate differential_evolution;
§Examples
Differential Evolution is a global optimization algorithm that tries to iteratively improve candidate solutions with regards to a user-defined cost function.
§Quick Start: Sum of Squares
This example finds the minimum of a simple 5-dimensional function.
extern crate differential_evolution;
use differential_evolution::self_adaptive_de;
fn main() {
// create a self adaptive DE with an inital search area
// from -10 to 10 in 5 dimensions.
let mut de = self_adaptive_de(vec![(-10.0, 10.0); 5], |pos| {
// cost function to minimize: sum of squares
pos.iter().fold(0.0, |sum, x| sum + x*x)
});
// perform 10000 cost evaluations
de.iter().nth(10000);
// show the result
let (cost, pos) = de.best().unwrap();
println!("cost: {}", cost);
println!("pos: {:?}", pos);
}
§Tutorial: Rastrigin
The population supports an Iterator
for evaluating. Each call
of next()
evaluates the cost function and returns the
fitness value of the current global best. This way it is possible
to use all the iterator’s features for optimizig. Here are a few
examples.
Let’s say we have the Rastrigin cost function:
use std::f32::consts::PI;
fn rastrigin(pos: &[f32]) -> f32 {
pos.iter().fold(0.0, |sum, x|
sum + x * x - 10.0 * (2.0 * PI * x).cos() + 10.0)
}
We’d like to search for the minimum in the range -5.12 to 5.12, for 30 dimensions:
let initial_min_max = vec![(-5.12, 5.12); 30];
We can create a self adaptive DE, and search until the cost reaches a given minimum:
let mut de = self_adaptive_de(initial_min_max, rastrigin);
de.iter().find(|&cost| cost < 0.1);
This is a bit dangerous though, because the optimizer might never reach that minimum. It is safer to just let it run for a given number of evaluations:
let mut de = self_adaptive_de(initial_min_max, rastrigin);
de.iter().nth(10000);
If is possible to do some smart combinations: run until cost is below a threshold, or until the maximum number of iterations have been reached:
let mut de = self_adaptive_de(initial_min_max, sum_of_squares);
de.iter().take(100000).find(|&cost| cost < 0.1);
When you are finished with iterating, you can extract the best solution found so far with
de.best()
. This retrieves the minimum cost and the position vector that has lead to this
cost:
let (cost, pos) = de.best().unwrap();
println!("{} best cost", cost);
println!("{:?} best position", pos);
§Similar Crates
Structs§
- PopIter
- Iterator for the differential evolution, to perform a single cost
evaluation every time
move()
is called. - Population
- Holds the population for the differential evolution based on the given settings.
- Settings
- Holds all settings for the self adaptive differential evolution algorithm.
Functions§
- self_
adaptive_ de - Convenience function to create a fully configured self adaptive differential evolution population.