use nalgebra::DVector;
use crate::{CMAESOptions, Individual, Mode, ObjectiveFunction, ParallelObjectiveFunction};
const PRINT_GAP_EVALS: usize = 200;
const INVALID_OPTIONS: &str = "Invalid options";
const INVALID_VALUE: &str = "Objective function returned an invalid value";
pub fn fmin<F, V>(f: F, initial_mean: V, initial_step_size: f64) -> Individual
where
F: ObjectiveFunction,
V: Into<DVector<f64>>,
{
CMAESOptions::new(initial_mean, initial_step_size)
.enable_printing(PRINT_GAP_EVALS)
.build(f)
.expect(INVALID_OPTIONS)
.run()
.overall_best
.expect(INVALID_VALUE)
}
pub fn fmin_parallel<F, V>(f: F, initial_mean: V, initial_step_size: f64) -> Individual
where
F: ParallelObjectiveFunction,
V: Into<DVector<f64>>,
{
CMAESOptions::new(initial_mean, initial_step_size)
.enable_printing(PRINT_GAP_EVALS)
.build(f)
.expect(INVALID_OPTIONS)
.run_parallel()
.overall_best
.expect(INVALID_VALUE)
}
pub fn fmax<F, V>(f: F, initial_mean: V, initial_step_size: f64) -> Individual
where
F: ObjectiveFunction,
V: Into<DVector<f64>>,
{
CMAESOptions::new(initial_mean, initial_step_size)
.mode(Mode::Maximize)
.enable_printing(PRINT_GAP_EVALS)
.build(f)
.expect(INVALID_OPTIONS)
.run()
.overall_best
.expect(INVALID_VALUE)
}
pub fn fmax_parallel<F, V>(f: F, initial_mean: V, initial_step_size: f64) -> Individual
where
F: ParallelObjectiveFunction,
V: Into<DVector<f64>>,
{
CMAESOptions::new(initial_mean, initial_step_size)
.mode(Mode::Maximize)
.enable_printing(PRINT_GAP_EVALS)
.build(f)
.expect(INVALID_OPTIONS)
.run_parallel()
.overall_best
.expect(INVALID_VALUE)
}