From ac6900f3f4280d43b92b1e6a8db88893caab9c15 Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 30 Sep 2025 12:35:58 +0200 Subject: [PATCH 1/2] Fix bug in simulated annealing when dealing with negative objectives --- kernel_tuner/strategies/simulated_annealing.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kernel_tuner/strategies/simulated_annealing.py b/kernel_tuner/strategies/simulated_annealing.py index 5055f302..6dcc2167 100644 --- a/kernel_tuner/strategies/simulated_annealing.py +++ b/kernel_tuner/strategies/simulated_annealing.py @@ -104,10 +104,14 @@ def acceptance_prob(old_cost, new_cost, T, tuning_options): res = 1.0 # maybe move if old cost is better than new cost depending on T and random value else: - if tuning_options.objective_higher_is_better: - res = np.exp(((new_cost-old_cost)/new_cost)/T) - else: - res = np.exp(((old_cost-new_cost)/old_cost)/T) + # we must have abs_diff <= 0, as new_cost >= old_cost + abs_diff = old_cost - new_cost + + # relative to abs(old_cost), as the cost might be negative + rel_diff = abs_diff / np.abs(old_cost) + + # exponential decay + res = np.exp(rel_diff / T) return res From 810ffe3157b0e1cd824c48ee129c42b61b83edb1 Mon Sep 17 00:00:00 2001 From: stijn Date: Tue, 30 Sep 2025 12:40:22 +0200 Subject: [PATCH 2/2] Remove unused `tuning_options` parameter to keep SonarQube happy --- kernel_tuner/strategies/simulated_annealing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel_tuner/strategies/simulated_annealing.py b/kernel_tuner/strategies/simulated_annealing.py index 6dcc2167..962a1e34 100644 --- a/kernel_tuner/strategies/simulated_annealing.py +++ b/kernel_tuner/strategies/simulated_annealing.py @@ -59,7 +59,7 @@ def tune(searchspace: Searchspace, runner, tuning_options): print(e) return cost_func.results - ap = acceptance_prob(old_cost, new_cost, T, tuning_options) + ap = acceptance_prob(old_cost, new_cost, T) r = random.random() if ap > r: @@ -90,7 +90,7 @@ def tune(searchspace: Searchspace, runner, tuning_options): tune.__doc__ = common.get_strategy_docstring("Simulated Annealing", _options) -def acceptance_prob(old_cost, new_cost, T, tuning_options): +def acceptance_prob(old_cost, new_cost, T): """Annealing equation, with modifications to work towards a lower value.""" res = 0.0 # if start pos is not valid, always move