Skip to content

Filter feasible trials when selecting best_trial in get_all_study_summaries - #6879

Merged
c-bata merged 1 commit into
optuna:masterfrom
taran-dev4u:fix/study-summaries-respect-constraints
Oct 7, 2026
Merged

c-bata merged 1 commit into
optuna:masterfrom
taran-dev4u:fix/study-summaries-respect-constraints

Conversation

@taran-dev4u

Copy link
Copy Markdown
Contributor

Motivation

Closes #6825.

In constrained optimization studies, get_all_study_summaries selected best_trial by computing min/max over all TrialState.COMPLETE trials without filtering for feasibility (all(x <= 0.0 for x in trial.constraints.values())). This caused StudySummary.best_trial to pick an infeasible trial with a better objective value rather than matching Study.best_trial. When no feasible completed trials exist, StudySummary.best_trial should be None.

Description of the changes

  • Filter completed_trials with _get_feasible_trials in get_all_study_summaries before selecting best_trial for single-objective studies.
  • Set best_trial = None when no feasible completed trials exist.
  • Added tests in tests/study_tests/test_study.py covering feasible/infeasible trial selection for minimize/maximize directions and all-infeasible studies across storage modes.

Checklist

  • I used an LLM while working on this PR.

@not522

not522 commented Oct 1, 2026

Copy link
Copy Markdown
Member

@c-bata @yen-0 Could you review this PR?

@yen-0 yen-0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 yen-0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@taran-dev4u
taran-dev4u force-pushed the fix/study-summaries-respect-constraints branch from a178530 to d8425e0 Compare October 3, 2026 18:09
@taran-dev4u

Copy link
Copy Markdown
Contributor Author

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 optuna/study/study.py without the extra branching.

Here is the reproduction script and output verifying both the feasible selection and all-infeasible None behavior:

Reproduction script and output
import 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 None

Output:

Study best_trial:   number=1, value=5.0
Summary best_trial: number=1, value=5.0
Summary best_trial: None

…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]>
@taran-dev4u
taran-dev4u force-pushed the fix/study-summaries-respect-constraints branch from d8425e0 to aecf5ff Compare October 6, 2026 04:07

@yen-0 yen-0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@c-bata c-bata added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Oct 7, 2026
@c-bata c-bata added this to the v5.1.0 milestone Oct 7, 2026
@c-bata
c-bata merged commit 8763c55 into optuna:master Oct 7, 2026
13 checks passed
@taran-dev4u
taran-dev4u deleted the fix/study-summaries-respect-constraints branch October 7, 2026 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_all_study_summaries ignores constraints when selecting best_trial

4 participants