Skip to content

Align constraint observations by name in GPSampler - #6887

Merged
kAIto47802 merged 6 commits into
optuna:masterfrom
aswanth-07:fix/gp-constraint-name-alignment
Oct 14, 2026
Merged

kAIto47802 merged 6 commits into
optuna:masterfrom
aswanth-07:fix/gp-constraint-name-alignment

Conversation

@aswanth-07

@aswanth-07 aswanth-07 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

GPSampler fits one Gaussian process per constraint, but collecting each trial's constraints.values() mixes different functions when users report the same names in different orders. The feasibility mask remains unchanged and conceals the mismatched model inputs.

Description of the changes

I sort constraint names alphabetically and gather every trial's values by those names. I retain key-set validation so different names raise ValueError, including when their counts match. The canonical order also removes dependence on the first trial's reporting order, as requested in review.

Regression coverage checks named and numeric-index keys, inconsistent name sets, reordered first and subsequent trials, and seeded suggestions for single- and multi-objective studies. Storage formats, feasibility thresholds, GP fitting and acquisition algorithms are unchanged. Legacy sequence constraints use their existing string-index names in canonical string order.

The separate EDF figure cleanup is already upstream. This PR contains only the GP sampler and its tests.

Validation of e01f706 on Windows:

  • pytest tests/samplers_tests/test_gp.py tests/visualization_tests/test_edf.py -k 'not cuda_default_device' -q -W error::RuntimeWarning: 80 passed, one CUDA-default-device case deselected, four existing UserWarnings.
  • All 36 pairs of three-constraint insertion orders produce the same alphabetically ordered matrix, preserve feasibility and leave trial attributes unchanged. Twelve legacy-index names follow canonical string order.
  • Changed-file Ruff lint and formatting, full-repository mypy (324 files), and whitespace checks pass.

The two new canonical-order matrix cases fail on the preceding head and pass after this update. Current-head CI has 12 passing check contexts. The Windows job reports 5,136 passed, 69 skipped and one failure: the unchanged test_optimize_parallel_timeout[grpc_rdb--1-None] test hit sqlite3.OperationalError: database is locked while writing a trial parameter. That test uses the default TPE sampler and does not call the changed GP constraint helper. A maintainer must rerun the failed job. The full test suite, cross-process reproducibility, Linux/macOS and optimization-quality benchmarks were not run locally.

Checklist

  • I used an LLM while working on this PR.

@aswanth-07
aswanth-07 marked this pull request as ready for review October 7, 2026 10:36
Close pyplot figures retained by earlier tests before rendering the
empty-study regression, and close the returned figure afterward.
Keep RuntimeWarnings as errors so invalid numerical operations
still fail.
Comment thread tests/visualization_tests/test_edf.py Resolved
@c-bata

c-bata commented Oct 9, 2026

Copy link
Copy Markdown
Member

@not522 @kAIto47802 Could you review this PR?

@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 9, 2026
Comment thread optuna/samplers/_gp/sampler.py Outdated
if any(len(_constraint_vals[0]) != len(c) for c in _constraint_vals):
raise ValueError("The number of constraints must be the same for all trials.")
constraints = [trial.constraints for trial in trials]
constraint_keys = constraints[0].keys()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for your PR!

I think it would be better to arrange the keys in alphabetical order, as this makes it easier to ensure the reproducibility of optimization.

Suggested change
constraint_keys = constraints[0].keys()
constraint_keys = sorted(constraints[0].keys())

@aswanth-07 aswanth-07 Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in e01f706. I sorted the constraint names alphabetically while keeping key-set validation separate. The tests now check a reversed first trial as well as later reporting order, and all 36 three-constraint order pairs produce the same canonical matrix. The focused GP/EDF tests pass (80 passed, one CUDA case deselected), along with Ruff and full-repository mypy. Current-head CI has 12 passing contexts. The Windows job failed in the unchanged default-TPE parallel test with sqlite3.OperationalError: database is locked. Could you rerun that job?

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! LGTM!

@not522 not522 added this to the v5.1.0 milestone Oct 13, 2026
@not522 not522 removed their assignment Oct 14, 2026

@kAIto47802 kAIto47802 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

@kAIto47802
kAIto47802 merged commit 7e2f1d5 into optuna:master Oct 14, 2026
11 checks passed
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.

4 participants