2026-01-30 18:08:13 +01:00
|
|
|
# optimizer
|
2026-01-30 17:33:33 +01:00
|
|
|
|
2026-01-30 19:58:18 +01:00
|
|
|
A Rust library for black-box optimization with multiple sampling strategies.
|
2026-01-30 17:33:33 +01:00
|
|
|
|
2026-01-30 18:08:13 +01:00
|
|
|
[](https://docs.rs/optimizer)
|
|
|
|
|
[](https://crates.io/crates/optimizer)
|
|
|
|
|
[](https://codecov.io/gh/raimannma/rust-optimizer)
|
2026-01-30 17:33:33 +01:00
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
|
|
- Optuna-like API for hyperparameter optimization
|
2026-01-30 19:58:18 +01:00
|
|
|
- Multiple sampling strategies:
|
|
|
|
|
- **Random Search** - Simple random sampling for baseline comparisons
|
|
|
|
|
- **TPE (Tree-Parzen Estimator)** - Bayesian optimization for efficient search
|
|
|
|
|
- **Grid Search** - Exhaustive search over a specified parameter grid
|
2026-01-30 17:33:33 +01:00
|
|
|
- Float, integer, and categorical parameter types
|
|
|
|
|
- Log-scale and stepped parameter sampling
|
|
|
|
|
- Sync and async optimization with parallel trial evaluation
|
|
|
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
|
|
|
|
|
|
```rust
|
2026-01-30 19:58:18 +01:00
|
|
|
use optimizer::{Direction, Study};
|
|
|
|
|
use optimizer::sampler::tpe::TpeSampler;
|
2026-01-30 17:33:33 +01:00
|
|
|
|
2026-01-30 19:58:18 +01:00
|
|
|
let sampler = TpeSampler::builder().seed(42).build().unwrap();
|
2026-01-30 17:33:33 +01:00
|
|
|
let study: Study<f64> = Study::with_sampler(Direction::Minimize, sampler);
|
|
|
|
|
|
|
|
|
|
study
|
|
|
|
|
.optimize_with_sampler(20, |trial| {
|
|
|
|
|
let x = trial.suggest_float("x", -10.0, 10.0)?;
|
2026-01-30 19:24:58 +01:00
|
|
|
Ok::<_, optimizer::Error>(x * x)
|
2026-01-30 17:33:33 +01:00
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let best = study.best_trial().unwrap();
|
|
|
|
|
println!("Best value: {} at x={:?}", best.value, best.params);
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-30 19:58:18 +01:00
|
|
|
## Samplers
|
|
|
|
|
|
|
|
|
|
### Random Search
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
use optimizer::{Direction, Study};
|
|
|
|
|
use optimizer::sampler::random::RandomSampler;
|
|
|
|
|
|
|
|
|
|
let study: Study<f64> = Study::with_sampler(
|
|
|
|
|
Direction::Minimize,
|
|
|
|
|
RandomSampler::with_seed(42),
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### TPE (Tree-Parzen Estimator)
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
use optimizer::{Direction, Study};
|
|
|
|
|
use optimizer::sampler::tpe::TpeSampler;
|
|
|
|
|
|
|
|
|
|
let sampler = TpeSampler::builder()
|
|
|
|
|
.gamma(0.15) // Quantile for good/bad split
|
|
|
|
|
.n_startup_trials(20) // Random trials before TPE kicks in
|
|
|
|
|
.n_ei_candidates(32) // Candidates to evaluate
|
|
|
|
|
.seed(42)
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let study: Study<f64> = Study::with_sampler(Direction::Minimize, sampler);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Grid Search
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
use optimizer::{Direction, Study};
|
|
|
|
|
use optimizer::sampler::grid::GridSearchSampler;
|
|
|
|
|
|
|
|
|
|
let sampler = GridSearchSampler::builder()
|
|
|
|
|
.n_points_per_param(10) // Number of points per parameter dimension
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let study: Study<f64> = Study::with_sampler(Direction::Minimize, sampler);
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-30 17:33:33 +01:00
|
|
|
## Feature Flags
|
|
|
|
|
|
|
|
|
|
- `async` - Enable async optimization methods (requires tokio)
|
|
|
|
|
|
|
|
|
|
## Documentation
|
|
|
|
|
|
2026-01-30 18:08:13 +01:00
|
|
|
Full API documentation is available at [docs.rs/optimizer](https://docs.rs/optimizer).
|
2026-01-30 17:33:33 +01:00
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
MIT
|