diff --git a/kernel_tuner/strategies/simulated_annealing.py b/kernel_tuner/strategies/simulated_annealing.py index 5055f302..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 @@ -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