tempura-sa
Temperature-Driven Optimization Primitives for Rust.
Tempura is a zero-dependency, production-grade annealing framework implementing Simulated Annealing, Parallel Tempering, and Population Annealing. Deterministic by construction — same seed, same result, every time.
Features
- Three algorithms — SA, Parallel Tempering (replica exchange), Population Annealing
- Six cooling schedules — Linear, Exponential, Logarithmic (Hajek-convergent), Fast, Cauchy, Adaptive
- Four move operators — Gaussian, Swap, Neighbor, and the
MoveOperatortrait for custom moves - Two PRNGs — Xoshiro256++ (default) and PCG-64; both period > 2¹²⁸
- Type-state builders — required fields enforced at compile time, no runtime panics
- Branchless hot paths — Metropolis, Barker, and log-domain acceptance;
fast_exp(~5 cycles) - Rich diagnostics — acceptance rate, improvement ratio, optional sampled trajectory
- Zero runtime dependencies — no
rand, nolibc, nonum-traits - MSRV 1.70 — stable Rust only
Quick Start
[]
= "0.1"
use *;
// Minimize f(x) = (x - 3.7)²
let result =
.objective
.move_op
.schedule
.seed
.iterations
.build
.unwrap
.run;
println!; // ≈ 3.700000
println!; // ≈ 0.000000
Algorithms
| Algorithm | Type | When to use |
|---|---|---|
Annealer |
Single-solution SA | General-purpose, fast, low memory |
ParallelTempering |
Replica exchange | Rugged landscapes, barrier crossing |
PopulationAnnealing |
Ensemble resampling | Partition function estimation, broad exploration |
Parallel Tempering
use *;
use PTBuilder;
let result = new
.temperatures
.objective
.move_op
.seed
.steps
.build
.unwrap
.run;
Population Annealing
use *;
use PABuilder;
let result = new
.population_size
.objective
.move_op
.schedule
.seed
.sweeps_per_step
.annealing_steps
.build
.unwrap
.run;
Cooling Schedules
| Schedule | Formula | Convergence guarantee |
|---|---|---|
Linear |
T₀ − α·k | None |
Exponential |
T₀ · αᵏ | None (fast in practice) |
Logarithmic |
c / ln(1 + k) | Yes — Hajek (1988) |
Fast |
T₀ / (1 + k) | None |
Cauchy |
T₀ / (1 + k²) | None |
Adaptive |
Feedback-controlled | Empirical |
Move Operators
| Operator | State type | Notes |
|---|---|---|
GaussianMove |
f64 (or any float) |
Box-Muller N(0, σ) perturbation |
SwapMove |
Vec<usize> |
Uniform random transposition |
SwapMoveReversible |
Vec<usize> |
Swap + undo for ReversibleMove |
NeighborMove |
Vec<i64> |
Bounded ±1 walk with exact Hastings correction |
Implement MoveOperator<S> for any custom state type:
use *;
;
Diagnostics
let result = /* ... */;
println!;
println!;
println!;
println!;
// Optional trajectory (enable with .trajectory_interval(n))
if let Some = &result.trajectory
Performance
Hot-path primitives (measured on x86-64, single core):
| Primitive | Throughput |
|---|---|
Xoshiro256PlusPlus::next_f64 |
~1 ns |
metropolis_accept |
~2 ns (branchless) |
metropolis_accept_log_domain |
~3 ns (exp-free) |
fast_exp |
~5 cycles, 0.1% relative error |
| Full SA step (f64 + Gaussian + Metropolis) | ~15 ns |
Run the benchmarks:
Test Suites
Ten hypothesis-driven statistical test suites covering 37 tests:
| Suite | Hypothesis | Tests |
|---|---|---|
| H-01 | Boltzmann distribution convergence, ergodicity | 4 |
| H-02 | Detailed balance, Metropolis-Hastings, Barker | 4 |
| H-03 | Cooling schedule ordering, log-schedule superiority | 5 |
| H-04 | Acceptance rate monotonicity, adaptive stability | 4 |
| H-05 | Parallel tempering swap rates, barrier crossing | 4 |
| H-06 | Population annealing effective fraction | 3 |
| H-07 | Quantum tunneling probability scaling | 3 |
| H-08 | RNG independence, period, statistical quality | 4 |
| H-09 | Cache layout, SoA vs AoA, determinism | 4 |
| H-10 | Branchless correctness, fast_exp accuracy | 6 |
Examples
Three showcase example files demonstrating real-world SA applications:
Safety
#![forbid(unsafe_code)]— zero unsafe in the library- No
unwrap()in library code; all error paths returnResult<_, AnnealError> AnnealErrorhas two variants:MissingField { field }andInvalidParameter { name, reason }
Minimum Supported Rust Version
1.70 (stable). Uses only stable language features. MSRV bumps are treated as breaking changes and gated behind a minor version bump.
Contributing
See CONTRIBUTING.md for the development workflow, verification gate, and pull request process.
Security
See SECURITY.md for the vulnerability reporting policy.
License
Licensed under either of:
at your option.