Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constraints handling for contour plot #677

Merged

Conversation

toshihikoyanase
Copy link
Member

Contributor License Agreement

This repository (optuna-dashboard) and Goptuna share common code.
This pull request may therefore be ported to Goptuna.
Make sure that you understand the consequences concerning licenses and check the box below if you accept the term before creating this pull request.

  • I agree this patch may be ported to Goptuna by other Goptuna contributors.

Reference Issues/PRs

Distinguish between feasible and infeasible trials in contour plots, similar to the history plot updated in #529.
The visualization function in Optuna has already supported this feature since optuna/optuna#4975 was merged.

What does this implement/fix? Explain your changes.

  • Infeasible trials are plotted with #cccccc dots.
  • Feasible trials are plotted with #000000 dots, consistent with previous versions.
  • Each dot has a Gray edge, independent of the dot type.

image

@toshihikoyanase
Copy link
Member Author

Let me share the code example to create the plot above:

from __future__ import annotations

import optuna
from optuna import Trial
from optuna.samplers import TPESampler
from optuna.trial import FrozenTrial


def objective(trial: Trial) -> float:
    x = trial.suggest_float("x", -1, 1)
    y = trial.suggest_float("y", -1, 1)

    cx = x - 0.5  # x should be less than 0.5.
    cy = y - 0.5  # y should be less than 0.5.

    trial.set_user_attr("constraints", [cx, cy])
    # x = 0.5, y = -0.5
    return (x - 0.5) ** 2 + (y + 0.5) ** 2


def constraints_func(trial: FrozenTrial) -> list[float]:
    return trial.user_attrs["constraints"]


sampler = TPESampler(constraints_func=constraints_func)
study = optuna.create_study(sampler=sampler, storage="sqlite:///db-contour.sqlite3")
study.optimize(objective, n_trials=20)

optuna.visualization.plot_contour(study).show()

@keisuke-umezawa keisuke-umezawa self-assigned this Nov 1, 2023
Copy link
Member

@keisuke-umezawa keisuke-umezawa left a comment

Choose a reason for hiding this comment

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

LGTM!

@keisuke-umezawa keisuke-umezawa merged commit e0c89e2 into optuna:main Nov 6, 2023
@toshihikoyanase toshihikoyanase deleted the add-constrained-contour branch November 6, 2023 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants