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

Support constraints for intermediate values plot #4851

Merged
merged 5 commits into from
Aug 4, 2023

Conversation

adjeiv
Copy link
Contributor

@adjeiv adjeiv commented Aug 1, 2023

Motivation

References #4830

Description of the changes

Support constraints with the intermediate values plots, displaying infeasible trials in #CCCCCC colour.
Tests added accordingly.

import optuna
from optuna.Trial import Trial, FrozenTrial
from optuna.Study import create_study
from optuna.visualization import plot_intermediate_values
from typing import Sequence

def objective_with_constraints(trial: Trial) -> float:
        trial.set_user_attr("constraint", [trial.number % 2])

        trial.report(trial.number, step=0)
        trial.report(trial.number + 1, step=1)
        return 0.0

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

study = create_study(sampler=optuna.samplers.NSGAIIISampler(constraints_func=constraints))
study.optimize(objective_with_constraints, n_trials=10)

plot_intermediate_values(study)

Plot is below:
image

@github-actions github-actions bot added the optuna.visualization Related to the `optuna.visualization` submodule. This is automatically labeled by github-actions. label Aug 1, 2023
@adjeiv
Copy link
Contributor Author

adjeiv commented Aug 1, 2023

Re. failing docs build, readthedocs/readthedocs.org#10279 seems relevant... Pinning the sphinx version to be less than 7 should fix this

@not522
Copy link
Member

not522 commented Aug 2, 2023

Thank you for your PR and the investigation for CI fail. The Optuna maintainers are working to fix the CI. The fix is not necessary to merge this PR, so don't worry about it.

@not522
Copy link
Member

not522 commented Aug 2, 2023

@Alnusjaponica Could you review this PR?

@not522 not522 added the feature Change that does not break compatibility, but affects the public interfaces. label Aug 2, 2023
Copy link
Collaborator

@Alnusjaponica Alnusjaponica left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.
Could you make a similar change in optuna/visualization/matplotlib/_intermediate_values.py?

marker={"maxdisplayed": 10},
marker=default_marker
if tinfo.feasible
else default_marker | {"color": "#CCCCCC"}, # type: ignore[operator]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using | as a union operator for dict is only available since Python 3.9. Several ways to do it before Python 3.9 is mentioned in PEP584, so please use one of them.

@@ -69,7 +103,9 @@ def objective(trial: Trial, report_intermediate_values: bool) -> float:
_IntermediatePlotInfo(trial_infos=[]),
Copy link
Collaborator

@Alnusjaponica Alnusjaponica Aug 3, 2023

Choose a reason for hiding this comment

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

By adding the following example, you'll see the test fails when python<=3.8
ref. #4851 (comment)

        _IntermediatePlotInfo(
            trial_infos=[
                _TrialInfo(
                    trial_number=0, sorted_intermediate_values=[(0, 1.0), (1, 2.0)], feasible=True
                ),
                _TrialInfo(
                    trial_number=1, sorted_intermediate_values=[(1, 2.0), (0, 1.0)], feasible=False
                ) 
            ]
        )

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add this test case to confirm if test_plot_intermediate_values works when study has infeasible results?

Copy link
Collaborator

@Alnusjaponica Alnusjaponica left a comment

Choose a reason for hiding this comment

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

Thanks for your prompt response!
I have added some comments and would appreciate it if you could review them.

marker={"maxdisplayed": 10},
marker=default_marker
if tinfo.feasible
else default_marker.update({"color": "#CCCCCC"}), # type: ignore[dict-item]
Copy link
Collaborator

@Alnusjaponica Alnusjaponica Aug 3, 2023

Choose a reason for hiding this comment

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

Sorry for the confusion but dict.update() returns None like list.sort(). Please change this part like this.

Suggested change
else default_marker.update({"color": "#CCCCCC"}), # type: ignore[dict-item]
else {**default_marker, "color": "#CCCCCC"},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course - silly mistake. Thank you :)

@@ -69,7 +103,9 @@ def objective(trial: Trial, report_intermediate_values: bool) -> float:
_IntermediatePlotInfo(trial_infos=[]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add this test case to confirm if test_plot_intermediate_values works when study has infeasible results?

@Alnusjaponica
Copy link
Collaborator

Thanks! LGTM.
Let me approve this PR after we confirm CI success.

Copy link
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

Thank you! LGTM except the format. Please check the Checks CI.

@codecov-commenter
Copy link

codecov-commenter commented Aug 3, 2023

Codecov Report

Merging #4851 (0da819e) into master (55ed020) will decrease coverage by 0.05%.
Report is 84 commits behind head on master.
The diff coverage is 100.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master    #4851      +/-   ##
==========================================
- Coverage   89.52%   89.48%   -0.05%     
==========================================
  Files         197      200       +3     
  Lines       14676    14785     +109     
==========================================
+ Hits        13139    13230      +91     
- Misses       1537     1555      +18     
Files Changed Coverage Δ
...a/visualization/matplotlib/_intermediate_values.py 92.59% <ø> (+3.70%) ⬆️
optuna/visualization/_intermediate_values.py 94.87% <100.00%> (+1.12%) ⬆️

... and 18 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Collaborator

@Alnusjaponica Alnusjaponica left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM.

@Alnusjaponica Alnusjaponica removed their assignment Aug 4, 2023
@not522 not522 added this to the v3.4.0 milestone Aug 4, 2023
@not522 not522 merged commit 056dd2f into optuna:master Aug 4, 2023
30 checks passed
@adjeiv adjeiv deleted the intermediate_values_plot_constraints branch August 4, 2023 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Change that does not break compatibility, but affects the public interfaces. optuna.visualization Related to the `optuna.visualization` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants