Crates.io | pso |
lib.rs | pso |
version | 0.2.0 |
source | src |
created_at | 2020-06-30 23:10:22.000172 |
updated_at | 2020-07-06 01:10:44.514185 |
description | Particle Swarm Optimizer |
homepage | |
repository | |
max_upload_size | |
id | 260064 |
size | 64,957 |
Easy and Efficient Particle Swarm Optimizer in Rust
// set up a PSO with 8 default swarms
let pso = PSO::default(8, false);
// set up the stop condition and search space with a JobConfig
let mut jc = JobConfig::new(5); // search a 5 variable space
jc.exit_cost(10e-10); // stop optimizing when the objective cost reaches 10e-10
jc.variable_bound([-5.0, 5.0]); // constrain the 5D search space to (-5, 5) along all dimensions
// create a simple objective function
let obj = |x_vec: &[f64]| -> f64 { x_vec.iter().map(|x| x.powi(2)).sum::<f64>() };
// search for the minimum
let min = pso.run_job_fn(jc, obj);
assert!(min.0 < 10e-9);
for x in min.1 {
assert!(x.abs() < 0.001);
}
// define a swarm configuration
let mut sc = SwarmConfig::new();
sc.synergic_behavior(0.4, 100); // collaborate with other swarms every 100 iterations using a global-coefficient of 0.4
sc.motion_coefficients(0.5, 0.6, 1.0); // use custom motion coefficients
sc.num_particles(256); // put 256 particles in each swarm
// set up a PSO with 8 swarms using the above SwarmConfig
let pso = PSO::from_swarm_config(8, false, &sc);
// set up the stop condition and search space with a JobConfig
let mut jc = JobConfig::new(5);
jc.max_iter_and_exit_cost(10000, 10e-10); // stop after the first of these conditions is met
jc.variable_bounds(vec![
// define a custom upper and lower bound on each dimension
[-10.0, 10.0],
[-8.0, 8.0],
[-6.0, 6.0],
[-4.0, 4.0],
[-2.0, 2.0],
]);
jc.update_console(100); // update the console with the current minimum every 100 iterations
// create a simple objective function using some external data
let mins = [1.0; 5];
let obj = move |x_vec: &[f64]| -> f64 {
x_vec
.iter()
.enumerate()
.map(|(i, x)| (x - mins[i]).powi(2))
.sum::<f64>()
};
// search for the minimum
let min = pso.run_job_fn(jc, obj);
assert!(min.0 < 10e-9);
for (i, x) in min.1.iter().enumerate() {
assert!((x - mins[i]).abs() < 0.001);
}