|
| 1 | +import pygad |
| 2 | +import numpy |
| 3 | +import random |
| 4 | + |
| 5 | +# Global constants for testing |
| 6 | +num_generations = 5 |
| 7 | +num_parents_mating = 4 |
| 8 | +sol_per_pop = 10 |
| 9 | +num_genes = 10 |
| 10 | +random_seed = 42 |
| 11 | + |
| 12 | +def fitness_func(ga_instance, solution, solution_idx): |
| 13 | + return numpy.sum(solution) |
| 14 | + |
| 15 | +def fitness_func_multi(ga_instance, solution, solution_idx): |
| 16 | + return [numpy.sum(solution), numpy.sum(solution**2)] |
| 17 | + |
| 18 | +def run_ga_with_params(parent_selection_type='sss', crossover_type='single_point', mutation_type='random', multi_objective=False): |
| 19 | + if multi_objective: |
| 20 | + f = fitness_func_multi |
| 21 | + else: |
| 22 | + f = fitness_func |
| 23 | + |
| 24 | + ga_instance = pygad.GA(num_generations=num_generations, |
| 25 | + num_parents_mating=num_parents_mating, |
| 26 | + fitness_func=f, |
| 27 | + sol_per_pop=sol_per_pop, |
| 28 | + num_genes=num_genes, |
| 29 | + parent_selection_type=parent_selection_type, |
| 30 | + crossover_type=crossover_type, |
| 31 | + mutation_type=mutation_type, |
| 32 | + random_seed=random_seed, |
| 33 | + suppress_warnings=True) |
| 34 | + ga_instance.run() |
| 35 | + return ga_instance |
| 36 | + |
| 37 | +def test_selection_operators(): |
| 38 | + operators = ['sss', 'rws', 'sus', 'rank', 'random', 'tournament'] |
| 39 | + for op in operators: |
| 40 | + ga = run_ga_with_params(parent_selection_type=op) |
| 41 | + # Verify parents were selected |
| 42 | + assert ga.last_generation_parents.shape == (num_parents_mating, num_genes) |
| 43 | + print(f"Selection operator '{op}' passed.") |
| 44 | + |
| 45 | +def test_crossover_operators(): |
| 46 | + operators = ['single_point', 'two_points', 'uniform', 'scattered'] |
| 47 | + for op in operators: |
| 48 | + ga = run_ga_with_params(crossover_type=op) |
| 49 | + # Verify population shape |
| 50 | + assert ga.population.shape == (sol_per_pop, num_genes) |
| 51 | + print(f"Crossover operator '{op}' passed.") |
| 52 | + |
| 53 | +def test_mutation_operators(): |
| 54 | + operators = ['random', 'swap', 'inversion', 'scramble'] |
| 55 | + for op in operators: |
| 56 | + ga = run_ga_with_params(mutation_type=op) |
| 57 | + # Verify population shape |
| 58 | + assert ga.population.shape == (sol_per_pop, num_genes) |
| 59 | + print(f"Mutation operator '{op}' passed.") |
| 60 | + |
| 61 | +def test_multi_objective_selection(): |
| 62 | + # NSGA-II is usually used for multi-objective |
| 63 | + ga = run_ga_with_params(parent_selection_type='nsga2', multi_objective=True) |
| 64 | + assert ga.last_generation_parents.shape == (num_parents_mating, num_genes) |
| 65 | + print("Multi-objective selection (nsga2) passed.") |
| 66 | + |
| 67 | + # Tournament NSGA-II |
| 68 | + ga = run_ga_with_params(parent_selection_type='tournament_nsga2', multi_objective=True) |
| 69 | + assert ga.last_generation_parents.shape == (num_parents_mating, num_genes) |
| 70 | + print("Multi-objective selection (tournament_nsga2) passed.") |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + test_selection_operators() |
| 74 | + test_crossover_operators() |
| 75 | + test_mutation_operators() |
| 76 | + test_multi_objective_selection() |
| 77 | + print("\nAll operator tests passed!") |
0 commit comments