From 7fe814dacc0a220b5f4bdec4a38af0cc23d71a52 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Wed, 8 May 2024 15:14:06 +0900 Subject: [PATCH 1/9] Update sampler.py --- optuna/samplers/_tpe/sampler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 30d6d82e27..9599f8bc9c 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -516,6 +516,8 @@ def _build_parzen_estimator( weights_below = _calculate_weights_below_for_multi_objective( study, trials, self._constraints_func )[param_mask_below] + if not np.isfinite(weights_below.sum()): + weights_below = np.ones_like(weights_below) mpe = self._parzen_estimator_cls( observations, search_space, self._parzen_estimator_parameters, weights_below ) From 47afce797404d1ba5e279dc62bf30e1fde019484 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 02:58:38 +0900 Subject: [PATCH 2/9] Update sampler.py --- optuna/samplers/_tpe/sampler.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 9599f8bc9c..9d21fe52ef 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -516,8 +516,8 @@ def _build_parzen_estimator( weights_below = _calculate_weights_below_for_multi_objective( study, trials, self._constraints_func )[param_mask_below] - if not np.isfinite(weights_below.sum()): - weights_below = np.ones_like(weights_below) + if not np.isfinite(weights_below).all(): + weights_below = 1.0 - np.isfinite(weights_below) mpe = self._parzen_estimator_cls( observations, search_space, self._parzen_estimator_parameters, weights_below ) @@ -816,8 +816,7 @@ 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) + weights_below = np.clip(contributions / np.max(contributions), EPS, 1) # For now, EPS weight is assigned to infeasible trials. weights_below_all = np.full(len(below_trials), EPS) From 800f78f0b7059b1c632668f67fbb6fb398384bd5 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 03:21:43 +0900 Subject: [PATCH 3/9] Update sampler.py --- optuna/samplers/_tpe/sampler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 9d21fe52ef..50d5d19262 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -816,6 +816,7 @@ 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 = np.clip(contributions, EPS, None) weights_below = np.clip(contributions / np.max(contributions), EPS, 1) # For now, EPS weight is assigned to infeasible trials. From dbf2ec574e1e0164304767396fcb4e62e64517ea Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 03:49:31 +0900 Subject: [PATCH 4/9] Update sampler.py --- optuna/samplers/_tpe/sampler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 50d5d19262..0d01561a5a 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -517,7 +517,7 @@ def _build_parzen_estimator( study, trials, self._constraints_func )[param_mask_below] if not np.isfinite(weights_below).all(): - weights_below = 1.0 - np.isfinite(weights_below) + weights_below = np.clip(1.0 - np.isfinite(weights_below), EPS, 1.0) mpe = self._parzen_estimator_cls( observations, search_space, self._parzen_estimator_parameters, weights_below ) @@ -817,7 +817,7 @@ def _calculate_weights_below_for_multi_objective( [hv - WFG().compute(lvals[indices_mat[i]], reference_point) for i in range(n_below)] ) contributions = np.clip(contributions, EPS, None) - weights_below = np.clip(contributions / np.max(contributions), EPS, 1) + weights_below = np.clip(contributions / np.max(contributions), EPS, 1.0) # For now, EPS weight is assigned to infeasible trials. weights_below_all = np.full(len(below_trials), EPS) From fc4346995784dbb0b91b1f11b6e3ef2032801f69 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 12:38:51 +0900 Subject: [PATCH 5/9] Update optuna/samplers/_tpe/sampler.py Co-authored-by: Shuhei Watanabe <47781922+nabenabe0928@users.noreply.github.com> --- optuna/samplers/_tpe/sampler.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 0d01561a5a..9a00680766 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -816,8 +816,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 = np.clip(contributions, EPS, None) - weights_below = np.clip(contributions / np.max(contributions), EPS, 1.0) + contributions[np.isnan(contributions)] = np.inf + max_contribution = np.max(contributions) + if not np.isfinite(max_contribution): + weights_below = np.ones_like(contributions, dtype=float) + weights_below[np.isfinite(contributions)] = EPS + 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) From 94e63c3f46cb264fdb8b7e07fb70de21c7e200ab Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 12:41:00 +0900 Subject: [PATCH 6/9] Update sampler.py --- optuna/samplers/_tpe/sampler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 9a00680766..f6bc03d89b 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -516,8 +516,7 @@ def _build_parzen_estimator( weights_below = _calculate_weights_below_for_multi_objective( study, trials, self._constraints_func )[param_mask_below] - if not np.isfinite(weights_below).all(): - weights_below = np.clip(1.0 - np.isfinite(weights_below), EPS, 1.0) + assert np.isfinite(weights_below).all() mpe = self._parzen_estimator_cls( observations, search_space, self._parzen_estimator_parameters, weights_below ) From d023f30f9635668840a53e2e01a52dc9863a7aa9 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 12:57:36 +0900 Subject: [PATCH 7/9] Update sampler.py --- optuna/samplers/_tpe/sampler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index f6bc03d89b..e706e8b3e0 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -816,7 +816,7 @@ def _calculate_weights_below_for_multi_objective( [hv - WFG().compute(lvals[indices_mat[i]], reference_point) for i in range(n_below)] ) contributions[np.isnan(contributions)] = np.inf - max_contribution = np.max(contributions) + max_contribution = np.maximum(np.max(contributions), EPS) if not np.isfinite(max_contribution): weights_below = np.ones_like(contributions, dtype=float) weights_below[np.isfinite(contributions)] = EPS From 6297c9b3cda4186aa797f6bf9eb4832016d978ca Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 9 May 2024 15:33:54 +0900 Subject: [PATCH 8/9] Update test_multi_objective_sampler.py --- tests/samplers_tests/tpe_tests/test_multi_objective_sampler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/samplers_tests/tpe_tests/test_multi_objective_sampler.py b/tests/samplers_tests/tpe_tests/test_multi_objective_sampler.py index 4905d1d196..7244110983 100644 --- a/tests/samplers_tests/tpe_tests/test_multi_objective_sampler.py +++ b/tests/samplers_tests/tpe_tests/test_multi_objective_sampler.py @@ -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"]) From 5b412620f71247c6a261b3c204a06dc368c25bd1 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Fri, 10 May 2024 18:25:30 +0900 Subject: [PATCH 9/9] Update optuna/samplers/_tpe/sampler.py Co-authored-by: Shuhei Watanabe <47781922+nabenabe0928@users.noreply.github.com> --- optuna/samplers/_tpe/sampler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index e706e8b3e0..8e9244bbe0 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -819,6 +819,7 @@ def _calculate_weights_below_for_multi_objective( max_contribution = np.maximum(np.max(contributions), EPS) if not np.isfinite(max_contribution): weights_below = np.ones_like(contributions, dtype=float) + # TODO(nabenabe0928): Make the weights for non Pareto solutions to zero. weights_below[np.isfinite(contributions)] = EPS else: weights_below = np.clip(contributions / max_contribution, EPS, 1)