From 20fc0f8be2ae5e365bd4c08b2b56182e106a7afb Mon Sep 17 00:00:00 2001 From: Ahmed Gad Date: Mon, 6 Nov 2023 15:45:27 -0500 Subject: [PATCH] More tests --- tests/test_adaptive_mutation.py | 695 +++++++++++++++++++++++++--- tests/test_allow_duplicate_genes.py | 237 ++++++++-- tests/test_crossover_mutation.py | 223 +++++++-- 3 files changed, 1022 insertions(+), 133 deletions(-) diff --git a/tests/test_adaptive_mutation.py b/tests/test_adaptive_mutation.py index 9b427df4..8a9d3906 100644 --- a/tests/test_adaptive_mutation.py +++ b/tests/test_adaptive_mutation.py @@ -24,21 +24,39 @@ def output_adaptive_mutation(gene_space=None, init_range_low=-4, init_range_high=4, initial_population=None, - mutation_probability=[0.2, 0.1], + mutation_probability=None, + mutation_num_genes=None, fitness_batch_size=None, - mutation_type="adaptive"): + mutation_type="adaptive", + parent_selection_type='sss', + multi_objective=False): - def fitness_func_single(ga, solution, idx): + def fitness_func_no_batch_single(ga, solution, idx): return random.random() - def fitness_func_batch(ga, soluions, idxs): + def fitness_func_batch_single(ga, soluions, idxs): return numpy.random.uniform(size=len(soluions)) - if fitness_batch_size in [1, None]: - fitness_func = fitness_func_single - else: - fitness_func = fitness_func_batch - + def fitness_func_no_batch_multi(ga, solution, idx): + return [random.random(), random.random()] + + def fitness_func_batch_multi(ga, soluions, idxs): + f = [] + for sol in soluions: + f.append([random.random(), random.random()]) + return f + + if fitness_batch_size is None or (type(fitness_batch_size) in pygad.GA.supported_int_types and fitness_batch_size == 1): + if multi_objective == True: + fitness_func = fitness_func_no_batch_multi + else: + fitness_func = fitness_func_no_batch_single + elif (type(fitness_batch_size) in pygad.GA.supported_int_types and fitness_batch_size > 1): + if multi_objective == True: + fitness_func = fitness_func_batch_multi + else: + fitness_func = fitness_func_batch_single + ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=5, fitness_func=fitness_func, @@ -49,11 +67,13 @@ def fitness_func_batch(ga, soluions, idxs): initial_population=initial_population, init_range_low=init_range_low, init_range_high=init_range_high, + parent_selection_type=parent_selection_type, random_mutation_min_val=random_mutation_min_val, random_mutation_max_val=random_mutation_max_val, allow_duplicate_genes=True, mutation_by_replacement=mutation_by_replacement, save_solutions=True, + mutation_num_genes=mutation_num_genes, mutation_probability=mutation_probability, mutation_type=mutation_type, suppress_warnings=True, @@ -64,28 +84,58 @@ def fitness_func_batch(ga, soluions, idxs): return None, ga_instance -def test_adaptive_mutation(): - result, ga_instance = output_adaptive_mutation() +def test_adaptive_mutation(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_gene_space(): - result, ga_instance = output_adaptive_mutation(gene_space=range(10)) +def test_adaptive_mutation_gene_space(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(gene_space=range(10), + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_int_gene_type(): - result, ga_instance = output_adaptive_mutation(gene_type=int) +def test_adaptive_mutation_int_gene_type(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(gene_type=int, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_gene_space_gene_type(): +def test_adaptive_mutation_gene_space_gene_type(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): result, ga_instance = output_adaptive_mutation(gene_space={"low": 0, "high": 10}, - gene_type=[float, 2]) + gene_type=[float, 2], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_nested_gene_space(): +def test_adaptive_mutation_nested_gene_space(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): result, ga_instance = output_adaptive_mutation(gene_space=[[0, 1, 2, 3, 4], numpy.arange(5, 10), range(10, 15), @@ -95,15 +145,29 @@ def test_adaptive_mutation_nested_gene_space(): numpy.arange(30, 35), numpy.arange(35, 40), numpy.arange(40, 45), - [45, 46, 47, 48, 49]]) + [45, 46, 47, 48, 49]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_nested_gene_type(): - result, ga_instance = output_adaptive_mutation(gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) +def test_adaptive_mutation_nested_gene_type(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_nested_gene_space_nested_gene_type(): +def test_adaptive_mutation_nested_gene_space_nested_gene_type(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): result, ga_instance = output_adaptive_mutation(gene_space=[[0, 1, 2, 3, 4], numpy.arange(5, 10), range(10, 15), @@ -114,109 +178,606 @@ def test_adaptive_mutation_nested_gene_space_nested_gene_type(): numpy.arange(35, 40), numpy.arange(40, 45), [45, 46, 47, 48, 49]], - gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) + gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_initial_population(): +def test_adaptive_mutation_initial_population(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): global initial_population - result, ga_instance = output_adaptive_mutation(initial_population=initial_population) + result, ga_instance = output_adaptive_mutation(initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_initial_population_nested_gene_type(): +def test_adaptive_mutation_initial_population_nested_gene_type(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): global initial_population result, ga_instance = output_adaptive_mutation(initial_population=initial_population, - gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) + gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) # assert result == True -def test_adaptive_mutation_fitness_batch_size_1(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=1) +def test_adaptive_mutation_fitness_batch_size_1(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=1, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_2(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=2, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_3(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=3, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_4(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=4, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_5(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=5, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_6(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=6, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_7(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=7, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_8(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=8, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_9(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=9, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +def test_adaptive_mutation_fitness_batch_size_10(multi_objective=False, + parent_selection_type='sss', + mutation_num_genes=None, + mutation_probability=None): + result, ga_instance = output_adaptive_mutation(fitness_batch_size=10, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective, + mutation_num_genes=mutation_num_genes, + mutation_probability=mutation_probability) + +if __name__ == "__main__": + #### Single-objective mutation_probability + print() + test_adaptive_mutation(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_int_gene_type(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_gene_space(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_gene_space_gene_type(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_2(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=2) + test_adaptive_mutation_nested_gene_space(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_3(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=3) + test_adaptive_mutation_nested_gene_type(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_4(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=4) + test_adaptive_mutation_initial_population(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_5(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=5) + test_adaptive_mutation_initial_population_nested_gene_type(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_6(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=6) + test_adaptive_mutation_fitness_batch_size_1(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_7(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=7) + test_adaptive_mutation_fitness_batch_size_1(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_8(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=8) + test_adaptive_mutation_fitness_batch_size_2(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_9(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=9) + test_adaptive_mutation_fitness_batch_size_3(mutation_probability=[0.2, 0.1]) + print() -def test_adaptive_mutation_fitness_batch_size_10(): - result, ga_instance = output_adaptive_mutation(fitness_batch_size=10) + test_adaptive_mutation_fitness_batch_size_4(mutation_probability=[0.2, 0.1]) + print() -if __name__ == "__main__": + test_adaptive_mutation_fitness_batch_size_5(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_6(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_7(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_8(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_9(mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_10(mutation_probability=[0.2, 0.1]) + print() + + #### Single-objective mutation_num_genes + print() + test_adaptive_mutation(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_int_gene_type(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_gene_space(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_gene_space_gene_type(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_nested_gene_space(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_nested_gene_type(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_initial_population(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_initial_population_nested_gene_type(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_1(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_1(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_2(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_3(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_4(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_5(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_6(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_7(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_8(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_9(mutation_num_genes=[6, 3]) + print() + + test_adaptive_mutation_fitness_batch_size_10(mutation_num_genes=[6, 3]) + print() + + #### Multi-objective mutation_probability + print() + test_adaptive_mutation(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_int_gene_type(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_gene_space(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_gene_space_gene_type(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_nested_gene_space(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_nested_gene_type(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_initial_population(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_initial_population_nested_gene_type(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_2(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_3(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_4(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_5(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_6(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_7(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_8(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_9(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_10(multi_objective=True, + mutation_probability=[0.2, 0.1]) + print() + + #### Multi-objective mutation_num_genes + test_adaptive_mutation(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_int_gene_type(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_gene_space(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_gene_space_gene_type(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_nested_gene_space(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_nested_gene_type(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_initial_population(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_initial_population_nested_gene_type(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_2(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_3(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_4(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_5(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_6(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_7(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_8(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_9(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + test_adaptive_mutation_fitness_batch_size_10(multi_objective=True, + mutation_num_genes=[6, 4]) + print() + + + #### Multi-objective NSGA-II Parent Selection mutation_probability + print() + test_adaptive_mutation(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_int_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_gene_space(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_gene_space_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_nested_gene_space(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_nested_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_initial_population(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_initial_population_nested_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_2(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_3(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_4(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_5(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_6(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_7(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_8(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_9(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + print() + + test_adaptive_mutation_fitness_batch_size_10(multi_objective=True, + parent_selection_type='nsga2', + mutation_probability=[0.2, 0.1]) + + #### Multi-objective NSGA-II Parent Selection mutation_num_genes print() - test_adaptive_mutation() + test_adaptive_mutation(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_int_gene_type() + test_adaptive_mutation_int_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_gene_space() + test_adaptive_mutation_gene_space(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_gene_space_gene_type() + test_adaptive_mutation_gene_space_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_nested_gene_space() + test_adaptive_mutation_nested_gene_space(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_nested_gene_type() + test_adaptive_mutation_nested_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_initial_population() + test_adaptive_mutation_initial_population(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_initial_population_nested_gene_type() + test_adaptive_mutation_initial_population_nested_gene_type(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_1() + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_1() + test_adaptive_mutation_fitness_batch_size_1(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_2() + test_adaptive_mutation_fitness_batch_size_2(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_3() + test_adaptive_mutation_fitness_batch_size_3(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_4() + test_adaptive_mutation_fitness_batch_size_4(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_5() + test_adaptive_mutation_fitness_batch_size_5(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_6() + test_adaptive_mutation_fitness_batch_size_6(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_7() + test_adaptive_mutation_fitness_batch_size_7(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_8() + test_adaptive_mutation_fitness_batch_size_8(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_9() + test_adaptive_mutation_fitness_batch_size_9(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() - test_adaptive_mutation_fitness_batch_size_10() + test_adaptive_mutation_fitness_batch_size_10(multi_objective=True, + parent_selection_type='nsga2', + mutation_num_genes=[6, 4]) print() diff --git a/tests/test_allow_duplicate_genes.py b/tests/test_allow_duplicate_genes.py index f485419e..27bdb29e 100644 --- a/tests/test_allow_duplicate_genes.py +++ b/tests/test_allow_duplicate_genes.py @@ -24,11 +24,21 @@ def number_duplicate_genes(gene_space=None, init_range_low=-4, init_range_high=4, random_seed=123, - initial_population=None): + initial_population=None, + parent_selection_type='sss', + multi_objective=False): - def fitness_func(ga, solution, idx): + def fitness_func_no_batch_single(ga, solution, idx): return random.random() + def fitness_func_no_batch_multi(ga, solution, idx): + return [random.random(), random.random()] + + if multi_objective == True: + fitness_func = fitness_func_no_batch_multi + else: + fitness_func = fitness_func_no_batch_single + ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=5, fitness_func=fitness_func, @@ -37,6 +47,7 @@ def fitness_func(ga, solution, idx): gene_space=gene_space, gene_type=gene_type, initial_population=initial_population, + parent_selection_type=parent_selection_type, init_range_low=init_range_low, init_range_high=init_range_high, random_mutation_min_val=random_mutation_min_val, @@ -64,23 +75,30 @@ def test_number_duplicates_default(): assert num_duplicates == 0 -def test_number_duplicates_default_initial_population(): - num_duplicates = number_duplicate_genes(initial_population=initial_population) +def test_number_duplicates_default_initial_population(multi_objective=False, + parent_selection_type='sss'): + num_duplicates = number_duplicate_genes(initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_float_gene_type(): +def test_number_duplicates_float_gene_type(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 num_duplicates = number_duplicate_genes(gene_type=float, num_genes=num_genes, init_range_low=0, init_range_high=1, random_mutation_min_val=0, - random_mutation_max_val=1) + random_mutation_max_val=1, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_float_gene_type_initial_population(): +def test_number_duplicates_float_gene_type_initial_population(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 num_duplicates = number_duplicate_genes(gene_type=float, num_genes=num_genes, @@ -88,11 +106,14 @@ def test_number_duplicates_float_gene_type_initial_population(): init_range_high=1, initial_population=initial_population, random_mutation_min_val=0, - random_mutation_max_val=1) + random_mutation_max_val=1, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_int_gene_type(): +def test_number_duplicates_int_gene_type(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 init_range_low = 0 init_range_high = init_range_low + num_genes @@ -104,11 +125,14 @@ def test_number_duplicates_int_gene_type(): init_range_low=init_range_low, init_range_high=init_range_high, random_mutation_min_val=random_mutation_min_val, - random_mutation_max_val=random_mutation_max_val) + random_mutation_max_val=random_mutation_max_val, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_int_gene_type_initial_population(): +def test_number_duplicates_int_gene_type_initial_population(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 init_range_low = 0 init_range_high = init_range_low + num_genes @@ -121,11 +145,14 @@ def test_number_duplicates_int_gene_type_initial_population(): init_range_high=init_range_high, initial_population=initial_population, random_mutation_min_val=random_mutation_min_val, - random_mutation_max_val=random_mutation_max_val) + random_mutation_max_val=random_mutation_max_val, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_int_gene_type_replacement(): +def test_number_duplicates_int_gene_type_replacement(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 init_range_low = 0 init_range_high = init_range_low + num_genes @@ -137,11 +164,14 @@ def test_number_duplicates_int_gene_type_replacement(): init_range_low=init_range_low, init_range_high=init_range_high, random_mutation_min_val=random_mutation_min_val, - random_mutation_max_val=random_mutation_max_val) + random_mutation_max_val=random_mutation_max_val, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_int_gene_type_replacement_initial_population(): +def test_number_duplicates_int_gene_type_replacement_initial_population(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 init_range_low = 0 init_range_high = init_range_low + num_genes @@ -154,54 +184,75 @@ def test_number_duplicates_int_gene_type_replacement_initial_population(): init_range_high=init_range_high, initial_population=initial_population, random_mutation_min_val=random_mutation_min_val, - random_mutation_max_val=random_mutation_max_val) + random_mutation_max_val=random_mutation_max_val, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_single_gene_space(): +def test_number_duplicates_single_gene_space(multi_objective=False, + parent_selection_type='sss'): num_duplicates = number_duplicate_genes(gene_space=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - num_genes=10) + num_genes=10, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_single_gene_space_initial_population(): +def test_number_duplicates_single_gene_space_initial_population(multi_objective=False, + parent_selection_type='sss'): num_duplicates = number_duplicate_genes(gene_space=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], num_genes=10, - initial_population=initial_population) + initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_single_range_gene_space(): +def test_number_duplicates_single_range_gene_space(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 num_duplicates = number_duplicate_genes(gene_space=range(num_genes), - num_genes=num_genes) + num_genes=num_genes, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_single_range_gene_space_initial_population(): +def test_number_duplicates_single_range_gene_space_initial_population(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 num_duplicates = number_duplicate_genes(gene_space=range(num_genes), num_genes=num_genes, - initial_population=initial_population) + initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_single_numpy_range_gene_space(): +def test_number_duplicates_single_numpy_range_gene_space(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 num_duplicates = number_duplicate_genes(gene_space=numpy.arange(num_genes), - num_genes=num_genes) + num_genes=num_genes, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_single_numpy_range_gene_space_initial_population(): +def test_number_duplicates_single_numpy_range_gene_space_initial_population(multi_objective=False, + parent_selection_type='sss'): num_genes = 10 num_duplicates = number_duplicate_genes(gene_space=numpy.arange(num_genes), num_genes=num_genes, - initial_population=initial_population) + initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_nested_gene_space(): +def test_number_duplicates_nested_gene_space(multi_objective=False, + parent_selection_type='sss'): num_duplicates = number_duplicate_genes(gene_space=[[0, 1], [1, 2], [2, 3], @@ -213,11 +264,14 @@ def test_number_duplicates_nested_gene_space(): [8, 9], [9, 10]], gene_type=int, - num_genes=10) + num_genes=10, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 -def test_number_duplicates_nested_gene_space_initial_population(): +def test_number_duplicates_nested_gene_space_initial_population(multi_objective=False, + parent_selection_type='sss'): num_duplicates = number_duplicate_genes(gene_space=[[0, 1], [1, 2], [2, 3], @@ -230,7 +284,9 @@ def test_number_duplicates_nested_gene_space_initial_population(): [9, 10]], gene_type=int, num_genes=10, - initial_population=initial_population) + initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 @@ -274,7 +330,8 @@ def test_number_duplicates_nested_gene_space_initial_population(): # assert num_duplicates == 0 -def test_number_duplicates_nested_gene_space_nested_gene_type_initial_population(): +def test_number_duplicates_nested_gene_space_nested_gene_type_initial_population(multi_objective=False, + parent_selection_type='sss'): num_duplicates = number_duplicate_genes(gene_space=[[0, 1], [1, 2], [2, 3], @@ -287,11 +344,14 @@ def test_number_duplicates_nested_gene_space_nested_gene_type_initial_population [9, 10]], gene_type=[int, int, int, int, int, int, int, int, int, int], num_genes=10, - initial_population=initial_population) + initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert num_duplicates == 0 if __name__ == "__main__": + #### Single-objective print() test_number_duplicates_default() print() @@ -339,3 +399,112 @@ def test_number_duplicates_nested_gene_space_nested_gene_type_initial_population test_number_duplicates_nested_gene_space_nested_gene_type_initial_population() print() + #### Multi-objective + print() + test_number_duplicates_default_initial_population(multi_objective=True) + print() + + test_number_duplicates_float_gene_type(multi_objective=True) + print() + test_number_duplicates_float_gene_type_initial_population(multi_objective=True) + print() + + test_number_duplicates_int_gene_type(multi_objective=True) + print() + test_number_duplicates_int_gene_type_initial_population(multi_objective=True) + print() + + test_number_duplicates_int_gene_type_replacement(multi_objective=True) + print() + test_number_duplicates_int_gene_type_replacement_initial_population(multi_objective=True) + print() + + test_number_duplicates_single_gene_space(multi_objective=True) + print() + test_number_duplicates_single_gene_space_initial_population(multi_objective=True) + print() + + test_number_duplicates_single_range_gene_space(multi_objective=True) + print() + test_number_duplicates_single_range_gene_space_initial_population(multi_objective=True) + print() + + test_number_duplicates_single_numpy_range_gene_space(multi_objective=True) + print() + test_number_duplicates_single_numpy_range_gene_space_initial_population(multi_objective=True) + print() + + test_number_duplicates_nested_gene_space(multi_objective=True) + print() + test_number_duplicates_nested_gene_space_initial_population(multi_objective=True) + print() + + # This example causes duplicates that can only be solved by changing a chain of genes. + # test_number_duplicates_nested_gene_space_nested_gene_type(multi_objective=True) + # print() + test_number_duplicates_nested_gene_space_nested_gene_type_initial_population(multi_objective=True) + print() + + #### Multi-objective NSGA-II Parent Selection + print() + test_number_duplicates_default_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_float_gene_type(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_float_gene_type_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_int_gene_type(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_int_gene_type_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_int_gene_type_replacement(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_int_gene_type_replacement_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_single_gene_space(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_single_gene_space_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_single_range_gene_space(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_single_range_gene_space_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_single_numpy_range_gene_space(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_single_numpy_range_gene_space_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_number_duplicates_nested_gene_space(multi_objective=True, + parent_selection_type='nsga2') + print() + test_number_duplicates_nested_gene_space_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + # This example causes duplicates that can only be solved by changing a chain of genes. + # test_number_duplicates_nested_gene_space_nested_gene_type(multi_objective=True, + # parent_selection_type='nsga2') + # print() + test_number_duplicates_nested_gene_space_nested_gene_type_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + diff --git a/tests/test_crossover_mutation.py b/tests/test_crossover_mutation.py index 6b964c2b..a11d65ca 100644 --- a/tests/test_crossover_mutation.py +++ b/tests/test_crossover_mutation.py @@ -27,11 +27,21 @@ def output_crossover_mutation(gene_space=None, crossover_probability=None, mutation_probability=None, crossover_type=None, - mutation_type=None): + mutation_type=None, + parent_selection_type='sss', + multi_objective=False): - def fitness_func(ga, solution, idx): + def fitness_func_no_batch_single(ga, solution, idx): return random.random() + def fitness_func_no_batch_multi(ga, solution, idx): + return [random.random(), random.random()] + + if multi_objective == True: + fitness_func = fitness_func_no_batch_multi + else: + fitness_func = fitness_func_no_batch_single + ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=5, fitness_func=fitness_func, @@ -39,6 +49,7 @@ def fitness_func(ga, solution, idx): num_genes=num_genes, gene_space=gene_space, gene_type=gene_type, + parent_selection_type=parent_selection_type, initial_population=initial_population, init_range_low=init_range_low, init_range_high=init_range_high, @@ -69,30 +80,42 @@ def fitness_func(ga, solution, idx): print(f"Comparison result is {result}") return result, ga_instance -def test_no_crossover_no_mutation(): - result, ga_instance = output_crossover_mutation() +def test_no_crossover_no_mutation(multi_objective=False, + parent_selection_type='sss'): + result, ga_instance = output_crossover_mutation(parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_gene_space(): - result, ga_instance = output_crossover_mutation(gene_space=range(10)) +def test_no_crossover_no_mutation_gene_space(multi_objective=False, + parent_selection_type='sss'): + result, ga_instance = output_crossover_mutation(gene_space=range(10), + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_int_gene_type(): - result, ga_instance = output_crossover_mutation(gene_type=int) +def test_no_crossover_no_mutation_int_gene_type(multi_objective=False, + parent_selection_type='sss'): + result, ga_instance = output_crossover_mutation(gene_type=int, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_gene_space_gene_type(): +def test_no_crossover_no_mutation_gene_space_gene_type(multi_objective=False, + parent_selection_type='sss'): result, ga_instance = output_crossover_mutation(gene_space={"low": 0, "high": 10}, - gene_type=[float, 2]) + gene_type=[float, 2], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_nested_gene_space(): +def test_no_crossover_no_mutation_nested_gene_space(multi_objective=False, + parent_selection_type='sss'): result, ga_instance = output_crossover_mutation(gene_space=[[0, 1, 2, 3, 4], numpy.arange(5, 10), range(10, 15), @@ -102,15 +125,21 @@ def test_no_crossover_no_mutation_nested_gene_space(): numpy.arange(30, 35), numpy.arange(35, 40), numpy.arange(40, 45), - [45, 46, 47, 48, 49]]) + [45, 46, 47, 48, 49]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_nested_gene_type(): - result, ga_instance = output_crossover_mutation(gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) +def test_no_crossover_no_mutation_nested_gene_type(multi_objective=False, + parent_selection_type='sss'): + result, ga_instance = output_crossover_mutation(gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_nested_gene_space_nested_gene_type(): +def test_no_crossover_no_mutation_nested_gene_space_nested_gene_type(multi_objective=False, + parent_selection_type='sss'): result, ga_instance = output_crossover_mutation(gene_space=[[0, 1, 2, 3, 4], numpy.arange(5, 10), range(10, 15), @@ -121,43 +150,60 @@ def test_no_crossover_no_mutation_nested_gene_space_nested_gene_type(): numpy.arange(35, 40), numpy.arange(40, 45), [45, 46, 47, 48, 49]], - gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) + gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_initial_population(): +def test_no_crossover_no_mutation_initial_population(multi_objective=False, + parent_selection_type='sss'): global initial_population - result, ga_instance = output_crossover_mutation(initial_population=initial_population) + result, ga_instance = output_crossover_mutation(initial_population=initial_population, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_no_crossover_no_mutation_initial_population_nested_gene_type(): +def test_no_crossover_no_mutation_initial_population_nested_gene_type(multi_objective=False, + parent_selection_type='sss'): global initial_population result, ga_instance = output_crossover_mutation(initial_population=initial_population, - gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) + gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]], + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_crossover_no_mutation_zero_crossover_probability(): +def test_crossover_no_mutation_zero_crossover_probability(multi_objective=False, + parent_selection_type='sss'): global initial_population result, ga_instance = output_crossover_mutation(crossover_type="single_point", - crossover_probability=0.0) + crossover_probability=0.0, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_zero_crossover_probability_zero_mutation_probability(): +def test_zero_crossover_probability_zero_mutation_probability(multi_objective=False, + parent_selection_type='sss'): global initial_population result, ga_instance = output_crossover_mutation(crossover_type="single_point", crossover_probability=0.0, mutation_type="random", - mutation_probability=0.0) + mutation_probability=0.0, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) assert result == True -def test_random_mutation_manual_call(): +def test_random_mutation_manual_call(multi_objective=False, + parent_selection_type='sss'): result, ga_instance = output_crossover_mutation(mutation_type="random", random_mutation_min_val=888, - random_mutation_max_val=999) + random_mutation_max_val=999, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) ga_instance.mutation_num_genes = 9 temp_offspring = numpy.array(initial_population[0:1]) @@ -171,10 +217,13 @@ def test_random_mutation_manual_call(): assert len(comp_sorted) in range(1, 1 + 1 + ga_instance.mutation_num_genes) assert comp_sorted[0] == 0 -def test_random_mutation_manual_call2(): +def test_random_mutation_manual_call2(multi_objective=False, + parent_selection_type='sss'): result, ga_instance = output_crossover_mutation(mutation_type="random", random_mutation_min_val=888, - random_mutation_max_val=999) + random_mutation_max_val=999, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) ga_instance.mutation_num_genes = 10 temp_offspring = numpy.array(initial_population[0:1]) @@ -188,14 +237,17 @@ def test_random_mutation_manual_call2(): assert len(comp_sorted) in range(1, 1 + 1 + ga_instance.mutation_num_genes) # assert comp_sorted[0] == 0 -def test_random_mutation_manual_call3(): +def test_random_mutation_manual_call3(multi_objective=False, + parent_selection_type='sss'): # Use random_mutation_min_val & random_mutation_max_val as numbers. random_mutation_min_val = 888 random_mutation_max_val = 999 result, ga_instance = output_crossover_mutation(mutation_type="random", random_mutation_min_val=random_mutation_min_val, random_mutation_max_val=random_mutation_max_val, - mutation_by_replacement=True) + mutation_by_replacement=True, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) ga_instance.mutation_num_genes = 10 temp_offspring = numpy.array(initial_population[0:1]) @@ -209,14 +261,17 @@ def test_random_mutation_manual_call3(): for value in comp_sorted: assert value in value_space -def test_random_mutation_manual_call4(): +def test_random_mutation_manual_call4(multi_objective=False, + parent_selection_type='sss'): # Use random_mutation_min_val & random_mutation_max_val as lists. random_mutation_min_val = [888]*10 random_mutation_max_val = [999]*10 result, ga_instance = output_crossover_mutation(mutation_type="random", random_mutation_min_val=random_mutation_min_val, random_mutation_max_val=random_mutation_max_val, - mutation_by_replacement=True) + mutation_by_replacement=True, + parent_selection_type=parent_selection_type, + multi_objective=multi_objective) ga_instance.mutation_num_genes = 10 temp_offspring = numpy.array(initial_population[0:1]) @@ -231,6 +286,7 @@ def test_random_mutation_manual_call4(): assert value in value_space if __name__ == "__main__": + #### Single-objective print() test_no_crossover_no_mutation() print() @@ -274,3 +330,106 @@ def test_random_mutation_manual_call4(): test_random_mutation_manual_call4() print() + #### Multi-objective + print() + test_no_crossover_no_mutation(multi_objective=True) + print() + + test_no_crossover_no_mutation_int_gene_type(multi_objective=True) + print() + + test_no_crossover_no_mutation_gene_space(multi_objective=True) + print() + + test_no_crossover_no_mutation_gene_space_gene_type(multi_objective=True) + print() + + test_no_crossover_no_mutation_nested_gene_space(multi_objective=True) + print() + + test_no_crossover_no_mutation_nested_gene_type(multi_objective=True) + print() + + test_no_crossover_no_mutation_initial_population(multi_objective=True) + print() + + test_no_crossover_no_mutation_initial_population_nested_gene_type(multi_objective=True) + print() + + test_crossover_no_mutation_zero_crossover_probability(multi_objective=True) + print() + + test_zero_crossover_probability_zero_mutation_probability(multi_objective=True) + print() + + test_random_mutation_manual_call(multi_objective=True) + print() + + test_random_mutation_manual_call2(multi_objective=True) + print() + + test_random_mutation_manual_call3(multi_objective=True) + print() + + test_random_mutation_manual_call4(multi_objective=True) + print() + + + #### Multi-objective NSGA-II Parent Selection + print() + test_no_crossover_no_mutation(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_int_gene_type(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_gene_space(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_gene_space_gene_type(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_nested_gene_space(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_nested_gene_type(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_initial_population(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_no_crossover_no_mutation_initial_population_nested_gene_type(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_crossover_no_mutation_zero_crossover_probability(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_zero_crossover_probability_zero_mutation_probability(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_random_mutation_manual_call(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_random_mutation_manual_call2(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_random_mutation_manual_call3(multi_objective=True, + parent_selection_type='nsga2') + print() + + test_random_mutation_manual_call4(multi_objective=True, + parent_selection_type='nsga2') + print() +