Manuel 11a8534b38 fix: address 18 bugs found during codebase audit (#8)
* fix: address 18 bugs found during codebase audit

High severity:
- TPE/MOTPE: match parameters by exact distribution equality instead of
  flat-mapping over all param values, preventing cross-parameter mixing
- MultivariateTpeSampler: find_matching_param now uses search space
  distributions for exact matching instead of type+range heuristic
- JournalStorage: write_to_file no longer advances file_offset (left to
  refresh), both operations serialized under single io_lock mutex,
  refresh uses fetch_max and deduplicates by trial ID

Medium severity:
- NSGA-III: use actual Pareto front ranks for tournament selection
  instead of artificial cyclic indices
- sample_random: apply step quantization after log-scale sampling
- internal_bounds: return None for non-positive log-scale bounds
- SobolSampler: use per-trial dimension HashMap for concurrent safety
- JournalStorage refresh: protect with io_lock mutex, use fetch_max
- n_trials(): filter by TrialState::Complete as documented
- FloatParam: reject NaN/Infinity in validate()
- Pruners: assert n_min_trials >= 1, guard compute_percentile on empty
- Visualization: escape_js for importance chart parameter names

Low severity:
- save(): use peek_next_trial_id() from Storage trait
- csv_escape: handle carriage return per RFC 4180
- from_internal: use saturating arithmetic for stepped Int distributions
- BoolParam: bounds-check categorical index < 2
- min_max: skip NaN values with safe fallback

* ci: trigger CI on pull requests targeting any branch
2026-02-13 10:03:37 +01:00
2026-02-12 14:12:52 +01:00
2026-02-11 23:40:22 +01:00
2026-02-12 13:49:49 +01:00
2026-02-12 13:29:44 +01:00

optimizer

Bayesian and population-based optimization library with an Optuna-like API for hyperparameter tuning and black-box optimization. Supports 12 samplers, 8 pruners, multi-objective optimization, async parallelism, and persistent storage.

Docs Crates.io codecov

Quick Start

use optimizer::prelude::*;

let study: Study<f64> = Study::new(Direction::Minimize);
let x = FloatParam::new(-10.0, 10.0).name("x");

study.optimize(50, |trial| {
    let val = x.suggest(trial)?;
    Ok::<_, Error>((val - 3.0).powi(2))
}).unwrap();

let best = study.best_trial().unwrap();
println!("Best x = {:.4}, f(x) = {:.4}", best.get(&x).unwrap(), best.value);

Features at a Glance

  • Samplers — Random, TPE, Multivariate TPE, Grid, Sobol, CMA-ES, Gaussian Process, Differential Evolution, BOHB, NSGA-II, NSGA-III, MOEA/D
  • Pruners — Median, Percentile, Threshold, Patient, Hyperband, Successive Halving, Wilcoxon, Nop
  • Parameters — Float, Int, Categorical, Bool, and Enum types with .name() labels and typed access
  • Multi-objective — Pareto front extraction with NSGA-II/III and MOEA/D
  • Async & parallel — Concurrent trial evaluation with Tokio
  • Storage backends — In-memory (default) or JSONL journal for persistence and resumption
  • Visualization — HTML reports with optimization history and parameter importance
  • Analysis — fANOVA and Spearman correlation for parameter importance

Feature Flags

Flag Enables Default
async Async/parallel optimization (Tokio) No
derive #[derive(Categorical)] for enum parameters No
serde Serialization of trials and parameters No
journal JSONL storage backend (implies serde) No
sobol Sobol quasi-random sampler No
cma-es CMA-ES sampler (requires nalgebra) No
gp Gaussian Process sampler (requires nalgebra) No
tracing Structured logging with tracing No

Examples

cargo run --example basic_optimization                       # Minimize a quadratic — simplest possible usage
cargo run --example parameter_types --features derive        # All 5 param types + #[derive(Categorical)]
cargo run --example sampler_comparison                       # Compare Random, TPE, and Grid on the same problem
cargo run --example pruning                                  # Trial pruning with MedianPruner
cargo run --example early_stopping                           # Halt a study when a target is reached
cargo run --example async_parallel --features async          # Evaluate trials concurrently with tokio
cargo run --example journal_storage --features journal       # Persist trials to disk and resume later
cargo run --example ask_and_tell                             # Decouple sampling from evaluation
cargo run --example multi_objective                          # Optimize competing objectives + Pareto front

Learn More

License

MIT

S
Description
A Rust library for black-box optimization using Tree-Parzen Estimator (TPE).
Readme 831 KiB
Languages
Rust 99.8%
Shell 0.2%