Skip to content

About

A Rust library for black-box optimization using Tree-Parzen Estimator (TPE).

Resources

Stars

7 stars

Watchers

1 watching

Forks

Repository files navigation

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
  • DataFrame export — study.to_dataframe() into polars for ad-hoc analysis (feature polars)

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
polars Export trials to polars DataFrames (requires Rust 1.94+) No

Polars

use optimizer::polars::prelude::*; // the polars version optimizer was built against
use optimizer::prelude::*;

// polars errors are `PolarsError`, not `optimizer::Error`, so return a boxed error.
// Spell out `std::result::Result`: the optimizer prelude's `Result<T>` takes one argument.
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    // ... build and run `study` ...
    let df = study.to_dataframe()?;                                              // categorical = choice index
    let df = study.to_dataframe_with(&[&optimizer, &batch_norm, &activation])?; // "adam", true, "Relu"
    let top = study.top_trials(5).to_dataframe()?;
    Ok(())
}

Name your params with .name() so columns get readable labels. To call .lazy() on the result, add polars directly at the same minor version as optimizer's and enable lazy (a polars/lazy entry in optimizer's features list does not work):

polars = { version = "0.55", default-features = false, features = ["lazy"] } # must match optimizer's polars minor

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
cargo run --example dataframe --features polars              # Analyse trials with polars

Learn More

License

MIT

About

A Rust library for black-box optimization using Tree-Parzen Estimator (TPE).

Resources

Stars

7 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages