Skip to content

Commit f2902b5

Browse files
committed
Implemented a fix for the non-constrained version of GA
1 parent ec51b0a commit f2902b5

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

kernel_tuner/strategies/genetic_algorithm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
3636
population = GA.generate_population()
3737

3838
for generation in range(generations):
39-
if any([not searchspace.is_param_config_valid(tuple(dna)) for dna in population]):
39+
if constraint_aware and any([not searchspace.is_param_config_valid(tuple(dna)) for dna in population]):
4040
raise ValueError(f"Generation {generation}/{generations}, population validity: {[searchspace.is_param_config_valid(tuple(dna)) for dna in population]}")
4141

4242
# determine fitness of population members
@@ -77,7 +77,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
7777
for child in children:
7878
child = GA.mutate(child)
7979

80-
if child not in population and searchspace.is_param_config_valid(tuple(child)):
80+
if child not in population and (not constraint_aware or searchspace.is_param_config_valid(tuple(child))):
8181
population.append(child)
8282

8383
if len(population) >= pop_size:
@@ -161,7 +161,7 @@ def mutate(self, dna, cache=False):
161161
if cache:
162162
neighbors = self.searchspace.get_neighbors(tuple(dna), neighbor_method="Hamming")
163163
else:
164-
neighbors = self.searchspace.get_neighbors(tuple(dna), neighbor_method="Hamming")
164+
neighbors = self.searchspace.get_neighbors_no_cache(tuple(dna), neighbor_method="Hamming")
165165
if len(neighbors) > 0:
166166
return list(random.choice(neighbors))
167167
else:

0 commit comments

Comments
 (0)