|
| 1 | +import pygad |
| 2 | +import random |
| 3 | +import numpy |
| 4 | + |
| 5 | +num_generations = 1 |
| 6 | + |
| 7 | +initial_population = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 8 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 9 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 10 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 11 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 12 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 13 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 14 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 15 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 16 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] |
| 17 | + |
| 18 | +def output_adaptive_mutation(gene_space=None, |
| 19 | + gene_type=float, |
| 20 | + num_genes=10, |
| 21 | + mutation_by_replacement=False, |
| 22 | + random_mutation_min_val=-1, |
| 23 | + random_mutation_max_val=1, |
| 24 | + init_range_low=-4, |
| 25 | + init_range_high=4, |
| 26 | + initial_population=None, |
| 27 | + mutation_probability=[0.2, 0.1], |
| 28 | + fitness_batch_size=None, |
| 29 | + mutation_type="adaptive"): |
| 30 | + |
| 31 | + def fitness_func_single(ga, solution, idx): |
| 32 | + return random.random() |
| 33 | + |
| 34 | + def fitness_func_batch(ga, soluions, idxs): |
| 35 | + return numpy.random.uniform(size=len(soluions)) |
| 36 | + |
| 37 | + if fitness_batch_size in [1, None]: |
| 38 | + fitness_func = fitness_func_single |
| 39 | + else: |
| 40 | + fitness_func = fitness_func_batch |
| 41 | + |
| 42 | + ga_instance = pygad.GA(num_generations=num_generations, |
| 43 | + num_parents_mating=5, |
| 44 | + fitness_func=fitness_func, |
| 45 | + sol_per_pop=10, |
| 46 | + num_genes=num_genes, |
| 47 | + gene_space=gene_space, |
| 48 | + gene_type=gene_type, |
| 49 | + initial_population=initial_population, |
| 50 | + init_range_low=init_range_low, |
| 51 | + init_range_high=init_range_high, |
| 52 | + random_mutation_min_val=random_mutation_min_val, |
| 53 | + random_mutation_max_val=random_mutation_max_val, |
| 54 | + allow_duplicate_genes=True, |
| 55 | + mutation_by_replacement=mutation_by_replacement, |
| 56 | + save_solutions=True, |
| 57 | + mutation_probability=mutation_probability, |
| 58 | + mutation_type=mutation_type, |
| 59 | + suppress_warnings=True, |
| 60 | + fitness_batch_size=fitness_batch_size, |
| 61 | + random_seed=1) |
| 62 | + |
| 63 | + ga_instance.run() |
| 64 | + |
| 65 | + return None, ga_instance |
| 66 | + |
| 67 | +def test_adaptive_mutation(): |
| 68 | + result, ga_instance = output_adaptive_mutation() |
| 69 | + |
| 70 | + # assert result == True |
| 71 | + |
| 72 | +def test_adaptive_mutation_gene_space(): |
| 73 | + result, ga_instance = output_adaptive_mutation(gene_space=range(10)) |
| 74 | + |
| 75 | + # assert result == True |
| 76 | + |
| 77 | +def test_adaptive_mutation_int_gene_type(): |
| 78 | + result, ga_instance = output_adaptive_mutation(gene_type=int) |
| 79 | + |
| 80 | + # assert result == True |
| 81 | + |
| 82 | +def test_adaptive_mutation_gene_space_gene_type(): |
| 83 | + result, ga_instance = output_adaptive_mutation(gene_space={"low": 0, "high": 10}, |
| 84 | + gene_type=[float, 2]) |
| 85 | + |
| 86 | + # assert result == True |
| 87 | + |
| 88 | +def test_adaptive_mutation_nested_gene_space(): |
| 89 | + result, ga_instance = output_adaptive_mutation(gene_space=[[0, 1, 2, 3, 4], |
| 90 | + numpy.arange(5, 10), |
| 91 | + range(10, 15), |
| 92 | + {"low": 15, "high": 20}, |
| 93 | + {"low": 20, "high": 30, "step": 2}, |
| 94 | + None, |
| 95 | + numpy.arange(30, 35), |
| 96 | + numpy.arange(35, 40), |
| 97 | + numpy.arange(40, 45), |
| 98 | + [45, 46, 47, 48, 49]]) |
| 99 | + # assert result == True |
| 100 | + |
| 101 | +def test_adaptive_mutation_nested_gene_type(): |
| 102 | + 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]]) |
| 103 | + |
| 104 | + # assert result == True |
| 105 | + |
| 106 | +def test_adaptive_mutation_nested_gene_space_nested_gene_type(): |
| 107 | + result, ga_instance = output_adaptive_mutation(gene_space=[[0, 1, 2, 3, 4], |
| 108 | + numpy.arange(5, 10), |
| 109 | + range(10, 15), |
| 110 | + {"low": 15, "high": 20}, |
| 111 | + {"low": 20, "high": 30, "step": 2}, |
| 112 | + None, |
| 113 | + numpy.arange(30, 35), |
| 114 | + numpy.arange(35, 40), |
| 115 | + numpy.arange(40, 45), |
| 116 | + [45, 46, 47, 48, 49]], |
| 117 | + gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) |
| 118 | + |
| 119 | + # assert result == True |
| 120 | + |
| 121 | +def test_adaptive_mutation_initial_population(): |
| 122 | + global initial_population |
| 123 | + result, ga_instance = output_adaptive_mutation(initial_population=initial_population) |
| 124 | + |
| 125 | + # assert result == True |
| 126 | + |
| 127 | +def test_adaptive_mutation_initial_population_nested_gene_type(): |
| 128 | + global initial_population |
| 129 | + result, ga_instance = output_adaptive_mutation(initial_population=initial_population, |
| 130 | + gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]]) |
| 131 | + |
| 132 | + # assert result == True |
| 133 | + |
| 134 | +def test_adaptive_mutation_fitness_batch_size_1(): |
| 135 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=1) |
| 136 | + |
| 137 | +def test_adaptive_mutation_fitness_batch_size_2(): |
| 138 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=2) |
| 139 | + |
| 140 | +def test_adaptive_mutation_fitness_batch_size_3(): |
| 141 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=3) |
| 142 | + |
| 143 | +def test_adaptive_mutation_fitness_batch_size_4(): |
| 144 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=4) |
| 145 | + |
| 146 | +def test_adaptive_mutation_fitness_batch_size_5(): |
| 147 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=5) |
| 148 | + |
| 149 | +def test_adaptive_mutation_fitness_batch_size_6(): |
| 150 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=6) |
| 151 | + |
| 152 | +def test_adaptive_mutation_fitness_batch_size_7(): |
| 153 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=7) |
| 154 | + |
| 155 | +def test_adaptive_mutation_fitness_batch_size_8(): |
| 156 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=8) |
| 157 | + |
| 158 | +def test_adaptive_mutation_fitness_batch_size_9(): |
| 159 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=9) |
| 160 | + |
| 161 | +def test_adaptive_mutation_fitness_batch_size_10(): |
| 162 | + result, ga_instance = output_adaptive_mutation(fitness_batch_size=10) |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + print() |
| 166 | + test_adaptive_mutation() |
| 167 | + print() |
| 168 | + |
| 169 | + test_adaptive_mutation_int_gene_type() |
| 170 | + print() |
| 171 | + |
| 172 | + test_adaptive_mutation_gene_space() |
| 173 | + print() |
| 174 | + |
| 175 | + test_adaptive_mutation_gene_space_gene_type() |
| 176 | + print() |
| 177 | + |
| 178 | + test_adaptive_mutation_nested_gene_space() |
| 179 | + print() |
| 180 | + |
| 181 | + test_adaptive_mutation_nested_gene_type() |
| 182 | + print() |
| 183 | + |
| 184 | + test_adaptive_mutation_initial_population() |
| 185 | + print() |
| 186 | + |
| 187 | + test_adaptive_mutation_initial_population_nested_gene_type() |
| 188 | + print() |
| 189 | + |
| 190 | + test_adaptive_mutation_fitness_batch_size_1() |
| 191 | + print() |
| 192 | + |
| 193 | + test_adaptive_mutation_fitness_batch_size_1() |
| 194 | + print() |
| 195 | + |
| 196 | + test_adaptive_mutation_fitness_batch_size_2() |
| 197 | + print() |
| 198 | + |
| 199 | + test_adaptive_mutation_fitness_batch_size_3() |
| 200 | + print() |
| 201 | + |
| 202 | + test_adaptive_mutation_fitness_batch_size_4() |
| 203 | + print() |
| 204 | + |
| 205 | + test_adaptive_mutation_fitness_batch_size_5() |
| 206 | + print() |
| 207 | + |
| 208 | + test_adaptive_mutation_fitness_batch_size_6() |
| 209 | + print() |
| 210 | + |
| 211 | + test_adaptive_mutation_fitness_batch_size_7() |
| 212 | + print() |
| 213 | + |
| 214 | + test_adaptive_mutation_fitness_batch_size_8() |
| 215 | + print() |
| 216 | + |
| 217 | + test_adaptive_mutation_fitness_batch_size_9() |
| 218 | + print() |
| 219 | + |
| 220 | + test_adaptive_mutation_fitness_batch_size_10() |
| 221 | + print() |
| 222 | + |
0 commit comments