Skip to content

Commit 9196266

Browse files
committed
Improvements to constraint-aware strategies
1 parent 2e718f4 commit 9196266

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

kernel_tuner/strategies/genetic_algorithm.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,40 @@
1212
_options = dict(
1313
popsize=("population size", 30),
1414
maxiter=("maximum number of generations", 30),
15-
constraint_aware=("constraint-aware optimization (True/False)", True),
1615
method=("crossover method to use, choose any from single_point, two_point, uniform, disruptive_uniform", "uniform"),
1716
mutation_chance=("chance to mutate is 1 in mutation_chance", 20),
17+
constraint_aware=("constraint-aware optimization (True/False)", True),
1818
)
1919

2020

2121
def tune(searchspace: Searchspace, runner, tuning_options):
2222

2323
options = tuning_options.strategy_options
24-
pop_size, generations, constraint_aware, method, mutation_chance = common.get_options(options, _options)
24+
pop_size, generations, method, mutation_chance, constraint_aware = common.get_options(options, _options)
2525

26-
GA = GeneticAlgorithm(pop_size, searchspace, constraint_aware, method, mutation_chance)
26+
# if necessary adjust the popsize to a sensible value based on search space size
27+
pop_size = min(round((searchspace.size / generations) * 3), pop_size)
2728

28-
# if left to the default, adjust the popsize to a sensible value for small search spaces
29-
if pop_size == _options["popsize"][1]:
30-
pop_size = min(round(searchspace.size / 2), pop_size)
31-
else:
32-
# otherwise, just make sure it doesn't exceed the search space size
33-
pop_size = min(searchspace.size, pop_size)
29+
GA = GeneticAlgorithm(pop_size, searchspace, method, mutation_chance, constraint_aware)
3430

3531
best_score = 1e20
3632
cost_func = CostFunc(searchspace, tuning_options, runner)
33+
num_evaluated = 0
3734

3835
population = GA.generate_population()
3936

4037
for generation in range(generations):
38+
if any([not searchspace.is_param_config_valid(tuple(dna)) for dna in population]):
39+
raise ValueError(f"Generation {generation}/{generations}, population validity: {[searchspace.is_param_config_valid(tuple(dna)) for dna in population]}")
4140

4241
# determine fitness of population members
4342
weighted_population = []
4443
for dna in population:
4544
try:
4645
# if we are not constraint-aware we should check restrictions upon evaluation
4746
time = cost_func(dna, check_restrictions=not constraint_aware)
48-
except util.StopCriterionReached as e:
47+
num_evaluated += 1
48+
except StopCriterionReached as e:
4949
if tuning_options.verbose:
5050
print(e)
5151
return cost_func.results
@@ -68,15 +68,15 @@ def tune(searchspace: Searchspace, runner, tuning_options):
6868
population = []
6969

7070
# crossover and mutate
71-
while len(population) < pop_size:
71+
while len(population) < pop_size and searchspace.size > num_evaluated + len(population):
7272
dna1, dna2 = GA.weighted_choice(weighted_population, 2)
7373

7474
children = GA.crossover(dna1, dna2)
7575

7676
for child in children:
7777
child = GA.mutate(child)
7878

79-
if child not in population:
79+
if child not in population and searchspace.is_param_config_valid(tuple(child)):
8080
population.append(child)
8181

8282
if len(population) >= pop_size:
@@ -91,13 +91,13 @@ def tune(searchspace: Searchspace, runner, tuning_options):
9191

9292
class GeneticAlgorithm:
9393

94-
def __init__(self, pop_size, searchspace, constraint_aware=False, method="uniform", mutation_chance=10):
94+
def __init__(self, pop_size, searchspace, method="uniform", mutation_chance=10, constraint_aware=True):
9595
self.pop_size = pop_size
9696
self.searchspace = searchspace
9797
self.tune_params = searchspace.tune_params.copy()
98-
self.constraint_aware = constraint_aware
9998
self.crossover_method = supported_methods[method]
10099
self.mutation_chance = mutation_chance
100+
self.constraint_aware = constraint_aware
101101

102102
def generate_population(self):
103103
""" Constraint-aware population creation method """

kernel_tuner/strategies/pso.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
w=("Inertia weight constant", 0.5),
1717
c1=("Cognitive constant", 3.0),
1818
c2=("Social constant", 1.5),
19-
constraint_aware=("constraint-aware optimization (True/False)", False))
19+
constraint_aware=("constraint-aware optimization (True/False)", True))
2020

2121
def tune(searchspace: Searchspace, runner, tuning_options):
2222

0 commit comments

Comments
 (0)