vanth/crates/varo/tests/integration/main.rs
Markus Scully db531c8c73
🧪 workspace, varo: Implement RNG with ChaCha8 and add optimization function
- Updated `Cargo.toml` and `Cargo.lock` to downgrade `rand_core` to 0.6.4 and add `rand_chacha` dependency with `serde1` feature.
- Implemented `Rng` struct wrapping `ChaCha8Rng` with seed initialization, stream selection, and number generation methods.
- Added `rng_new`, `rng_from_seed`, `rng_set_stream`, `rng_next_u32`, `rng_next_u64`, `rng_fill_bytes`, `rng_gen_f32`, and `rng_gen_gaussian` functions.
- Made `value` field in `Score` and `values` field in `OptimizationResult` public.
- Implemented `Distribution::sample` method that generates Gaussian numbers when moments are available.
- Added `From<f32>` implementation for `Score`.
- Implemented `optimize` function that evaluates candidates using cloned RNG streams and returns sorted results.
- Added integration test `test_optimize` that verifies optimization sorting behavior.
2025-08-06 16:46:58 +03:00

30 lines
790 B
Rust

use varo::{optimize, Rng, Score, Varo};
#[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!();
}