Skip to content

Commit e747542

Browse files
committed
Hoist ignore of invalid parameters
The ignore setting was previously only applying to the locally generated trial.
1 parent fbeaec3 commit e747542

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

optimas/generators/ax/service/base.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ def suggest(self, num_points: Optional[int]) -> List[dict]:
157157
points.append(point)
158158
return points
159159

160+
def _ignore_out_of_bounds(self, trial: Trial) -> None:
161+
"""Check if trial parameters are within their bounds."""
162+
for var, value in zip(
163+
trial.varying_parameters, trial.parameter_values
164+
):
165+
if value < var.lower_bound or value > var.upper_bound:
166+
if not self._fit_out_of_design:
167+
ignore_reason = (
168+
f"Parameter {var.name} value {value} is outside "
169+
f"allowed range [{var.lower_bound}, {var.upper_bound}]. "
170+
"Set `fit_out_of_design=True` if you want "
171+
"the model to use these data."
172+
)
173+
trial.ignore(reason=ignore_reason)
174+
175+
def ignore_trials(self, trials: List[Trial]) -> None:
176+
"""Ignore trials as determined by the generator."""
177+
for trial in trials:
178+
if not hasattr(trial, "ax_trial_id"):
179+
# Handle unknown trial
180+
self._ignore_out_of_bounds(trial)
181+
160182
def ingest(self, results: List[dict]) -> None:
161183
"""Send the results of evaluations to the generator."""
162184
for result in results:
@@ -168,7 +190,7 @@ def ingest(self, results: List[dict]) -> None:
168190
custom_parameters=self._custom_trial_parameters,
169191
)
170192
try:
171-
trial_id = trial._ax_trial_id
193+
trial_id = trial.ax_trial_id
172194
ax_trial = self._ax_client.get_trial(trial_id)
173195
except AttributeError:
174196
params = {}
@@ -188,15 +210,6 @@ def ingest(self, results: List[dict]) -> None:
188210
ax_trial.add_arm(Arm(parameters=params))
189211
ax_trial.mark_running(no_runner_required=True)
190212
trial_id = ax_trial.index
191-
else:
192-
ignore_reason = (
193-
f"The parameters {params} are outside of the "
194-
"range of the varying parameters. "
195-
"Set `fit_out_of_design=True` if you want "
196-
"the model to use these data."
197-
)
198-
trial.ignore(reason=ignore_reason)
199-
continue
200213
else:
201214
raise error
202215
ax_trial = self._ax_client.get_trial(trial_id)

optimas/generators/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ def ask_trials(self, n_trials: int) -> List[Trial]:
239239
trials.append(trial)
240240
return trials
241241

242+
def ignore_trials(self, trials: List[Trial]) -> None:
243+
"""Ignore trials as determined by the generator."""
244+
pass
245+
242246
def tell_trials(
243247
self, trials: List[Trial], allow_saving_model: Optional[bool] = True
244248
) -> None:
@@ -254,6 +258,7 @@ def tell_trials(
254258
255259
"""
256260
# Feed data to the generator, using the standardized API
261+
self.ignore_trials(trials)
257262
self.ingest([trial.to_dict() for trial in trials])
258263

259264
# Perform additional checks that rely on the trial format

0 commit comments

Comments
 (0)