The three runnable examples called out in spec §18.5 / §19. All open with `use heuropt::prelude::*;` so they double as a check that the prelude is sufficient on its own: - toy_nsga2.rs: Schaffer N.1 solved with NSGA-II. - random_search.rs: 2D sphere solved with RandomSearch. - custom_optimizer.rs: a minimal hill-climber implementing `Optimizer<P>` directly, demonstrating spec §2.3.
43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
//! Solve the Schaffer N.1 two-objective problem with NSGA-II.
|
|
//!
|
|
//! Run with:
|
|
//!
|
|
//! ```bash
|
|
//! cargo run --example toy_nsga2
|
|
//! ```
|
|
|
|
use heuropt::prelude::*;
|
|
|
|
struct SchafferN1;
|
|
|
|
impl Problem for SchafferN1 {
|
|
type Decision = Vec<f64>;
|
|
|
|
fn objectives(&self) -> ObjectiveSpace {
|
|
ObjectiveSpace::new(vec![Objective::minimize("f1"), Objective::minimize("f2")])
|
|
}
|
|
|
|
fn evaluate(&self, x: &Vec<f64>) -> Evaluation {
|
|
let v = x[0];
|
|
Evaluation::new(vec![v * v, (v - 2.0).powi(2)])
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let initializer = RealBounds::new(vec![(-5.0, 5.0)]);
|
|
let variation = GaussianMutation { sigma: 0.2 };
|
|
let config = Nsga2Config { population_size: 60, generations: 80, seed: 42 };
|
|
let mut optimizer = Nsga2::new(config, initializer, variation);
|
|
|
|
let result = optimizer.run(&SchafferN1);
|
|
|
|
println!("Final population: {}", result.population.len());
|
|
println!("Pareto front size: {}", result.pareto_front.len());
|
|
println!("Total evaluations: {}", result.evaluations);
|
|
println!("First few front points (f1, f2):");
|
|
for c in result.pareto_front.iter().take(8) {
|
|
let o = &c.evaluation.objectives;
|
|
println!(" ({:.4}, {:.4})", o[0], o[1]);
|
|
}
|
|
}
|