- Added `.zed/settings.json` to configure rust-analyzer with `leptosfmt`. - Created `rustfmt.toml` with max width 120 and comment wrapping settings. - Applied consistent formatting across all modified crates using rustfmt. - Reorganized import statements and improved code style in multiple modules.
30 lines
778 B
Rust
30 lines
778 B
Rust
use varo::{Rng, Score, Varo, optimize};
|
|
|
|
#[test]
|
|
fn test_optimize() {
|
|
struct Foo {
|
|
x: f32,
|
|
}
|
|
|
|
impl Varo for Foo {
|
|
fn next(digest: &mut varo::Rng) -> Self {
|
|
let x = varo::rng_gen_f32(digest) * 10.0;
|
|
Foo { x }
|
|
}
|
|
}
|
|
|
|
fn evaluate(foo: Foo) -> Score {
|
|
let x = foo.x;
|
|
let score = -0.9 * x.powi(3) + 2.6 * x.powi(2) - 4.0 * x;
|
|
score.into()
|
|
}
|
|
|
|
let mut rng = varo::rng_new();
|
|
let optimization_result = optimize(evaluate, &mut rng, 10);
|
|
assert_eq!(optimization_result.values.len(), 10);
|
|
let scores: Vec<f32> = optimization_result.values.iter().map(|pair| pair.1).collect();
|
|
for i in 0..scores.len() - 1 {
|
|
assert!(scores[i] > scores[i + 1]);
|
|
}
|
|
println!();
|
|
}
|