Skip to content

Commit ac6900f

Browse files
committed
Fix bug in simulated annealing when dealing with negative objectives
1 parent 5a48c0b commit ac6900f

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

kernel_tuner/strategies/simulated_annealing.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ def acceptance_prob(old_cost, new_cost, T, tuning_options):
104104
res = 1.0
105105
# maybe move if old cost is better than new cost depending on T and random value
106106
else:
107-
if tuning_options.objective_higher_is_better:
108-
res = np.exp(((new_cost-old_cost)/new_cost)/T)
109-
else:
110-
res = np.exp(((old_cost-new_cost)/old_cost)/T)
107+
# we must have abs_diff <= 0, as new_cost >= old_cost
108+
abs_diff = old_cost - new_cost
109+
110+
# relative to abs(old_cost), as the cost might be negative
111+
rel_diff = abs_diff / np.abs(old_cost)
112+
113+
# exponential decay
114+
res = np.exp(rel_diff / T)
111115
return res
112116

113117

0 commit comments

Comments
 (0)