Function simple_optimization::random_search
source · [−]pub fn random_search<A: 'static + Send + Sync, T: 'static + Copy + Send + Sync + Default + SampleUniform + PartialOrd, const N: usize>(
ranges: [Range<T>; N],
f: fn(_: &[T; N], _: Option<Arc<A>>) -> f64,
evaluation_data: Option<Arc<A>>,
polling: Option<Polling>,
threads: Option<usize>,
iterations: u64
) -> [T; N]
Expand description
Randomly pick parameters for simple_function
in the ranges 0..5
, 5..15
, and 10..20
and return the parameters which produce the minimum result from simple_function
out of 10,000
samples, printing progress every 10ms
, and exiting early if a value is found which is less than or equal to 19.
.
use std::sync::Arc;
use simple_optimization::{random_search, Polling};
fn simple_function(list: &[f64; 3], _: Option<Arc::<()>>) -> f64 { list.iter().sum() }
let best = random_search(
[0f64..10f64, 5f64..15f64, 10f64..20f64], // Value ranges.
simple_function, // Evaluation function.
None, // No additional evaluation data.
// By using `new` this defaults to polling every `10ms`, we also print progress `true` and exit early if `19.` or less is reached.
Some(Polling::new(true,Some(19.))),
None, // We don't specify the number of threads.
1000, // Take `1000` samples (split between threads, so each thread only takes `1000/n` samples).
);
assert!(simple_function(&best, None) < 19.);