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

Guarantee weights_below to be finite in MOTPE #5435

Merged
merged 9 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions optuna/samplers/_tpe/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def _build_parzen_estimator(
weights_below = _calculate_weights_below_for_multi_objective(
study, trials, self._constraints_func
)[param_mask_below]
assert np.isfinite(weights_below).all()
mpe = self._parzen_estimator_cls(
observations, search_space, self._parzen_estimator_parameters, weights_below
)
Expand Down Expand Up @@ -814,8 +815,13 @@ def _calculate_weights_below_for_multi_objective(
contributions = np.asarray(
[hv - WFG().compute(lvals[indices_mat[i]], reference_point) for i in range(n_below)]
)
contributions += EPS
weights_below = np.clip(contributions / np.max(contributions), 0, 1)
contributions[np.isnan(contributions)] = np.inf
max_contribution = np.maximum(np.max(contributions), EPS)
eukaryo marked this conversation as resolved.
Show resolved Hide resolved
if not np.isfinite(max_contribution):
weights_below = np.ones_like(contributions, dtype=float)
weights_below[np.isfinite(contributions)] = EPS
eukaryo marked this conversation as resolved.
Show resolved Hide resolved
else:
weights_below = np.clip(contributions / max_contribution, EPS, 1)

# For now, EPS weight is assigned to infeasible trials.
weights_below_all = np.full(len(below_trials), EPS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ def test_calculate_weights_below_for_multi_objective() -> None:
None,
)
assert len(weights_below) == 3
assert all([np.isnan(w) for w in weights_below])
assert not any([np.isnan(w) for w in weights_below])
assert sum(weights_below) > 0

# Three samples with two infeasible trials.
study = optuna.create_study(directions=["minimize", "minimize"])
Expand Down