Merged
Conversation
Contributor
|
@sawa3030 Could you review this PR? |
nabenabe0928
reviewed
Aug 4, 2025
Contributor
There was a problem hiding this comment.
I confirmed that the PR works as intended with the following code:
Verification Code
from __future__ import annotations
from collections.abc import Sequence
from typing import NamedTuple
import numpy as np
from tqdm import tqdm
import optuna
from optuna._hypervolume import compute_hypervolume
from optuna.study import Study
from optuna.study._study_direction import StudyDirection
from optuna.trial import TrialState
from optuna.visualization._plotly_imports import _imports
def this_pr(
values_array: np.ndarray, ref_point: np.ndarray,
) -> np.ndarray:
n_trials = values_array.shape[0]
hypervolume_values = np.empty(n_trials, dtype=float)
best_trials_values_normalized: np.ndarray | None = None
hypervolume = 0.0
for i in tqdm(range(n_trials)):
values_normalized = values_array[i, np.newaxis, :]
if best_trials_values_normalized is not None:
if (best_trials_values_normalized <= values_normalized).all(axis=1).any(axis=0):
hypervolume_values[i] = hypervolume
continue
hypervolume += np.prod(ref_point - values_normalized)
if best_trials_values_normalized is None:
best_trials_values_normalized = values_normalized
else:
limited_sols = np.maximum(best_trials_values_normalized, values_normalized)
hypervolume -= compute_hypervolume(limited_sols, ref_point)
is_kept = (best_trials_values_normalized < values_normalized).any(axis=1)
best_trials_values_normalized = np.concatenate(
[best_trials_values_normalized[is_kept, :], values_normalized], axis=0
)
hypervolume_values[i] = hypervolume
return hypervolume_values
def master(
values_array: np.ndarray, ref_point: np.ndarray,
) -> np.ndarray:
n_trials = values_array.shape[0]
hypervolume_values = np.empty(n_trials, dtype=float)
best_trials_values_normalized: np.ndarray | None = None
for i in tqdm(range(n_trials)):
values_normalized = values_array[i, np.newaxis, :]
if best_trials_values_normalized is not None:
if (best_trials_values_normalized <= values_normalized).all(axis=1).any(axis=0):
hypervolume_values[i] = hypervolume_values[i - 1]
continue
if best_trials_values_normalized is None:
best_trials_values_normalized = values_normalized
else:
is_kept = (best_trials_values_normalized < values_normalized).any(axis=1)
best_trials_values_normalized = np.concatenate(
[best_trials_values_normalized[is_kept, :], values_normalized], axis=0
)
hypervolume_values[i] = compute_hypervolume(best_trials_values_normalized, ref_point)
return hypervolume_values
if __name__ == "__main__":
rng = np.random.RandomState(42)
values_array = rng.normal(size=(5000, 4))
ref_point = np.full(4, 10.0)
out = this_pr(values_array.copy(), ref_point)
ans = master(values_array.copy(), ref_point)
assert np.allclose(out, ans)
nabenabe0928
reviewed
Aug 4, 2025
nabenabe0928
reviewed
Aug 4, 2025
Co-authored-by: Shuhei Watanabe <47781922+nabenabe0928@users.noreply.github.com>
Contributor
y0z
approved these changes
Aug 5, 2025
Member
y0z
left a comment
There was a problem hiding this comment.
@not522
The change works as expected. LGTM.
NIT:
Since values appear to be unnormalized and not guaranteed to be in the [0, 1] range in the general case, I thought the names values_normalized and best_trials_values_normalized might be inappropriate. However, these names do not affect the actual logic, so a fix could be considered in a follow-up PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
plot_hypervolume_historycan be accelerated by using incremental updates.Description of the changes
Changed hypervolume calculation to use incremental updates (
S(A v B) = S(A) + S(B) - S(A ^ B)).Benchmark
master: 213.47 sec
PR: 1.52 sec