Skip to content

Commit b3b94ec

Browse files
fixing SA after fix in costfunc
1 parent c84227d commit b3b94ec

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

kernel_tuner/strategies/simulated_annealing.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
5454
print(e)
5555
return cost_func.results
5656

57-
ap = acceptance_prob(old_cost, new_cost, T)
57+
ap = acceptance_prob(old_cost, new_cost, T, tuning_options)
5858
r = random.random()
5959

6060
if ap > r:
@@ -85,20 +85,26 @@ def tune(searchspace: Searchspace, runner, tuning_options):
8585

8686
tune.__doc__ = common.get_strategy_docstring("Simulated Annealing", _options)
8787

88-
def acceptance_prob(old_cost, new_cost, T):
88+
def acceptance_prob(old_cost, new_cost, T, tuning_options):
8989
"""Annealing equation, with modifications to work towards a lower value."""
9090
error_val = sys.float_info.max
91+
res = 0.0
9192
# if start pos is not valid, always move
9293
if old_cost == error_val:
93-
return 1.0
94+
res = 1.0
9495
# if we have found a valid ps before, never move to nonvalid pos
95-
if new_cost == error_val:
96-
return 0.0
96+
elif new_cost == error_val:
97+
res = 0.0
9798
# always move if new cost is better
98-
if new_cost < old_cost:
99-
return 1.0
99+
elif new_cost < old_cost:
100+
res = 1.0
100101
# maybe move if old cost is better than new cost depending on T and random value
101-
return np.exp(((old_cost-new_cost)/old_cost)/T)
102+
else:
103+
if tuning_options.objective_higher_is_better:
104+
res = np.exp(((new_cost-old_cost)/new_cost)/T)
105+
else:
106+
res = np.exp(((old_cost-new_cost)/old_cost)/T)
107+
return res
102108

103109

104110
def neighbor(pos, searchspace: Searchspace):

0 commit comments

Comments
 (0)