@@ -1050,6 +1050,56 @@ def test_save_solutions_both_keep_adaptive_mutation_save_solutions_multi_objecti
10501050
10511051
10521052
1053+ #### List length consistency
1054+
1055+ def run_for_list_lengths (multi_objective = False ,
1056+ save_solutions = False ,
1057+ save_best_solutions = False ,
1058+ stop_at = None ):
1059+
1060+ def fitness_func_single (ga , solution , idx ):
1061+ return random .random ()
1062+
1063+ def fitness_func_multi (ga , solution , idx ):
1064+ return [random .random (), random .random ()]
1065+
1066+ fitness_func = fitness_func_multi if multi_objective else fitness_func_single
1067+
1068+ on_generation = None
1069+ if stop_at is not None :
1070+ def on_generation (ga ):
1071+ if ga .generations_completed >= stop_at :
1072+ return "stop"
1073+
1074+ ga_optimizer = pygad .GA (num_generations = num_generations ,
1075+ sol_per_pop = sol_per_pop ,
1076+ num_genes = 6 ,
1077+ num_parents_mating = num_parents_mating ,
1078+ fitness_func = fitness_func ,
1079+ on_generation = on_generation ,
1080+ save_best_solutions = save_best_solutions ,
1081+ save_solutions = save_solutions ,
1082+ random_seed = 42 ,
1083+ suppress_warnings = True )
1084+ ga_optimizer .run ()
1085+ return ga_optimizer
1086+
1087+ def test_list_lengths_best_solutions_on_generation_stop ():
1088+ ga = run_for_list_lengths (save_best_solutions = True , stop_at = 10 )
1089+ assert len (ga .best_solutions ) == len (ga .best_solutions_fitness )
1090+
1091+ def test_list_lengths_best_solutions_on_generation_stop_multi_objective ():
1092+ ga = run_for_list_lengths (multi_objective = True , save_best_solutions = True , stop_at = 10 )
1093+ assert len (ga .best_solutions ) == len (ga .best_solutions_fitness )
1094+
1095+ def test_list_lengths_best_solutions_normal_completion ():
1096+ ga = run_for_list_lengths (save_best_solutions = True )
1097+ assert len (ga .best_solutions ) == len (ga .best_solutions_fitness )
1098+
1099+ def test_list_lengths_solutions_on_generation_stop ():
1100+ ga = run_for_list_lengths (save_solutions = True , stop_at = 10 )
1101+ assert len (ga .solutions ) == len (ga .solutions_fitness )
1102+
10531103if __name__ == "__main__" :
10541104 #### Single Objective
10551105 print ()
0 commit comments