feat(heuropt-plot): v0.1.0 — SVG visualization companion crate
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.7+. Adds examples/visualize.rs that wires it all up: NSGA-II on Schaffer N.1, observer closure recording per-generation hypervolume, two SVGs written to disk.
This commit is contained in:
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- **`heuropt-plot` companion crate (v0.1.0)** at `heuropt-plot/`,
|
||||
published independently. Lightweight SVG-only plotter for Pareto
|
||||
fronts (`pareto_front_svg`) and convergence traces
|
||||
(`convergence_svg`) — hand-rolled SVG output, no `plotters` /
|
||||
`tiny-skia` dep so the crate stays a tiny optional addition.
|
||||
- `examples/visualize.rs` — runs NSGA-II on Schaffer N.1 with a
|
||||
closure observer that records hypervolume per generation, then
|
||||
emits `pareto_front.svg` + `convergence.svg` via `heuropt-plot`.
|
||||
|
||||
## [0.7.0] — 2026-05-05
|
||||
|
||||
Theme: async evaluation. heuropt now supports problems where each
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
[workspace]
|
||||
members = [".", "heuropt-plot"]
|
||||
|
||||
[package]
|
||||
name = "heuropt"
|
||||
version = "0.7.0"
|
||||
@@ -30,6 +33,7 @@ tracing = { version = "0.1", optional = true, default-features = false, features
|
||||
|
||||
[dev-dependencies]
|
||||
gungraun = "0.18"
|
||||
heuropt-plot = { path = "heuropt-plot" }
|
||||
proptest = "1"
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 700 450" font-family="system-ui, sans-serif" font-size="12">
|
||||
<rect x="0" y="0" width="700" height="450" fill="white"/>
|
||||
<text x="70" y="22" font-size="16" font-weight="bold">NSGA-II on Schaffer N.1 — hypervolume per generation</text>
|
||||
<rect x="70" y="40" width="610" height="360" fill="none" stroke="#888" />
|
||||
<line x1="70" y1="400" x2="70" y2="405" stroke="#888" />
|
||||
<text x="70" y="418" text-anchor="middle">0</text>
|
||||
<line x1="222.5" y1="400" x2="222.5" y2="405" stroke="#888" />
|
||||
<text x="222.5" y="418" text-anchor="middle">25</text>
|
||||
<line x1="375" y1="400" x2="375" y2="405" stroke="#888" />
|
||||
<text x="375" y="418" text-anchor="middle">50</text>
|
||||
<line x1="527.5" y1="400" x2="527.5" y2="405" stroke="#888" />
|
||||
<text x="527.5" y="418" text-anchor="middle">75</text>
|
||||
<line x1="680" y1="400" x2="680" y2="405" stroke="#888" />
|
||||
<text x="680" y="418" text-anchor="middle">100</text>
|
||||
<line x1="65" y1="400" x2="70" y2="400" stroke="#888" />
|
||||
<text x="62" y="400" text-anchor="end" dominant-baseline="middle">9.552e1</text>
|
||||
<line x1="65" y1="279.999999999999" x2="70" y2="279.999999999999" stroke="#888" />
|
||||
<text x="62" y="279.999999999999" text-anchor="end" dominant-baseline="middle">9.608e1</text>
|
||||
<line x1="65" y1="160.00000000000102" x2="70" y2="160.00000000000102" stroke="#888" />
|
||||
<text x="62" y="160.00000000000102" text-anchor="end" dominant-baseline="middle">9.664e1</text>
|
||||
<line x1="65" y1="40" x2="70" y2="40" stroke="#888" />
|
||||
<text x="62" y="40" text-anchor="end" dominant-baseline="middle">9.721e1</text>
|
||||
<text x="375" y="438" text-anchor="middle">generation</text>
|
||||
<text x="15" y="220" text-anchor="middle" transform="rotate(-90 15 220)">hypervolume</text>
|
||||
<polyline points="70.00,400.00 76.10,113.85 82.20,59.99 88.30,44.22 94.40,45.84 100.50,47.93 106.60,45.55 112.70,40.78 118.80,43.34 124.90,41.17 131.00,42.71 137.10,44.97 143.20,43.62 149.30,43.69 155.40,46.67 161.50,40.97 167.60,41.76 173.70,41.27 179.80,46.13 185.90,42.18 192.00,44.17 198.10,42.62 204.20,41.90 210.30,42.69 216.40,40.00 222.50,42.66 228.60,48.64 234.70,47.22 240.80,42.14 246.90,45.35 253.00,48.46 259.10,42.98 265.20,44.96 271.30,44.55 277.40,44.30 283.50,43.54 289.60,43.88 295.70,42.70 301.80,44.10 307.90,46.88 314.00,46.56 320.10,42.39 326.20,44.45 332.30,45.03 338.40,46.44 344.50,44.44 350.60,45.79 356.70,49.23 362.80,42.47 368.90,44.42 375.00,50.51 381.10,44.45 387.20,42.54 393.30,42.50 399.40,42.83 405.50,40.56 411.60,49.78 417.70,47.03 423.80,44.17 429.90,45.73 436.00,42.66 442.10,44.31 448.20,49.69 454.30,41.19 460.40,41.14 466.50,41.87 472.60,41.88 478.70,41.41 484.80,43.24 490.90,45.61 497.00,45.55 503.10,45.16 509.20,46.10 515.30,42.66 521.40,42.46 527.50,48.78 533.60,46.56 539.70,42.49 545.80,43.23 551.90,47.51 558.00,44.28 564.10,45.05 570.20,43.75 576.30,44.15 582.40,42.29 588.50,41.54 594.60,43.25 600.70,41.56 606.80,48.07 612.90,42.87 619.00,42.68 625.10,43.69 631.20,45.47 637.30,42.93 643.40,40.77 649.50,42.53 655.60,42.89 661.70,44.33 667.80,41.27 673.90,44.06 680.00,41.55" fill="none" stroke="#1f77b4" stroke-width="1.5" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.1 KiB |
@@ -0,0 +1,73 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 700 450" font-family="system-ui, sans-serif" font-size="12">
|
||||
<rect x="0" y="0" width="700" height="450" fill="white"/>
|
||||
<text x="60" y="22" font-size="16" font-weight="bold">NSGA-II on Schaffer N.1 — final Pareto front</text>
|
||||
<rect x="60" y="40" width="620" height="360" fill="none" stroke="#888" />
|
||||
<line x1="60" y1="400" x2="60" y2="405" stroke="#888" />
|
||||
<text x="60" y="418" text-anchor="middle">0.000</text>
|
||||
<line x1="266.66666666666663" y1="400" x2="266.66666666666663" y2="405" stroke="#888" />
|
||||
<text x="266.66666666666663" y="418" text-anchor="middle">1.333</text>
|
||||
<line x1="473.3333333333333" y1="400" x2="473.3333333333333" y2="405" stroke="#888" />
|
||||
<text x="473.3333333333333" y="418" text-anchor="middle">2.667</text>
|
||||
<line x1="680" y1="400" x2="680" y2="405" stroke="#888" />
|
||||
<text x="680" y="418" text-anchor="middle">4.000</text>
|
||||
<line x1="55" y1="400" x2="60" y2="400" stroke="#888" />
|
||||
<text x="52" y="400" text-anchor="end" dominant-baseline="middle">0.000</text>
|
||||
<line x1="55" y1="280" x2="60" y2="280" stroke="#888" />
|
||||
<text x="52" y="280" text-anchor="end" dominant-baseline="middle">1.333</text>
|
||||
<line x1="55" y1="160" x2="60" y2="160" stroke="#888" />
|
||||
<text x="52" y="160" text-anchor="end" dominant-baseline="middle">2.666</text>
|
||||
<line x1="55" y1="40" x2="60" y2="40" stroke="#888" />
|
||||
<text x="52" y="40" text-anchor="end" dominant-baseline="middle">3.999</text>
|
||||
<text x="370" y="438" text-anchor="middle">f1</text>
|
||||
<text x="15" y="220" text-anchor="middle" transform="rotate(-90 15 220)">f2</text>
|
||||
<circle cx="680.00" cy="400.00" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="60.00" cy="40.00" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="544.53" cy="395.16" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="596.60" cy="398.25" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="147.76" cy="259.91" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="77.82" cy="151.68" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="166.09" cy="276.21" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="512.44" cy="392.35" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="457.04" cy="385.63" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="122.47" cy="232.25" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="133.03" cy="244.68" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="92.63" cy="186.20" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="277.90" cy="340.31" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="329.99" cy="358.35" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="86.30" cy="173.00" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="83.00" cy="165.28" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="479.72" cy="388.69" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="71.88" cy="132.73" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="177.45" cy="285.16" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="60.12" cy="49.89" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="139.30" cy="251.43" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="228.69" cy="317.60" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="661.27" cy="399.92" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="247.24" cy="326.94" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="97.14" cy="194.62" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="291.01" cy="345.35" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="188.72" cy="293.31" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="237.44" cy="322.14" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="62.32" cy="82.64" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="400.75" cy="375.91" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="314.75" cy="353.60" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="206.51" cy="304.92" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="436.65" cy="382.48" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="263.18" cy="334.19" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="344.89" cy="362.64" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="67.81" cy="116.23" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="61.00" cy="68.34" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="60.73" cy="64.24" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="641.54" cy="399.64" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="115.80" cy="223.58" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="216.38" cy="310.79" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="169.95" cy="279.34" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="384.40" cy="372.44" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="65.45" cy="104.28" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="633.74" cy="399.48" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="64.21" cy="96.82" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="107.71" cy="212.00" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="414.47" cy="378.59" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="616.88" cy="399.02" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
<circle cx="419.20" cy="379.46" r="3" fill="#1f77b4" stroke="#0d4a8a" stroke-width="0.5" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 6.2 KiB |
@@ -0,0 +1,89 @@
|
||||
//! Visualize an NSGA-II run on Schaffer N.1 — produces two SVGs:
|
||||
//! `pareto_front.svg` (scatter plot of the final front) and
|
||||
//! `convergence.svg` (best-so-far hypervolume per generation).
|
||||
//!
|
||||
//! Uses the `heuropt-plot` companion crate plus the v0.6 observer
|
||||
//! API (`Periodic`) to record per-generation hypervolume into a Vec
|
||||
//! during the run.
|
||||
//!
|
||||
//! Run with: `cargo run --release --example visualize`
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::ops::ControlFlow;
|
||||
|
||||
use heuropt::metrics::hypervolume_2d;
|
||||
use heuropt::prelude::*;
|
||||
use heuropt_plot::{convergence_svg, 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 ref_point = [10.0, 10.0];
|
||||
|
||||
// Per-generation hypervolume trace, recorded by the observer.
|
||||
let history: RefCell<Vec<f64>> = RefCell::new(Vec::new());
|
||||
|
||||
let mut recorder = |snap: &Snapshot<'_, Vec<f64>>| -> ControlFlow<()> {
|
||||
let hv = match snap.pareto_front {
|
||||
Some(front) => hypervolume_2d(front, snap.objectives, ref_point),
|
||||
None => 0.0,
|
||||
};
|
||||
history.borrow_mut().push(hv);
|
||||
ControlFlow::Continue(())
|
||||
};
|
||||
|
||||
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_with(&problem, &mut recorder);
|
||||
|
||||
let front_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", front_svg).expect("write pareto_front.svg");
|
||||
|
||||
let trace = history.borrow();
|
||||
let conv_svg = convergence_svg(
|
||||
&trace,
|
||||
700,
|
||||
450,
|
||||
"NSGA-II on Schaffer N.1 — hypervolume per generation",
|
||||
"hypervolume",
|
||||
false, // higher is better
|
||||
);
|
||||
std::fs::write("convergence.svg", conv_svg).expect("write convergence.svg");
|
||||
|
||||
println!("Final front size: {}", result.pareto_front.len());
|
||||
println!(
|
||||
"Final hypervolume: {:.4}",
|
||||
trace.last().copied().unwrap_or(0.0)
|
||||
);
|
||||
println!("Wrote pareto_front.svg and convergence.svg");
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "heuropt-plot"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.85"
|
||||
authors = ["Stephen Waits <steve@waits.net>"]
|
||||
description = "Lightweight SVG visualization for heuropt Pareto fronts and convergence traces."
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/swaits/heuropt"
|
||||
homepage = "https://github.com/swaits/heuropt"
|
||||
documentation = "https://docs.rs/heuropt-plot"
|
||||
keywords = ["optimization", "pareto", "svg", "plotting", "heuropt"]
|
||||
categories = ["algorithms", "visualization"]
|
||||
|
||||
[dependencies]
|
||||
heuropt = { version = "0.7", path = ".." }
|
||||
@@ -0,0 +1,46 @@
|
||||
# heuropt-plot
|
||||
|
||||
[](https://crates.io/crates/heuropt-plot)
|
||||
[](https://docs.rs/heuropt-plot)
|
||||
[](../LICENSE)
|
||||
|
||||
Lightweight SVG plotting helpers for [`heuropt`](https://crates.io/crates/heuropt)
|
||||
results.
|
||||
|
||||
Hand-rolled SVG output (no `plotters`, no `tiny-skia`, no
|
||||
heavyweight dependency) so adding `heuropt-plot` to your project
|
||||
costs ~20 KB of compiled code.
|
||||
|
||||
## What's in the box
|
||||
|
||||
- `pareto_front_svg` — render a 2-objective Pareto front as an SVG
|
||||
scatter plot with axes and labels.
|
||||
- `convergence_svg` — render a "best fitness so far" trace as an
|
||||
SVG line plot.
|
||||
|
||||
Output is a `String` of valid SVG. Write it to a file, embed it in
|
||||
HTML, or pipe it to a browser.
|
||||
|
||||
## Example
|
||||
|
||||
```rust
|
||||
use heuropt::prelude::*;
|
||||
use heuropt_plot::pareto_front_svg;
|
||||
|
||||
let space = ObjectiveSpace::new(vec![
|
||||
Objective::minimize("f1"),
|
||||
Objective::minimize("f2"),
|
||||
]);
|
||||
let front = vec![
|
||||
Candidate::new((), Evaluation::new(vec![0.0, 1.0])),
|
||||
Candidate::new((), Evaluation::new(vec![0.5, 0.5])),
|
||||
Candidate::new((), Evaluation::new(vec![1.0, 0.0])),
|
||||
];
|
||||
|
||||
let svg = pareto_front_svg(&front, &space, 600, 400, "Sample front");
|
||||
std::fs::write("front.svg", svg).unwrap();
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT — see [LICENSE](../LICENSE) at the repo root.
|
||||
@@ -0,0 +1,368 @@
|
||||
//! Lightweight SVG plotting helpers for `heuropt` results.
|
||||
//!
|
||||
//! Two core primitives:
|
||||
//!
|
||||
//! - [`pareto_front_svg`] — render a 2-objective Pareto front as an
|
||||
//! SVG scatter plot with axes and labels.
|
||||
//! - [`convergence_svg`] — render a per-generation "best-fitness so
|
||||
//! far" trace as an SVG line plot.
|
||||
//!
|
||||
//! Hand-rolled SVG output (no `plotters` / `tiny-skia` dep) so the
|
||||
//! crate stays a tiny optional dependency. Output is a `String` of
|
||||
//! valid SVG — write it to a file, embed it in HTML, or pipe it to a
|
||||
//! browser.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```
|
||||
//! use heuropt::prelude::*;
|
||||
//! use heuropt_plot::pareto_front_svg;
|
||||
//!
|
||||
//! let space = ObjectiveSpace::new(vec![
|
||||
//! Objective::minimize("f1"),
|
||||
//! Objective::minimize("f2"),
|
||||
//! ]);
|
||||
//! let front = vec![
|
||||
//! Candidate::new((), Evaluation::new(vec![0.0, 1.0])),
|
||||
//! Candidate::new((), Evaluation::new(vec![0.5, 0.5])),
|
||||
//! Candidate::new((), Evaluation::new(vec![1.0, 0.0])),
|
||||
//! ];
|
||||
//! let svg = pareto_front_svg(&front, &space, 600, 400, "Sample front");
|
||||
//! assert!(svg.starts_with("<svg"));
|
||||
//! assert!(svg.contains("</svg>"));
|
||||
//! ```
|
||||
|
||||
use std::fmt::Write as _;
|
||||
|
||||
use heuropt::core::candidate::Candidate;
|
||||
use heuropt::core::objective::ObjectiveSpace;
|
||||
|
||||
/// Render a 2-objective Pareto front as an SVG scatter plot.
|
||||
///
|
||||
/// `width` and `height` are the SVG viewport dimensions in pixels.
|
||||
/// `title` is rendered at the top.
|
||||
///
|
||||
/// Points are plotted in minimization-oriented coordinates.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If `objectives.len() != 2`.
|
||||
pub fn pareto_front_svg<D>(
|
||||
front: &[Candidate<D>],
|
||||
objectives: &ObjectiveSpace,
|
||||
width: u32,
|
||||
height: u32,
|
||||
title: &str,
|
||||
) -> String {
|
||||
assert_eq!(
|
||||
objectives.len(),
|
||||
2,
|
||||
"pareto_front_svg requires exactly 2 objectives",
|
||||
);
|
||||
let oriented: Vec<[f64; 2]> = front
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let m = objectives.as_minimization(&c.evaluation.objectives);
|
||||
[m[0], m[1]]
|
||||
})
|
||||
.collect();
|
||||
let (xs_label, ys_label) = (
|
||||
objectives.objectives[0].name.as_str(),
|
||||
objectives.objectives[1].name.as_str(),
|
||||
);
|
||||
|
||||
let (xmin, xmax) = bounds(oriented.iter().map(|p| p[0]));
|
||||
let (ymin, ymax) = bounds(oriented.iter().map(|p| p[1]));
|
||||
let xspan = (xmax - xmin).max(1e-12);
|
||||
let yspan = (ymax - ymin).max(1e-12);
|
||||
|
||||
// Margins so axes/labels have room.
|
||||
let m_left = 60.0_f64;
|
||||
let m_right = 20.0_f64;
|
||||
let m_top = 40.0_f64;
|
||||
let m_bot = 50.0_f64;
|
||||
let plot_w = width as f64 - m_left - m_right;
|
||||
let plot_h = height as f64 - m_top - m_bot;
|
||||
|
||||
let to_x = |v: f64| m_left + (v - xmin) / xspan * plot_w;
|
||||
// Y is inverted: lower minimization value → higher pixel.
|
||||
let to_y = |v: f64| m_top + plot_h - (v - ymin) / yspan * plot_h;
|
||||
|
||||
let mut out = String::new();
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 {width} {height}\" \
|
||||
font-family=\"system-ui, sans-serif\" font-size=\"12\">",
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <rect x=\"0\" y=\"0\" width=\"{width}\" height=\"{height}\" fill=\"white\"/>",
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"22\" font-size=\"16\" font-weight=\"bold\">{title}</text>",
|
||||
x = m_left,
|
||||
title = escape_xml(title),
|
||||
);
|
||||
|
||||
// Axes box.
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" fill=\"none\" stroke=\"#888\" />",
|
||||
m_left, m_top, plot_w, plot_h,
|
||||
);
|
||||
|
||||
// X-axis ticks (3 ticks).
|
||||
for i in 0..=3 {
|
||||
let t = i as f64 / 3.0;
|
||||
let v = xmin + t * xspan;
|
||||
let x = to_x(v);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <line x1=\"{x}\" y1=\"{y0}\" x2=\"{x}\" y2=\"{y1}\" stroke=\"#888\" />",
|
||||
y0 = m_top + plot_h,
|
||||
y1 = m_top + plot_h + 5.0,
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"{y}\" text-anchor=\"middle\">{v:.3}</text>",
|
||||
y = m_top + plot_h + 18.0,
|
||||
);
|
||||
}
|
||||
// Y-axis ticks.
|
||||
for i in 0..=3 {
|
||||
let t = i as f64 / 3.0;
|
||||
let v = ymin + t * yspan;
|
||||
let y = to_y(v);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <line x1=\"{x0}\" y1=\"{y}\" x2=\"{x1}\" y2=\"{y}\" stroke=\"#888\" />",
|
||||
x0 = m_left - 5.0,
|
||||
x1 = m_left,
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"{y}\" text-anchor=\"end\" dominant-baseline=\"middle\">{v:.3}</text>",
|
||||
x = m_left - 8.0,
|
||||
);
|
||||
}
|
||||
|
||||
// Axis labels.
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"{y}\" text-anchor=\"middle\">{xs_label}</text>",
|
||||
x = m_left + plot_w / 2.0,
|
||||
y = height as f64 - 12.0,
|
||||
xs_label = escape_xml(xs_label),
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"15\" y=\"{y}\" text-anchor=\"middle\" \
|
||||
transform=\"rotate(-90 15 {y})\">{ys_label}</text>",
|
||||
y = m_top + plot_h / 2.0,
|
||||
ys_label = escape_xml(ys_label),
|
||||
);
|
||||
|
||||
// Points.
|
||||
for p in &oriented {
|
||||
let cx = to_x(p[0]);
|
||||
let cy = to_y(p[1]);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <circle cx=\"{cx:.2}\" cy=\"{cy:.2}\" r=\"3\" fill=\"#1f77b4\" \
|
||||
stroke=\"#0d4a8a\" stroke-width=\"0.5\" />",
|
||||
);
|
||||
}
|
||||
|
||||
out.push_str("</svg>");
|
||||
out
|
||||
}
|
||||
|
||||
/// Render a per-generation "best fitness so far" trace as an SVG line
|
||||
/// plot. `bests[i]` is the best fitness *after* generation `i`.
|
||||
///
|
||||
/// `direction_minimize` controls which way is "improvement": `true`
|
||||
/// for minimize problems, `false` for maximize.
|
||||
pub fn convergence_svg(
|
||||
bests: &[f64],
|
||||
width: u32,
|
||||
height: u32,
|
||||
title: &str,
|
||||
y_axis_label: &str,
|
||||
_direction_minimize: bool,
|
||||
) -> String {
|
||||
let n = bests.len();
|
||||
if n == 0 {
|
||||
return format!(
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 {width} {height}\">\
|
||||
<text x=\"10\" y=\"20\">{}</text></svg>",
|
||||
escape_xml(title)
|
||||
);
|
||||
}
|
||||
|
||||
let (ymin, ymax) = bounds(bests.iter().copied());
|
||||
let yspan = (ymax - ymin).max(1e-12);
|
||||
let xspan = (n - 1).max(1) as f64;
|
||||
|
||||
let m_left = 70.0_f64;
|
||||
let m_right = 20.0_f64;
|
||||
let m_top = 40.0_f64;
|
||||
let m_bot = 50.0_f64;
|
||||
let plot_w = width as f64 - m_left - m_right;
|
||||
let plot_h = height as f64 - m_top - m_bot;
|
||||
|
||||
let to_x = |i: usize| m_left + (i as f64) / xspan * plot_w;
|
||||
let to_y = |v: f64| m_top + plot_h - (v - ymin) / yspan * plot_h;
|
||||
|
||||
let mut out = String::new();
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 {width} {height}\" \
|
||||
font-family=\"system-ui, sans-serif\" font-size=\"12\">",
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <rect x=\"0\" y=\"0\" width=\"{width}\" height=\"{height}\" fill=\"white\"/>",
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"22\" font-size=\"16\" font-weight=\"bold\">{title}</text>",
|
||||
x = m_left,
|
||||
title = escape_xml(title),
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" fill=\"none\" stroke=\"#888\" />",
|
||||
m_left, m_top, plot_w, plot_h,
|
||||
);
|
||||
|
||||
// X axis: generation index.
|
||||
for i in 0..=4 {
|
||||
let t = i as f64 / 4.0;
|
||||
let g = (t * (n - 1) as f64).round() as usize;
|
||||
let x = to_x(g);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <line x1=\"{x}\" y1=\"{y0}\" x2=\"{x}\" y2=\"{y1}\" stroke=\"#888\" />",
|
||||
y0 = m_top + plot_h,
|
||||
y1 = m_top + plot_h + 5.0,
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"{y}\" text-anchor=\"middle\">{g}</text>",
|
||||
y = m_top + plot_h + 18.0,
|
||||
);
|
||||
}
|
||||
// Y ticks.
|
||||
for i in 0..=3 {
|
||||
let t = i as f64 / 3.0;
|
||||
let v = ymin + t * yspan;
|
||||
let y = to_y(v);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <line x1=\"{x0}\" y1=\"{y}\" x2=\"{x1}\" y2=\"{y}\" stroke=\"#888\" />",
|
||||
x0 = m_left - 5.0,
|
||||
x1 = m_left,
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"{y}\" text-anchor=\"end\" dominant-baseline=\"middle\">{v:.3e}</text>",
|
||||
x = m_left - 8.0,
|
||||
);
|
||||
}
|
||||
|
||||
// Axis labels.
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"{x}\" y=\"{y}\" text-anchor=\"middle\">generation</text>",
|
||||
x = m_left + plot_w / 2.0,
|
||||
y = height as f64 - 12.0,
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <text x=\"15\" y=\"{y}\" text-anchor=\"middle\" \
|
||||
transform=\"rotate(-90 15 {y})\">{label}</text>",
|
||||
y = m_top + plot_h / 2.0,
|
||||
label = escape_xml(y_axis_label),
|
||||
);
|
||||
|
||||
// Polyline.
|
||||
let mut points = String::new();
|
||||
for (i, &v) in bests.iter().enumerate() {
|
||||
if i > 0 {
|
||||
points.push(' ');
|
||||
}
|
||||
let _ = write!(points, "{:.2},{:.2}", to_x(i), to_y(v));
|
||||
}
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" <polyline points=\"{points}\" fill=\"none\" stroke=\"#1f77b4\" stroke-width=\"1.5\" />",
|
||||
);
|
||||
|
||||
out.push_str("</svg>");
|
||||
out
|
||||
}
|
||||
|
||||
fn bounds<I: IntoIterator<Item = f64>>(it: I) -> (f64, f64) {
|
||||
let mut lo = f64::INFINITY;
|
||||
let mut hi = f64::NEG_INFINITY;
|
||||
for v in it {
|
||||
if v.is_finite() {
|
||||
if v < lo {
|
||||
lo = v;
|
||||
}
|
||||
if v > hi {
|
||||
hi = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
if lo.is_infinite() {
|
||||
(0.0, 1.0)
|
||||
} else if (hi - lo).abs() < f64::EPSILON {
|
||||
// All points equal — give a small artificial span.
|
||||
(lo - 0.5, hi + 0.5)
|
||||
} else {
|
||||
(lo, hi)
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_xml(s: &str) -> String {
|
||||
s.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use heuropt::core::evaluation::Evaluation;
|
||||
use heuropt::core::objective::Objective;
|
||||
|
||||
#[test]
|
||||
fn pareto_svg_well_formed() {
|
||||
let space = ObjectiveSpace::new(vec![Objective::minimize("f1"), Objective::minimize("f2")]);
|
||||
let front = vec![
|
||||
Candidate::new((), Evaluation::new(vec![0.0, 1.0])),
|
||||
Candidate::new((), Evaluation::new(vec![1.0, 0.0])),
|
||||
];
|
||||
let svg = pareto_front_svg(&front, &space, 400, 300, "test");
|
||||
assert!(svg.starts_with("<svg"));
|
||||
assert!(svg.contains("</svg>"));
|
||||
assert!(svg.contains("<circle"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convergence_svg_well_formed() {
|
||||
let bests = vec![10.0, 5.0, 2.0, 1.0, 0.5];
|
||||
let svg = convergence_svg(&bests, 400, 300, "convergence", "best", true);
|
||||
assert!(svg.starts_with("<svg"));
|
||||
assert!(svg.contains("polyline"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convergence_empty_returns_valid_svg() {
|
||||
let svg = convergence_svg(&[], 200, 100, "empty", "y", true);
|
||||
assert!(svg.contains("<svg"));
|
||||
assert!(svg.contains("</svg>"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user