44def BMR_algorithm (bounds , num_iterations , population_size , num_variables , objective_func , constraints = None ):
55 population = np .random .uniform (low = bounds [:, 0 ], high = bounds [:, 1 ], size = (population_size , num_variables ))
66
7+ best_scores = []
8+
79 for iteration in range (num_iterations ):
810 if constraints :
911 fitness = [constrained_objective_function (ind , objective_func , constraints ) for ind in population ]
@@ -12,6 +14,8 @@ def BMR_algorithm(bounds, num_iterations, population_size, num_variables, object
1214
1315 best_solution = population [np .argmin (fitness )]
1416 mean_solution = np .mean (population , axis = 0 )
17+ best_score = np .min (fitness )
18+ best_scores .append (best_score )
1519
1620 for i in range (population_size ):
1721 r1 , r2 , r3 , r4 = np .random .rand (4 )
@@ -23,15 +27,16 @@ def BMR_algorithm(bounds, num_iterations, population_size, num_variables, object
2327 else :
2428 population [i ] = bounds [:, 1 ] - (bounds [:, 1 ] - bounds [:, 0 ]) * r3
2529
26- print ( f"Iteration { iteration + 1 } , Best Fitness: { np . min ( fitness ) } " )
30+ population = np . clip ( population , bounds [:, 0 ], bounds [:, 1 ] )
2731
28- best_fitness = np .min (fitness )
29- return best_fitness
32+ return best_solution , best_scores
3033
3134
3235def BWR_algorithm (bounds , num_iterations , population_size , num_variables , objective_func , constraints = None ):
3336 population = np .random .uniform (low = bounds [:, 0 ], high = bounds [:, 1 ], size = (population_size , num_variables ))
3437
38+ best_scores = []
39+
3540 for iteration in range (num_iterations ):
3641 if constraints :
3742 fitness = [constrained_objective_function (ind , objective_func , constraints ) for ind in population ]
@@ -40,6 +45,8 @@ def BWR_algorithm(bounds, num_iterations, population_size, num_variables, object
4045
4146 best_solution = population [np .argmin (fitness )]
4247 worst_solution = population [np .argmax (fitness )]
48+ best_score = np .min (fitness )
49+ best_scores .append (best_score )
4350
4451 for i in range (population_size ):
4552 r1 , r2 , r3 , r4 = np .random .rand (4 )
@@ -51,7 +58,6 @@ def BWR_algorithm(bounds, num_iterations, population_size, num_variables, object
5158 else :
5259 population [i ] = bounds [:, 1 ] - (bounds [:, 1 ] - bounds [:, 0 ]) * r3
5360
54- print ( f"Iteration { iteration + 1 } , Best Fitness: { np . min ( fitness ) } " )
61+ population = np . clip ( population , bounds [:, 0 ], bounds [:, 1 ] )
5562
56- best_fitness = np .min (fitness )
57- return best_fitness
63+ return best_solution , best_scores
0 commit comments