feat(genetic): make the fitness cache size configurable (#1376)

The per-run fitness cache of the GENETIC optimizer is unbounded. It
grows with individuals x generations (~110k keys at 300 x 400, about
1.1 kB each, 1.6 kB with an EV) and adds 120-170 MB to the peak memory
of a run, while only 6-15 % of the lookups hit. On 512 MB boards this
is the largest part of the optimizer's memory.

Add optimization.genetic.fitness_cache_max_entries:

- None (default): unbounded, the previous behaviour.
- 0: no cache; every genome is evaluated.
- N > 0: at most N keys. The cache is then an OrderedDict and evicts
  the oldest key first (FIFO, O(1)); LRU would cost bookkeeping on
  every hit for a cache that rarely hits.

A cache hit returns exactly what the deterministic evaluation would
return, so the plan for a fixed seed is identical with any setting.

Co-authored-by: Christin <christin@BigBrother.bikini.bottom.zone>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Andreas <drbacke@gmx.de>
This commit is contained in:
Christin
2026-10-11 09:19:33 +02:00
committed by GitHub
co-authored by Christin Claude Opus 5.5 Andreas
parent ecec39fcd8
commit 14d8587c9c
8 changed files with 294 additions and 9 deletions
@@ -8,7 +8,7 @@ import random
import sys
import time
from array import array
from collections import defaultdict
from collections import OrderedDict, defaultdict
from dataclasses import dataclass, field
from functools import lru_cache
from typing import Any, Optional
@@ -816,6 +816,9 @@ class GeneticOptimization(OptimizationBase):
# have changed even when the genome is identical.
self._fitness_cache_enabled = False
self._fitness_cache: dict[PackedGenes, FitnessCacheEntry] = {}
# Maximum number of cache keys. None keeps the cache unbounded; a bounded
# cache is an OrderedDict and evicts the oldest key first (FIFO).
self._fitness_cache_max_entries: Optional[int] = None
self._fitness_cache_hits = 0
self._fitness_cache_misses = 0
@@ -2922,8 +2925,37 @@ class GeneticOptimization(OptimizationBase):
)
self._fitness_cache[original_key] = entry
self._fitness_cache[canonical_key] = entry
max_entries = getattr(self, "_fitness_cache_max_entries", None)
if max_entries is not None:
self._evict_fitness_cache(max_entries)
return fitness
def _evict_fitness_cache(self, max_entries: int) -> None:
"""Drop the oldest cache keys until at most ``max_entries`` remain.
FIFO instead of LRU: the hit rate of the cache is low (typically below
15 %), so tracking recency would cost more than it saves. Evicting a key
only means that genome is evaluated again; the evaluation is
deterministic, so the optimization result does not change.
"""
cache = self._fitness_cache
if isinstance(cache, OrderedDict):
while len(cache) > max_entries:
cache.popitem(last=False)
else:
while len(cache) > max_entries:
del cache[next(iter(cache))]
def _configured_fitness_cache_max_entries(self) -> Optional[int]:
"""Return the configured fitness cache limit (None = unbounded)."""
try:
max_entries = self.config.optimization.genetic.fitness_cache_max_entries
except Exception:
return None
if max_entries is None:
return None
return max(int(max_entries), 0)
def _fitness_key(self, individual: list[int]) -> PackedGenes:
"""Return the fitness-relevant genome, excluding elapsed control slots."""
start_slot = self._control_start_slot()
@@ -3307,10 +3339,20 @@ class GeneticOptimization(OptimizationBase):
# The memoization scope is exactly one optimizer invocation. Always turn
# it off again, including when DEAP raises, so no later caller can reuse
# results under changed forecasts or device state.
# fitness_cache_max_entries: None = unbounded, 0 = no cache, N = at most
# N keys (oldest evicted first). Disabling or bounding the cache only
# re-evaluates genomes; it does not change the result.
max_entries = self._configured_fitness_cache_max_entries()
self._fitness_cache_max_entries = max_entries
self._fitness_cache.clear()
if max_entries is not None:
# Only a bounded cache pays for the ordered container (O(1) eviction).
self._fitness_cache = OrderedDict()
elif isinstance(self._fitness_cache, OrderedDict):
self._fitness_cache = {}
self._fitness_cache_hits = 0
self._fitness_cache_misses = 0
self._fitness_cache_enabled = True
self._fitness_cache_enabled = max_entries != 0
local_evaluations = 0
local_improvements = 0
local_initial_fitness = float("nan")
@@ -3359,13 +3401,25 @@ class GeneticOptimization(OptimizationBase):
cache_lookups = self._fitness_cache_hits + self._fitness_cache_misses
cache_hit_rate = self._fitness_cache_hits / cache_lookups if cache_lookups > 0 else 0.0
cache_keys = len(self._fitness_cache)
logger.info(
"Fitness cache: {} hits, {} misses, {:.1%} hit rate, {} keys.",
self._fitness_cache_hits,
self._fitness_cache_misses,
cache_hit_rate,
cache_keys,
)
if max_entries == 0:
logger.info("Fitness cache: disabled (fitness_cache_max_entries = 0).")
elif max_entries is None:
logger.info(
"Fitness cache: {} hits, {} misses, {:.1%} hit rate, {} keys.",
self._fitness_cache_hits,
self._fitness_cache_misses,
cache_hit_rate,
cache_keys,
)
else:
logger.info(
"Fitness cache: {} hits, {} misses, {:.1%} hit rate, {} keys (limit {}).",
self._fitness_cache_hits,
self._fitness_cache_misses,
cache_hit_rate,
cache_keys,
max_entries,
)
# Store fitness history
self.fitness_history = {
@@ -108,6 +108,21 @@ class GeneticCommonSettings(SettingsBaseModel):
},
)
fitness_cache_max_entries: Optional[int] = Field(
default=None,
ge=0,
json_schema_extra={
"description": (
"Maximum number of keys in the per-run fitness cache. None = unbounded "
"(default), 0 = cache disabled, N > 0 = keep at most N keys and evict the "
"oldest first. Every cached evaluation takes one or two keys. The cache "
"only saves repeated evaluations; the result for a fixed seed is the same "
"with any value. Limit it to cap memory on small devices."
),
"examples": [None, 0, 5000],
},
)
measurement_max_age_seconds: int = Field(
default=300,
gt=0,