Repository navigation
Filter feasible trials when selecting best_trial in get_all_study_summaries - #6879
Conversation
yen-0
left a comment
There was a problem hiding this comment.
Thanks for working on this. The production change can be reduced to one line by filtering at the existing assignment:
completed_trials = _get_feasible_trials(
[t for t in all_trials if t.state == TrialState.COMPLETE]
)
Since completed_trials is only used below, the existing length check already handles the all-infeasible case and returns best_trial = None. The additional branching in get_all_study_summaries is therefore unnecessary.
I also don’t think new test files are needed for this small fix. A reproduction script and output should be sufficient.
yen-0
left a comment
There was a problem hiding this comment.
Thanks for working on this. The production change can be reduced to one line by filtering at the existing assignment:
completed_trials = _get_feasible_trials(
[t for t in all_trials if t.state == TrialState.COMPLETE]
)
Since completed_trials is only used below, the existing length check already handles the all-infeasible case and returns best_trial = None. The additional branching in get_all_study_summaries is therefore unnecessary.
I also don’t think new test files are needed for this small fix. A reproduction script and output should be sufficient.
a178530 to
d8425e0
Compare
|
Thanks for the review! Updated the assignment directly: completed_trials = _get_feasible_trials(
[t for t in all_trials if t.state == TrialState.COMPLETE]
)This simplifies the diff down to one line in Here is the reproduction script and output verifying both the feasible selection and all-infeasible Reproduction script and outputimport optuna
def objective(trial):
x = trial.suggest_float("x", -10, 10)
trial.set_constraint("c", 5 - x) # feasible iff x >= 5
return x
# Case 1: Feasible trial present
study = optuna.create_study(direction="minimize")
study.enqueue_trial({"x": -10.0}) # infeasible
study.enqueue_trial({"x": 5.0}) # feasible
study.optimize(objective, n_trials=2)
summary = optuna.get_all_study_summaries(study._storage, include_best_trial=True)[0]
print(f"Study best_trial: number={study.best_trial.number}, value={study.best_trial.value}")
print(f"Summary best_trial: number={summary.best_trial.number}, value={summary.best_trial.value}")
assert summary.best_trial.number == study.best_trial.number
# Case 2: All infeasible trials
def infeasible_obj(trial):
x = trial.suggest_float("x", -10, 10)
trial.set_constraint("c", 10.0) # always infeasible
return x
study2 = optuna.create_study(direction="minimize")
study2.optimize(infeasible_obj, n_trials=2)
summary2 = optuna.get_all_study_summaries(study2._storage, include_best_trial=True)[0]
print(f"Summary best_trial: {summary2.best_trial}")
assert summary2.best_trial is NoneOutput: |
…maries Filter completed trials with _get_feasible_trials before computing min/max best_trial in get_all_study_summaries, ensuring constrained studies match Study.best_trial and return None when all trials are infeasible. Signed-off-by: Taran <[email protected]>
d8425e0 to
aecf5ff
Compare
Motivation
Closes #6825.
In constrained optimization studies,
get_all_study_summariesselectedbest_trialby computingmin/maxover allTrialState.COMPLETEtrials without filtering for feasibility (all(x <= 0.0 for x in trial.constraints.values())). This causedStudySummary.best_trialto pick an infeasible trial with a better objective value rather than matchingStudy.best_trial. When no feasible completed trials exist,StudySummary.best_trialshould beNone.Description of the changes
completed_trialswith_get_feasible_trialsinget_all_study_summariesbefore selectingbest_trialfor single-objective studies.best_trial = Nonewhen no feasible completed trials exist.tests/study_tests/test_study.pycovering feasible/infeasible trial selection for minimize/maximize directions and all-infeasible studies across storage modes.Checklist