Skip to content

Commit 08a1029

Browse files
changed SA to act similar with or without constraint awareness
1 parent ebe872d commit 08a1029

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

kernel_tuner/strategies/simulated_annealing.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,28 @@ def acceptance_prob(old_cost, new_cost, T, tuning_options):
115115
def neighbor(pos, searchspace: Searchspace, constraint_aware=True):
116116
"""Return a random neighbor of pos."""
117117

118+
def random_neighbor(pos, method):
119+
"""Helper method to return a random neighbor."""
120+
neighbors = searchspace.get_neighbors_no_cache(pos, neighbor_method=method)
121+
if not neighbors:
122+
return pos
123+
return random.choice(neighbors)
124+
125+
size = len(pos)
126+
118127
if constraint_aware:
119-
# Note: this is not the same as the previous implementation, because it is possible that non-edge parameters remain the same, but suggested configurations will all be within restrictions
120-
neighbors = searchspace.get_neighbors(tuple(pos), neighbor_method='Hamming') if random.random() < 0.2 else searchspace.get_neighbors(tuple(pos), neighbor_method='strictly-adjacent')
121-
if len(neighbors) > 0:
122-
return list(random.choice(neighbors))
123-
# if there are no neighbors, return a random configuration
124-
return list(searchspace.get_random_sample(1)[0])
128+
pos = tuple(pos)
129+
130+
# Note: the following tries to mimick as much as possible the earlier version of SA but in a constraint-aware version
131+
for i in range(size):
132+
if random.random() < 0.2:
133+
pos = random_neighbor(pos, 'Hamming')
134+
pos = random_neighbor(pos, 'adjacent')
135+
136+
return list(pos)
125137

126138
else:
127139
tune_params = searchspace.tune_params
128-
size = len(pos)
129140
pos_out = []
130141
# random mutation
131142
# expected value is set that values all dimensions attempt to get mutated

0 commit comments

Comments
 (0)