Skip to content

Optimization Guide

The Math SDK includes a Rust-based genetic algorithm for optimizing win distributions to meet target RTP (Return to Player), hit rates, and other constraints.

Overview

The optimization algorithm: - Adjusts win probabilities and multiplier distributions - Uses genetic algorithms to search for optimal configurations - Can target specific RTP, hit rate, and average win constraints - Scales specific win ranges to fine-tune distributions

Setup

1. Install Rust/Cargo

# Install Rust toolchain
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Verify installation
cargo --version

2. Build Optimization Program

cd optimization_program
cargo build --release
cd ..

Configuration

Optimization is configured in each game's game_optimization.py file:

Basic Structure

from optimization_program.optimization_config import (
    ConstructConditions,
    ConstructScaling,
    ConstructParameters,
)

class OptimizationSetup:
    def __init__(self, game_config):
        self.game_config = game_config
        self.game_config.optimization_params = {
            "base": {  # Bet mode name
                "conditions": {...},
                "scaling": {...},
                "parameters": {...}
            }
        }

Conditions

Define target metrics for different game phases:

"conditions": {
    # Base game conditions
    "base_game": ConstructConditions(
        hr=3.5,      # Target hit rate (% of spins with wins)
        rtp=0.59     # Target RTP contribution from base game
    ).return_dict(),

    # Free game conditions
    "free_game": ConstructConditions(
        rtp=0.37,    # Target RTP contribution from free games
        hr=200,      # Target hit rate (per 10k spins)
        search_conditions={"symbol": "scatter"}  # Trigger condition
    ).return_dict(),
}

ConstructConditions Parameters

  • hr (float): Target hit rate
  • For base game: percentage (e.g., 3.5 = 3.5%)
  • For features: occurrences per 10k spins (e.g., 200 = 200 times)
  • rtp (float): Target RTP contribution (e.g., 0.59 = 59%)
  • average_win (float, optional): Target average win when feature hits
  • search_conditions (dict, optional): Conditions to identify the feature
  • {"symbol": "scatter"} - Look for scatter symbol
  • {"event_type": "triggerFreeSpins"} - Look for specific event

Scaling

Adjust specific win ranges:

"scaling": ConstructScaling([
    # Scale wins between 10x-20x by factor of 1.5
    {"min": 10, "max": 20, "scale": 1.5},

    # Scale wins above 100x by factor of 0.8
    {"min": 100, "max": float('inf'), "scale": 0.8},
]).return_dict()

This is useful for: - Increasing frequency of mid-range wins - Reducing extreme wins - Balancing volatility

Parameters

Optimization algorithm settings:

"parameters": ConstructParameters(
    num_show=5000,              # Number of top outcomes to display
    num_per_fence=10000,        # Population size per optimization group
    test_spins=[50, 100, 200],  # Test with these spin counts
    score_type="rtp"            # Optimization scoring method
).return_dict()

ConstructParameters Options

  • num_show: How many outcomes to show in results
  • num_per_fence: Population size (larger = more thorough but slower)
  • test_spins: Spin counts to test optimized distributions
  • score_type: What to optimize for
  • "rtp" - Target RTP accuracy
  • "hr" - Target hit rate accuracy
  • "balanced" - Balance both

Complete Example

# games/my_game/game_optimization.py

from optimization_program.optimization_config import (
    ConstructConditions,
    ConstructScaling,
    ConstructParameters,
)

class OptimizationSetup:
    def __init__(self, game_config):
        self.game_config = game_config
        self.game_config.optimization_params = {
        "base": {
            "conditions": {
                # Base game: 3.5% hit rate, contributes 59% RTP
                "base_game": ConstructConditions(
                    hr=3.5,
                    rtp=0.59
                ).return_dict(),

                # Free spins: triggers 200 times per 10k spins, contributes 37% RTP
                "free_game": ConstructConditions(
                    rtp=0.37,
                    hr=200,
                    search_conditions={"symbol": "scatter"}
                ).return_dict(),

                # Big wins: average 50x when hitting 100x+
                "bigwin": ConstructConditions(
                    average_win=50,
                    search_conditions={"min_win": 100}
                ).return_dict(),
            },

            "scaling": ConstructScaling([
                # Increase mid-range wins (10x-50x)
                {"min": 10, "max": 50, "scale": 1.3},

                # Reduce very high wins (500x+)
                {"min": 500, "max": float('inf'), "scale": 0.7},
            ]).return_dict(),

            "parameters": ConstructParameters(
                num_show=5000,
                num_per_fence=20000,  # Larger for better accuracy
                test_spins=[100, 500, 1000],
                score_type="balanced"
            ).return_dict()
        }
    }
)

Running Optimization

Method 1: Via run.py

Edit games/<game_name>/run.py:

from optimization_program.run_script import OptimizationExecution

# Load game and config
game_state = GameState()
from games.my_game.game_optimization import optimization_setup

# Run optimization
OptimizationExecution(
    game_id="my_game",
    game_state=game_state,
    bet_modes=["base"],           # Bet modes to optimize
    run_sims=False,              # Use existing books (set True to regenerate)
    run_optimization=True,       # Enable optimization
    optimization_setup=optimization_setup
)

Then run:

make run GAME=my_game

Method 2: Separate Optimization Run

  1. Generate books first:
    # run_config.toml - first pass
    [pipeline]
    run_sims = true
    run_optimization = false
    
make run GAME=my_game
  1. Then optimize:
    # run_config.toml - second pass
    [pipeline]
    run_sims = false
    run_optimization = true
    
make run GAME=my_game

Optimization Flow

  1. Load Books: Reads existing simulation results
  2. Parse Setup: Loads game_optimization.py config
  3. Write Setup File: Generates optimization_program/src/setup.txt
  4. Run Rust Optimizer: Executes genetic algorithm
  5. Generate Results:
  6. Optimized probability distributions
  7. Updated CSV files
  8. Statistics and analysis
  9. Validate: Tests optimized distributions against targets

Output Files

After optimization:

games/<game_name>/build/optimization_files/
  ├── optimized_base_probs.csv        # Optimized probabilities
  ├── optimization_report.json        # Detailed statistics
  └── optimization_results/           # Intermediate results
      ├── generation_*.csv
      └── best_candidates.json

Interpreting Results

Optimization Report

{
  "target_rtp": 0.97,
  "achieved_rtp": 0.9698,
  "target_base_hr": 3.5,
  "achieved_base_hr": 3.48,
  "target_free_game_hr": 200,
  "achieved_free_game_hr": 198,
  "generations": 150,
  "best_score": 0.0023
}

Score: Lower is better (deviation from targets) - < 0.01: Excellent match - 0.01-0.05: Good match - > 0.05: May need adjustment

Common Issues

RTP too high/low - Adjust scaling factors - Modify paytable multipliers - Check base game win frequencies

Hit rate off target - Adjust trigger symbol distribution - Modify win condition thresholds - Check scatter placement in reels

Optimization not converging - Increase num_per_fence for larger population - Adjust test_spins for more accurate testing - Simplify conditions (fewer constraints)

Top-end coverage

The optimizer can only weight books that exist. If no simulated round lands between a mode's ordinary wins and its win cap, the published game has a hole there that no weighting fixes.

How it happens. The wincap distribution forces cap books on a big-win free reel (force_wincap), but the free_game distribution builds every other free game on the plain free reel with only the minimum scatter counts. Those free games almost never reach the thousands, so the only win above ~1,000x is the cap. Safari Pop shipped books like this until 2026-10-18: in base, the biggest win below 10,000x was 504x, every base win of 500x or more was the same single book replayed, and Stake Challenge tiers below the cap collapsed into the max-win event.

The fix, in game_config.py:

  1. Mix the big-win free reel into every non-forced free-game distribution at a small weight, without forcing the cap. The optimizer still sets how often those rounds happen, so RTP and hit rates hold:
    Distribution(
        criteria="free_game",
        quota=0.1,
        conditions={
            "reel_weights": {
                self.base_game_type: {"base": 1},
                self.free_game_type: {"free": 9, "wincap": 1},  # not {"free": 1}; tune, below
            },
            "scatter_triggers": {4: 20, 5: 6, 6: 2, 7: 1},     # reach the longer free-spin awards
            "force_wincap": False,
            "force_free_game": True,
        },
    ),
    
  2. If the game has no big-win free reel (template_scatter, template_expanding_wilds), add one: the free reel with more wilds/multipliers, the same reel the wincap distribution should use.
  3. Tune the mix per game. How hot the big-win reel is decides the share: start at 10% ({"free": 9, "wincap": 1}) and adjust until the check below shows no WARNING. Too little and bands go empty; too much and the curve turns top-heavy. Measured on base RTP, 2026-10-18:
Game Big-win reel Mix RTP from 1,000x-9,999x RTP from 100x-999x
Farm Pop 189 wilds 25% 21.7% 3.4% (top-heavy)
Farm Pop 10% 12.7% 16.4%
Farm Pop 5% 6.8% 22.1% (bands too thin)
Safari Pop 85 wilds 25% 12.8% 9.5% (top-heavy)
Safari Pop ~14% 8.3% 16.7%
Safari Pop 10% 5.5% 20.7% (2,500x-5,000x borderline)
  1. Cap hits outside the win-cap distribution are re-simulated (GameState.reject_unplanned_wincap(), called at the end of every check_repeat, on by default; config.reject_unplanned_wincap = False opts out). Without it, free-game books that run to the cap on the big-win reel are weighted as ordinary free games: Farm Pop's hot wincap reel (189 wilds) at a 25% mix made its base max win 1 in 93,471 instead of 1 in 1,000,000, and its 300× buy 1 in 120. A game's own check_repeat override must call it.
  2. More books help only at the margin. Safari Pop at 100,000 books per mode had nothing above 504x in base; with the reel mix, 20,000 books had 270 above 625x.

template_cluster, template_lines and template_ways ship with step 1 since 2026-10-18.

The check. Format checks (run_format_checks = true) print a top-end coverage table per mode after the RGS verification: book counts in halvings below the cap ([cap/32, cap/16) ... [cap/2, cap)), and a WARNING for an empty band, a band with fewer than 10 weighted books, or one book holding more than half of the top-end wins, and a top-heavy WARNING when more RTP comes from [cap/10, cap) than from [cap/100, cap/10) (checked for modes costing at most cap/100). The top band [cap/2, cap) is reported but not required. Run it on any publish folder:

python -m utils.top_end_coverage games/<game>/build/publish_files

Do not publish with a WARNING unless the game's design caps it on purpose (say so in the game's README).

Advanced Techniques

Multi-Stage Optimization

Optimize different aspects separately:

# Stage 1: Base game RTP
"base_game": ConstructConditions(rtp=0.60).return_dict()

# Stage 2: Free game frequency
"free_game": ConstructConditions(hr=200).return_dict()

# Stage 3: Fine-tune both
"base_game": ConstructConditions(hr=3.5, rtp=0.59).return_dict()

Conditional Scaling

Different scaling for different game states:

# In base game: scale medium wins up
"base_scaling": ConstructScaling([
    {"min": 5, "max": 20, "scale": 1.5}
]).return_dict()

# In free spins: scale high wins down
"free_spin_scaling": ConstructScaling([
    {"min": 50, "max": float('inf'), "scale": 0.8}
]).return_dict()

Search Conditions

Find specific outcomes:

# Find free spin triggers
search_conditions={"symbol": "scatter", "min_count": 3}

# Find high wins
search_conditions={"min_win": 100}

# Find specific events
search_conditions={"event_type": "triggerFreeSpins"}

# Combine conditions
search_conditions={
    "symbol": "M",
    "min_win": 50,
    "event_type": "updateGlobalMult"
}

Performance Tips

Quick Iteration

num_per_fence=5000          # Smaller population
test_spins=[50]             # Fewer test spins

Production Run

num_per_fence=50000         # Large population
test_spins=[100, 500, 1000] # Thorough testing

Memory Usage

  • Large num_per_fence increases memory usage
  • Use smaller books for initial optimization
  • Generate full books after optimization converges

Troubleshooting

"Optimization failed to converge"

  • Conflicting conditions (impossible to satisfy all targets)
  • Try relaxing some constraints
  • Check if paytable supports target RTP

"Setup file not found"

  • Ensure game_optimization.py exists
  • Check file is imported correctly in run.py
  • Verify OptimizationSetup is constructed properly

"Rust build failed"

  • Install Rust: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Update Rust: rustup update
  • Check optimization_program/Cargo.toml is valid

See Also