Adds heuropt-plot, a tiny SVG-only plotter that takes heuropt results and emits scatter plots (pareto_front_svg) and line plots (convergence_svg). No heavy 'plotters' or 'tiny-skia' dep — hand- rolled SVG so the crate adds <100 KB to a build. Workspace setup: root Cargo.toml gains [workspace] with members = ['.', 'heuropt-plot']. heuropt-plot has its own version (0.1.0) and publishes independently against heuropt 0.8+. Adds examples/visualize.rs that wires it up: NSGA-II on Schaffer N.1, plain run() (no observer plumbing), final-front SVG written to disk.
54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
//! Visualize an NSGA-II run on Schaffer N.1 with the `heuropt-plot`
|
|
//! companion crate — produces a `pareto_front.svg` of the final
|
|
//! Pareto front.
|
|
//!
|
|
//! Run with: `cargo run --release --example visualize`
|
|
|
|
use heuropt::prelude::*;
|
|
use heuropt_plot::pareto_front_svg;
|
|
|
|
struct Schaffer;
|
|
|
|
impl Problem for Schaffer {
|
|
type Decision = Vec<f64>;
|
|
fn objectives(&self) -> ObjectiveSpace {
|
|
ObjectiveSpace::new(vec![Objective::minimize("f1"), Objective::minimize("f2")])
|
|
}
|
|
fn evaluate(&self, x: &Vec<f64>) -> Evaluation {
|
|
Evaluation::new(vec![x[0] * x[0], (x[0] - 2.0).powi(2)])
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let problem = Schaffer;
|
|
let bounds = vec![(-5.0_f64, 5.0_f64)];
|
|
let space = problem.objectives();
|
|
|
|
let mut opt = Nsga2::new(
|
|
Nsga2Config {
|
|
population_size: 50,
|
|
generations: 100,
|
|
seed: 42,
|
|
},
|
|
RealBounds::new(bounds.clone()),
|
|
CompositeVariation {
|
|
crossover: SimulatedBinaryCrossover::new(bounds.clone(), 15.0, 0.5),
|
|
mutation: PolynomialMutation::new(bounds, 20.0, 1.0),
|
|
},
|
|
);
|
|
|
|
let result = opt.run(&problem);
|
|
|
|
let svg = pareto_front_svg(
|
|
&result.pareto_front,
|
|
&space,
|
|
700,
|
|
450,
|
|
"NSGA-II on Schaffer N.1 — final Pareto front",
|
|
);
|
|
std::fs::write("pareto_front.svg", svg).expect("write pareto_front.svg");
|
|
|
|
println!("Final front size: {}", result.pareto_front.len());
|
|
println!("Wrote pareto_front.svg");
|
|
}
|