@@ -65,7 +65,6 @@ def values_to_indices(individual_values, tune_params):
6565def indices_to_values (individual_indices , tune_params ):
6666 """Converts an individual's index vector back to its values."""
6767 tune_params_list = list (tune_params .values ())
68- print (f"{ tune_params_list = } { individual_indices = } " )
6968 values = []
7069 for dim , idx in enumerate (individual_indices ):
7170 values .append (tune_params_list [dim ][idx ])
@@ -78,18 +77,22 @@ def parse_method(method):
7877 match = re .fullmatch (pattern , method )
7978
8079 if match :
81- return match .group (1 ) == "best" , int (match .group (2 )), mutation [match .group (2 )], crossover [match .group (3 )]
80+ if match .group (1 ) in ["currenttobest" , "randtobest" ]:
81+ mutation_method = mutation [match .group (1 )]
82+ else :
83+ mutation_method = mutation [match .group (2 )]
84+ return match .group (1 ) == "best" , int (match .group (2 )), mutation_method , crossover [match .group (3 )]
8285 else :
8386 raise ValueError ("Error parsing differential evolution method" )
8487
8588
86- def random_draw (idxs , mutation , best ):
89+ def random_draw (idxs , mutate , best ):
8790 """
8891 Draw requested number of random individuals.
8992
9093 Draw without replacement unless there is not enough to draw from.
9194 """
92- draw = 2 * mutation + 1 - int (best )
95+ draw = 2 * mutate + 1 - int (best )
9396 return np .random .choice (idxs , draw , replace = draw >= len (idxs ))
9497
9598
@@ -98,23 +101,6 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
98101 A basic implementation of the Differential Evolution algorithm.
99102
100103 This function finds the minimum of a given cost function within specified bounds.
101-
102- Args:
103- cost_func (callable): The objective function to be minimized. It should take a
104- single argument (a numpy array of parameters) and return a
105- single scalar value (the cost).
106- bounds (list of tuples): A list where each tuple contains the (min, max) bounds
107- for each parameter. e.g., [(-5, 5), (-5, 5)]
108- popsize (int): The size of the population.
109- maxiter (int): The maximum number of generations to run.
110- F (float): The mutation factor, also known as the differential weight.
111- Should be in the range [0, 2].
112- CR (float): The crossover probability. Should be in the range [0, 1].
113- verbose (bool): If True, prints the progress of the algorithm at each generation.
114-
115- Returns:
116- dict: A dictionary containing the best solution found ('solution') and its
117- corresponding cost ('cost').
118104 """
119105 tune_params = cost_func .tuning_options .tune_params
120106 min_idx = np .zeros (len (tune_params ))
@@ -335,7 +321,7 @@ def exponential_crossover(donor_vector, target, CR):
335321mutation = {
336322 "1" : mutate_de_1 ,
337323 "2" : mutate_de_2 ,
338- "currenttobest1 " : mutate_currenttobest1 ,
339- "randtobest1 " : mutate_randtobest1 ,
324+ "currenttobest " : mutate_currenttobest1 ,
325+ "randtobest " : mutate_randtobest1 ,
340326}
341327crossover = {"bin" : binomial_crossover , "exp" : exponential_crossover }
0 commit comments