Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions kernel_tuner/strategies/simulated_annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down