11"""A simple Different Evolution for parameter search."""
2+
23import re
34import numpy as np
45
1213 maxiter = ("maximum number of generations" , 200 ),
1314 F = ("mutation factor (differential weight)" , 0.8 ),
1415 CR = ("crossover rate" , 0.9 ),
15- method = ("method" , "best1bin" )
16+ method = ("method" , "best1bin" ),
1617)
1718
18- supported_methods = ["best1bin" , "rand1bin" , "best2bin" , "rand2bin" , "best1exp" , "rand1exp" , "best2exp" , "rand2exp" , "currenttobest1bin" , "currenttobest1exp" , "randtobest1bin" , "randtobest1exp" ]
19+ supported_methods = [
20+ "best1bin" ,
21+ "rand1bin" ,
22+ "best2bin" ,
23+ "rand2bin" ,
24+ "best1exp" ,
25+ "rand1exp" ,
26+ "best2exp" ,
27+ "rand2exp" ,
28+ "currenttobest1bin" ,
29+ "currenttobest1exp" ,
30+ "randtobest1bin" ,
31+ "randtobest1exp" ,
32+ ]
1933
2034
2135def tune (searchspace : Searchspace , runner , tuning_options ):
@@ -59,7 +73,7 @@ def indices_to_values(individual_indices, tune_params):
5973
6074
6175def parse_method (method ):
62- """ Helper func to parse the preferred method into its components. """
76+ """Helper func to parse the preferred method into its components."""
6377 pattern = r"^(best|rand|currenttobest|randtobest)(1|2)(bin|exp)$"
6478 match = re .fullmatch (pattern , method )
6579
@@ -76,7 +90,7 @@ def random_draw(idxs, mutation, best):
7690 Draw without replacement unless there is not enough to draw from.
7791 """
7892 draw = 2 * mutation + 1 - int (best )
79- return np .random .choice (idxs , draw , replace = draw >= len (idxs ))
93+ return np .random .choice (idxs , draw , replace = draw >= len (idxs ))
8094
8195
8296def differential_evolution (searchspace , cost_func , bounds , popsize , maxiter , F , CR , method , verbose ):
@@ -104,15 +118,12 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
104118 """
105119 tune_params = cost_func .tuning_options .tune_params
106120 min_idx = np .zeros (len (tune_params ))
107- max_idx = [len (v )- 1 for v in tune_params .values ()]
121+ max_idx = [len (v ) - 1 for v in tune_params .values ()]
108122
109123 best , mutation , mutation_method , crossover_method = parse_method (method )
110124
111125 # --- 1. Initialization ---
112126
113- # Get the number of dimensions from the bounds list
114- dimensions = len (bounds )
115-
116127 # Convert bounds to a numpy array for easier manipulation
117128 bounds = np .array (bounds )
118129
@@ -187,11 +198,11 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
187198 if verbose :
188199 print (f"Generation { generation + 1 } , Best Cost: { best_cost :.6f} " )
189200
190- return {' solution' : best_solution , ' cost' : best_cost }
201+ return {" solution" : best_solution , " cost" : best_cost }
191202
192203
193204def round_and_clip (mutant_idx_float , min_idx , max_idx ):
194- """ Helper func to round floating index to nearest integer and clip within bounds. """
205+ """Helper func to round floating index to nearest integer and clip within bounds."""
195206 # Round to the nearest integer
196207 rounded_idx = np .round (mutant_idx_float )
197208
@@ -277,7 +288,7 @@ def mutate_de_2(best_idx, randos_idx, F, min_idx, max_idx, best):
277288
278289
279290def binomial_crossover (donor_vector , target , CR ):
280- """ Performs binomial crossover of donor_vector with target given crossover rate CR. """
291+ """Performs binomial crossover of donor_vector with target given crossover rate CR."""
281292 # Create the trial vector by mixing parameters from the target and donor vectors
282293 trial_vector = np .copy (target )
283294 dimensions = len (donor_vector )
@@ -320,5 +331,11 @@ def exponential_crossover(donor_vector, target, CR):
320331
321332 return trial_idx
322333
323- mutation = {"1" : mutate_de_1 , "2" : mutate_de_2 , "currenttobest1" : mutate_currenttobest1 , "randtobest1" : mutate_randtobest1 }
334+
335+ mutation = {
336+ "1" : mutate_de_1 ,
337+ "2" : mutate_de_2 ,
338+ "currenttobest1" : mutate_currenttobest1 ,
339+ "randtobest1" : mutate_randtobest1 ,
340+ }
324341crossover = {"bin" : binomial_crossover , "exp" : exponential_crossover }
0 commit comments