Skip to content

Refactor pareto front and support constraints_func in plot_pareto_front - #3128

Merged
toshihikoyanase merged 13 commits into
optuna:masterfrom
semiexp:refactor_pareto_front
Dec 24, 2021
Merged

toshihikoyanase merged 13 commits into
optuna:masterfrom
semiexp:refactor_pareto_front

Conversation

@semiexp

@semiexp semiexp commented Nov 28, 2021 •

Copy link
Copy Markdown
Contributor

Motivation

Implementation in #3125 is repetitious (10 times of go.Scatter and go.Scatter3d in total), so it will be desirable to be refactored. This refactoring is strongly tied with the enhancement of plot_pareto_front, so I did both in this PR.

Description of the changes

  • Fused _get_pareto_front_2d and _get_pareto_front_3d.
  • Added helper function _make_scatter_object_base and used it in plot_pareto_front because each invocation of go.Scatter and go.Scatter3d shares several parameters in common.
  • I added constraints_func property to plot_parent_front so that infeasible trials can be explicitly marked in case of training with objective constraints. Infeasible trials are excluded from the computation of pareto front.
    • I added _get_pareto_front_trials_by_trials to _multi_objective.py so that we don't have to create another Study in order to compute the pareto front of a part of trials in a Study.

@github-actions github-actions Bot added the optuna.visualization Related to the `optuna.visualization` submodule. This is automatically labeled by github-actions. label Nov 28, 2021
@semiexp

semiexp commented Nov 28, 2021 •

Copy link
Copy Markdown
Contributor Author

This branch derives from #3125, so we have to merge it first and then rebase this branch onto master.
Instead of reviewing 2 PRs sequentially, we decided to review the entire change (adding constraints_func + refactoring) at the same time.

@semiexp
semiexp force-pushed the refactor_pareto_front branch from 8451b66 to 2f1f5f7 Compare November 28, 2021 08:13
@keisuke-umezawa keisuke-umezawa added the sprint-20211128 PR from the online sprint event Nov 28, 2021. label Nov 28, 2021
@toshihikoyanase toshihikoyanase self-assigned this Nov 28, 2021
@toshihikoyanase toshihikoyanase added the feature Change that does not break compatibility, but affects the public interfaces. label Nov 28, 2021
@codecov-commenter

codecov-commenter commented Nov 28, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #3128 (ade4967) into master (cf03ade) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3128      +/-   ##
==========================================
+ Coverage   91.22%   91.23%   +0.01%     
==========================================
  Files         146      146              
  Lines       11918    11929      +11     
==========================================
+ Hits        10872    10884      +12     
+ Misses       1046     1045       -1     
Impacted Files Coverage Δ
optuna/study/_multi_objective.py 92.30% <100.00%> (+0.24%) ⬆️
optuna/visualization/_pareto_front.py 100.00% <100.00%> (+1.13%) ⬆️
optuna/study/study.py 96.01% <0.00%> (-0.04%) ⬇️
optuna/integration/sklearn.py 97.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf03ade...ade4967. Read the comment docs.

@toshihikoyanase toshihikoyanase added the v3 Issue/PR for Optuna version 3. label Nov 29, 2021
@HideakiImamura HideakiImamura self-assigned this Nov 29, 2021
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Dec 13, 2021
@semiexp
semiexp force-pushed the refactor_pareto_front branch from 2f1f5f7 to d24d23c Compare December 19, 2021 02:26
@semiexp semiexp changed the title [WIP] Refactor pareto front Refactor pareto front and support constraints_func in plot_pareto_front Dec 19, 2021
@hvy hvy added the sprint-20211219 PR from the online sprint event Dec 19, 2021. label Dec 19, 2021

@toshihikoyanase toshihikoyanase 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 for your PR! It basically looks great, but let me check carefully since it introduced many conditional branches.

Here are some early comments.

Comment thread optuna/study/_multi_objective.py Resolved
Comment thread optuna/study/_multi_objective.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved

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

Let me add my second comment. I went through the changes in the optuna module. I'll check the test code, next.

Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved

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

The code is a bit complex to be honest but it mostly LGTM.
I left some nitpicks on details and internals.

Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/study/_multi_objective.py Resolved
Comment thread optuna/study/_multi_objective.py Resolved
Comment thread optuna/study/_multi_objective.py Resolved
feasible_trials = []
infeasible_trials = []
for trial in study.get_trials(states=(TrialState.COMPLETE,)):
if all(map(lambda x: x <= 0.0, constraints_func(trial))):

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.

Not necessary in this PR, but it might soon make sense to encapsulate this logic into some reusable function.

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.

Sounds reasonable. I think FrozenTrial.is_feasible(constraints_func) is a simple choice. It can be a follow-up task.

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'm a bit conservative to add a new method to FrozenTrial sinceTrial does not have the corresponding method. And also the type signature will have a nested structure i.e., the method of FrozenTrial instance takes a function of FrozenTrial instance.

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 guess it can be discussed in a different ticket, but just to elaborate my initial thought, was to create a private free function.

Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
Comment thread optuna/visualization/_pareto_front.py Resolved
@toshihikoyanase

Copy link
Copy Markdown
Member

[Opinion wanted] The current Pareto-front plot uses gradations to visualize the trial.number to draw markers corresponding to trials. On the other hand, the constrained plots do not employ the gradations, so users cannot see the trial number. I think this is acceptable since both trials in the early phase and infeasible trials are drawn as gray points.

Plot without constraints
image

Plot with constraints
image

@hvy

hvy commented Dec 19, 2021 •

Copy link
Copy Markdown
Member

I personally find gradients quite informative during analysis. If possible, I'd vote for rendering them in this case too.

@toshihikoyanase toshihikoyanase 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 added some comments about the test cases. If you have any questions or comments, please let me know.

@semiexp
semiexp force-pushed the refactor_pareto_front branch from 6130148 to ff33baa Compare December 19, 2021 07:01
@semiexp
semiexp force-pushed the refactor_pareto_front branch from ff33baa to a0db84e Compare December 19, 2021 07:02

@hvy hvy 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, thanks a lot for the long-running work. The code looks simpler now and I'm sure this'll be useful for multi-objective analysis.

@github-actions github-actions Bot removed the stale Exempt from stale bot labeling. label Dec 19, 2021
@HideakiImamura HideakiImamura self-assigned this Dec 20, 2021
@HideakiImamura

HideakiImamura commented Dec 20, 2021 •

Copy link
Copy Markdown
Member

Note that this is related to #3116 and #2973.

@HideakiImamura HideakiImamura 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 for the great improvement of the plot_pareto_front! The code looks good to me. I have several suggestions, but they can be done as follow-up tasks. So, let me approve this PR.

axis_order:
A list of indices indicating the axis order. If :obj:`None` is specified,
default order is used.
constraints_func:

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 think it is useful to add an example using this option. It can be a follow-up task.

Comment on lines +80 to +82
If given, trials are classified into three categories: feasible and best, feasible but
non-best, and infeasible. Categories are shown in different colors. Here, whether a
trial is best (on Pareto front) or not is determined ignoring all infeasible trials.

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.

It would be easier to understand if it is clearly stated that infeasible points are colored in gray. It can be a follow-up task.

feasible_trials = []
infeasible_trials = []
for trial in study.get_trials(states=(TrialState.COMPLETE,)):
if all(map(lambda x: x <= 0.0, constraints_func(trial))):

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.

Sounds reasonable. I think FrozenTrial.is_feasible(constraints_func) is a simple choice. It can be a follow-up task.

@HideakiImamura

Copy link
Copy Markdown
Member

The current code produces the following plots. I found that there is no explanation of infeasible points in the plot. It can be improved in the followup PR.

code:

import optuna


def objective(trial):
    x = trial.suggest_float("x", 0, 5)
    y = trial.suggest_float("y", 0, 3)

    v0 = 4 * x ** 2 + 4 * y ** 2
    v1 = (x - 5) ** 2 + (y - 5) ** 2
    
    c0 = v0 - 60
    trial.set_user_attr("constraint", (c0,))

    return v0, v1


def constraints(trial):
    return trial.user_attrs["constraint"]


sampler = optuna.samplers.NSGAIISampler(constraints_func=constraints)
study = optuna.create_study(directions=["minimize", "minimize"], sampler=sampler)
study.optimize(objective, n_trials=50)

fig = optuna.visualization.plot_pareto_front(study)
fig.write_image("without.png")

fig = optuna.visualization.plot_pareto_front(study, constraints_func=constraints)
fig.write_image("with.png")

Without constraints: without.png
without

With constraints: with.png
with

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

Sorry for the delayed response. LGTM!

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. sprint-20211128 PR from the online sprint event Nov 28, 2021. sprint-20211219 PR from the online sprint event Dec 19, 2021. v3 Issue/PR for Optuna version 3.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants