Overview
Theank-opt module provides pure Rust hyper-parameter optimization for ANK backtests and agent-based models.
Key features:
- Multiple algorithms: grid search, random search, genetic algorithm (GA), differential evolution (DE)
- Successive halving scheduler for multi-fidelity optimization
- Mixed parameter spaces: continuous (real), discrete (int), categorical
- Log-scale parameters for learning rates, thresholds, etc.
- Grid: Cartesian product enumeration
- Random: Uniform random sampling
- GA: Genetic algorithm with tournament selection
- DE: Differential evolution for continuous optimization
Core Types
Space
Parameter space (Cartesian product of dimensions).Methods
Create a new parameter space.
Add a real-valued dimension.
Add an integer-valued dimension.
Add a categorical dimension.
Set log scale for the last real dimension.Useful for learning rates, decay factors, etc.
Set step size for the last integer dimension.
Dim (enum)
Spec for one dimension.Variants
Real-valued dimension.
log: If true, samples uniformly in log space
Integer-valued dimension.
step: Increment between valid values
Categorical dimension.
choices: List of valid values
Value (enum)
Parameter value.Variants
Real-valued parameter.
Integer-valued parameter.
Categorical parameter (index into choices).
Methods
Convert to f64 (real, int, or categorical index as f64).
Convert to i64 (real rounded, int, or categorical index as i64).
Get categorical index.
Params
Named parameter set. Type:IndexMap<String, Value>
Trial
Trial result.Fields
Trial ID.
Parameter set.
Score (higher is better).
Resources used (e.g., simulation ticks).
OptResult
Overall result with leaderboard.Fields
Best trial.
All trials.
Objective Trait
Objective to optimize: higher score is better.Methods
Evaluate a parameter setting with an optional resource budget (e.g., ticks) and a seed.Parameters:
params: Parameter values to evaluateresources: Computational budget (e.g., number of backtest ticks)seed: Random seed for reproducibility
Algorithms
Algo (enum)
Sampling/optimization algorithms.Variants
Grid search (Cartesian product enumeration).
Uniform random search.
Genetic algorithm.
Differential evolution.
Methods
Create grid search.
Create random search.
Create genetic algorithm builder.
Create differential evolution builder.
Grid
Cartesian grid search (finite).Methods
Enumerate all parameter combinations in the space.Note: For real dimensions, uses 10 bins (heuristic).
Random
Uniform random sampler.Methods
Create a new random sampler.
Sample one parameter set from the space.
GA (Genetic Algorithm)
Simple genetic algorithm (mixed spaces).Methods
Create a new genetic algorithm.Defaults:
- Population: 24
- Generations: 30
- Mutation probability: 0.10
- Tournament size: 3
Set population size.
Set number of generations.
Set mutation probability (0.0 to 1.0).
DE (Differential Evolution)
Differential evolution (continuous dims primarily).Methods
Create a new differential evolution optimizer.Defaults:
- Population: 24
- Generations: 60
- Mutation factor (F): 0.7
- Crossover rate (CR): 0.9
Set population size.
Set number of generations.
Set mutation factor (0.1 to 1.0).
Set crossover rate (0.0 to 1.0).
Scheduler
Scheduler for multi-fidelity evaluation (Successive Halving).Fields
Minimum resources.
Maximum resources.
Halving factor.
Methods
Create SHA schedule.Example:
min_r=100, max_r=10_000, eta=3This creates a ladder: [100, 300, 900, 2700, 8100, 10000]Return resource ladder:
[r0, r1, ..., rK].Budget
Budget configuration: limit by trials and/or wall-clock (ms).Fields
Number of trials.
Maximum wall-clock time in ms.
Methods
Set number of trials.
Set maximum wall-clock time in ms.
Optimizer
Optimizer for parameter search.Methods
Create a new optimizer.Parameters:
algo: Optimization algorithmsched: Optional multi-fidelity scheduler
Run the optimization algorithm on the given objective function.Returns: The best trial found and the complete history of all evaluations.
Usage Examples
Basic optimization
Log-scale parameters
Multi-fidelity optimization
Genetic algorithm tuning
Grid search with time limit
Analyzing results
Algorithm Selection Guide
Grid Search
Use when:- Few parameters (< 4 dimensions)
- Need exhaustive coverage
- Computation is cheap
- Deterministic, reproducible
- Guaranteed to find global optimum (if fine enough)
- Exponential growth in search space
- Inefficient for high dimensions
Random Search
Use when:- Many parameters (> 5 dimensions)
- Limited budget
- Quick baseline needed
- Simple, fast
- Good coverage in high dimensions
- No hyperparameters to tune
- No exploitation of promising regions
- May miss narrow optima
Genetic Algorithm (GA)
Use when:- Mixed parameter types (real + int + categorical)
- Multimodal objective
- Medium budget (100-1000 evals)
- Handles mixed spaces well
- Good exploration-exploitation balance
- Robust to noise
- Requires tuning (pop size, mutation rate)
- Slower than random search initially
Differential Evolution (DE)
Use when:- Mostly continuous parameters
- Smooth objective function
- Medium-high budget (200+ evals)
- Very effective for continuous optimization
- Few hyperparameters
- Fast convergence
- Limited support for categorical dims
- Requires more function evaluations than GA
Best Practices
1. Start with Random Search
Get a baseline with 20-50 random samples before running more expensive algorithms.2. Use Log Scale for Rate Parameters
Learning rates, decay factors, and thresholds often span orders of magnitude:3. Set Appropriate Step Sizes
For integer parameters, use meaningful steps:4. Use Multi-Fidelity for Expensive Objectives
Run cheap evaluations first, then scale up resources for promising configs:5. Set Time Limits
Prevent runaway optimization:6. Normalize Your Objective
Return scores in a consistent range (e.g., 0-1) for better algorithm performance.Successive Halving Explained
Successive Halving (SHA) is a multi-fidelity optimization technique:- Start cheap: Evaluate all candidates with minimal resources
- Eliminate losers: Keep only top performers
- Scale up: Give survivors more resources
- Repeat: Until one candidate gets full budget
Scheduler::sha(100, 10_000, 3)