Skip to content

Fix NaN comparison when skipping enqueued trials - #6803

Merged
c-bata merged 1 commit into
optuna:masterfrom
ishitta-iyer:fix/enqueue-trial-nan-duplicate
Aug 21, 2026
Merged

c-bata merged 1 commit into
optuna:masterfrom
ishitta-iyer:fix/enqueue-trial-nan-duplicate

Conversation

@ishitta-iyer

Copy link
Copy Markdown
Contributor

Motivation

Fixes #6798.

When skip_if_exists=True, Study.enqueue_trial incorrectly treats a newly enqueued NaN as a duplicate of any existing numeric value. This makes duplicate detection asymmetric: enqueueing 0.0 followed by NaN behaves differently from enqueueing the same values in reverse order.

Description of the changes

Use np.isclose(..., equal_nan=True) when comparing numeric parameter values.

This ensures that:

  • NaN and ordinary numeric values are distinct.
  • Two NaN values are treated as duplicates.
  • Duplicate detection is independent of enqueue order.
  • Existing approximate numeric comparison behavior is preserved.

A regression test covers both 0.0 followed by NaN and NaN followed by 0.0 across all configured storage modes.

Validation

  • 77 focused tests passed across all configured storage modes.
  • Ruff lint passed.
  • Ruff formatting check passed.
  • git diff --check passed.

@ishitta-iyer
ishitta-iyer force-pushed the fix/enqueue-trial-nan-duplicate branch from e0aae41 to ba3fcfb Compare August 13, 2026 19:44
@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 Aug 17, 2026

@gen740 gen740 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.

Good change.

However, I realized that the tests for enqueue_trial are a bit complicated and spread across the code base.

I think this is a good opportunity to introduce a simpler and more comprehensive test like:

@pytest.mark.parametrize("storage_mode", STORAGE_MODES)
@pytest.mark.parametrize(
    "params, expected_n_trials",
    [
        (["foo", "foo"], 1),
        ([1, 1], 1),
        ([1.1, 1.1], 1),
        ([1e17, 1e17], 1),
        ([1e-17, 1e-17], 1),
        ([float("inf"), float("inf")], 1),
        ([float("-inf"), float("-inf")], 1),
        ([float("nan"), float("nan")], 1),
        ([None, None], 1),
        ([0.0, float("nan")], 2),
        ([float("nan"), 0.0], 2),
    ],
)
def test_enqueue_trial_skip_existing_handles_values(
    storage_mode: str,
    params: list[Any],
    expected_n_trials: int,
) -> None:
    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage)

        for param in params:
            study.enqueue_trial({"x": param}, skip_if_exists=True)

        assert len(study.trials) == expected_n_trials

This test can cover many cases, and we can consolidate the following tests into it:

  • test_enqueue_trial_skips_existing_waiting
  • test_enqueue_trial_skip_existing_handles_common_types
  • test_enqueue_trial_skip_existing_distinguishes_nan_from_numeric_values

@gen740 gen740 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.

I reconsidered my suggestion above. Since this is more of a test refactoring and not directly related to this fix, it would be better handled separately.

The current change looks good. Approving this PR.

@c-bata c-bata 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.

LGTM.

@c-bata c-bata added this to the v5.0.0 milestone Aug 21, 2026
@c-bata
c-bata merged commit 1d8ce7b into optuna:master Aug 21, 2026
13 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.

Study.enqueue_trial treats NaN as a duplicate of any existing numeric value

3 participants