77from kernel_tuner .searchspace import Searchspace
88from kernel_tuner .strategies import common
99from kernel_tuner .strategies .common import CostFunc
10- from kernel_tuner .runners .parallel import ParallelRunner
1110
1211_options = dict (
1312 popsize = ("population size" , 20 ),
1413 maxiter = ("maximum number of generations" , 100 ),
1514 method = ("crossover method to use, choose any from single_point, two_point, uniform, disruptive_uniform" , "uniform" ),
1615 mutation_chance = ("chance to mutate is 1 in mutation_chance" , 10 ),
17- population = ("initial population" , None ),
1816)
1917
2018
2119def tune (searchspace : Searchspace , runner , tuning_options ):
2220
2321 options = tuning_options .strategy_options
24- pop_size , generations , method , mutation_chance , population = common .get_options (options , _options )
22+ pop_size , generations , method , mutation_chance = common .get_options (options , _options )
2523 crossover = supported_methods [method ]
2624
2725 best_score = 1e20
2826 cost_func = CostFunc (searchspace , tuning_options , runner )
2927
30- if not population :
31- population = list (list (p ) for p in searchspace .get_random_sample (pop_size ))
32- else :
33- pop_size = len (population )
34-
35- old_population = population
28+ population = list (list (p ) for p in searchspace .get_random_sample (pop_size ))
29+
3630 for generation in range (generations ):
3731
38- # Evaluate the entire population
39- try :
40- old_population = population
41- weighted_population = evaluate_population (runner , cost_func , population )
42- except util .StopCriterionReached as e :
43- if tuning_options .verbose :
44- print (e )
45- return cost_func .results
32+ # determine fitness of population members
33+ weighted_population = []
34+ for dna in population :
35+ try :
36+ time = cost_func (dna , check_restrictions = False )
37+ except util .StopCriterionReached as e :
38+ if tuning_options .verbose :
39+ print (e )
40+ return cost_func .results
41+
42+ weighted_population .append ((dna , time ))
4643
4744 # population is sorted such that better configs have higher chance of reproducing
4845 weighted_population .sort (key = lambda x : x [1 ])
@@ -72,8 +69,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
7269 break
7370
7471 # could combine old + new generation here and do a selection
75- tuning_options .strategy_options ["population" ] = old_population # for memetic strategy
76- tuning_options .strategy_options ["candidates" ] = population # for memetic strategy
72+
7773 return cost_func .results
7874
7975
@@ -180,28 +176,4 @@ def disruptive_uniform_crossover(dna1, dna2):
180176 "two_point" : two_point_crossover ,
181177 "uniform" : uniform_crossover ,
182178 "disruptive_uniform" : disruptive_uniform_crossover ,
183- }
184-
185- def evaluate_population (runner , cost_func , population ):
186- """
187- Evaluate the population based on the type of runner.
188-
189- Parameters:
190- - runner: The runner (ParallelRunner or SequentialRunner) determining how to process evaluations.
191- - cost_func: A function capable of evaluating the population.
192- - population: List of individuals to be evaluated.
193-
194- Returns:
195- - List of tuples (dna, fitness_score) representing the population and their evaluation results.
196- """
197- if isinstance (runner , ParallelRunner ):
198- # Process the whole population at once if using a ParallelRunner
199- results = cost_func (population , check_restrictions = False )
200- return list (zip (population , results ))
201- else :
202- # Process each individual sequentially for SequentialRunner
203- weighted_population = []
204- for dna in population :
205- time = cost_func (dna , check_restrictions = False ) # Cost function called with a single-element list
206- weighted_population .append ((dna , time ))
207- return weighted_population
179+ }
0 commit comments