-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Bug
After commit 0c6f17c (refactor(photonics): make num_optimization_steps an attribute of Config), calling optimize() on the photonics2d problem raises a KeyError when num_optimization_steps is not explicitly included in the config dict.
KeyError: 'num_optimization_steps'
Cause
The commit made two changes:
- Moved
num_optimization_stepsfrom a class-level default (_num_optimization_steps_default = 200) into theConfigdataclass (which extendsConditions) - Changed the access pattern from a safe
.get()with fallback to a hard key lookup:
- num_optimization_steps = conditions.get("num_optimization_steps", self._num_optimization_steps_default)
+ num_optimization_steps = conditions["num_optimization_steps"]However, _setup_simulation merges the passed config with self.conditions, which is the upcast Conditions instance (via upcast(self.config)). Since num_optimization_steps is a Config field but not a Conditions field, it gets stripped during the upcast and is never present in the merged dict — unless the caller explicitly passes it.
# v0.py:176 — self.conditions has no num_optimization_steps
current_conditions = {**dataclasses.asdict(self.conditions), **(config or {})}Reproduction
Any call to problem.optimize() where the config dict doesn't explicitly include num_optimization_steps will fail:
from engibench.utils.all_problems import BUILTIN_PROBLEMS
problem = BUILTIN_PROBLEMS["photonics2d"]()
problem.reset(seed=1)
design = problem.dataset["test"]["optimal_design"][0]
problem.optimize(design, config={"wavelength1": 1.0, "wavelength2": 1.3, "blur_radius": 2})
# KeyError: 'num_optimization_steps'Suggested Fix
Change _setup_simulation (line 176) to merge against self.config instead of self.conditions, so that Config-level defaults (including num_optimization_steps) are included:
- current_conditions = {**dataclasses.asdict(self.conditions), **(config or {})}
+ current_conditions = {**dataclasses.asdict(self.config), **(config or {})}