|
1 | 1 | import pygad
|
2 | 2 | import numpy
|
3 | 3 |
|
4 |
| -num_generations = 50 # Number of generations. |
5 |
| -sol_per_pop = 8 # Number of solutions in the population. |
6 |
| -num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool. |
7 |
| - |
8 |
| -# Parameters of the mutation operation. |
9 |
| -mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists. |
10 |
| -mutation_num_genes = None # Number of genes to mutate. If the parameter mutation_num_genes exists, then no need for the parameter mutation_percent_genes. |
11 |
| - |
12 |
| -parent_selection_type = "tournament" # Type of parent selection. |
13 |
| - |
14 |
| -crossover_type = "two_points" # Type of the crossover operator. |
15 |
| - |
16 |
| -mutation_type = "scramble" # Type of the mutation operator. |
17 |
| - |
18 |
| -keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing. |
19 |
| - |
20 |
| -init_range_low = -2 |
21 |
| -init_range_high = 5 |
22 |
| - |
23 | 4 | """
|
24 | 5 | Given the following function:
|
25 | 6 | y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6
|
|
30 | 11 | function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
|
31 | 12 | desired_output = 44 # Function output.
|
32 | 13 |
|
33 |
| -num_genes = len(function_inputs) |
34 |
| - |
35 |
| -def fitness_func(solution): |
| 14 | +def fitness_func(solution, solution_idx): |
36 | 15 | # Calculating the fitness value of each solution in the current population.
|
37 | 16 | # The fitness function calulates the sum of products between each input and its corresponding weight.
|
38 | 17 | output = numpy.sum(solution*function_inputs)
|
39 | 18 | fitness = 1.0 / numpy.abs(output - desired_output)
|
40 | 19 | return fitness
|
41 | 20 |
|
| 21 | +fitness_function = fitness_func |
| 22 | + |
| 23 | +num_generations = 50 # Number of generations. |
| 24 | +num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool. |
| 25 | + |
| 26 | +sol_per_pop = 8 # Number of solutions in the population. |
| 27 | +num_genes = len(function_inputs) |
| 28 | + |
| 29 | +init_range_low = -2 |
| 30 | +init_range_high = 5 |
| 31 | + |
| 32 | + |
| 33 | +parent_selection_type = "sss" # Type of parent selection. |
| 34 | +keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing. |
| 35 | + |
| 36 | +crossover_type = "single_point" # Type of the crossover operator. |
| 37 | + |
| 38 | +# Parameters of the mutation operation. |
| 39 | +mutation_type = "random" # Type of the mutation operator. |
| 40 | +mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists. |
| 41 | + |
| 42 | +def callback_generation(ga_instance): |
| 43 | + print("Generation :", ga_instance.generations_completed) |
| 44 | + print("Fitness of the best solution :", ga_instance.best_solution()[1]) |
| 45 | + |
42 | 46 | # Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
|
43 |
| -ga_instance = pygad.GA(num_generations=num_generations, |
44 |
| - sol_per_pop=sol_per_pop, |
45 |
| - num_parents_mating=num_parents_mating, |
46 |
| - num_genes=num_genes, |
47 |
| - fitness_func=fitness_func, |
48 |
| - mutation_percent_genes=mutation_percent_genes, |
49 |
| - mutation_num_genes=mutation_num_genes, |
50 |
| - init_range_low=init_range_low, |
51 |
| - init_range_high=init_range_high, |
52 |
| - parent_selection_type=parent_selection_type, |
53 |
| - crossover_type=crossover_type, |
A61D
54 |
| - mutation_type=mutation_type, |
55 |
| - keep_parents=keep_parents, |
56 |
| - K_tournament=3) |
| 47 | +ga_instance = pygad.GA(num_generations=num_generations, |
| 48 | + num_parents_mating=num_parents_mating, |
| 49 | + fitness_func=fitness_function, |
| 50 | + sol_per_pop=sol_per_pop, |
| 51 | + num_genes=num_genes, |
| 52 | + init_range_low=init_range_low, |
| 53 | + init_range_high=init_range_high, |
| 54 | + parent_selection_type=parent_selection_type, |
| 55 | + keep_parents=keep_parents, |
| 56 | + crossover_type=crossover_type, |
| 57 | + mutation_type=mutation_type, |
| 58 | + mutation_percent_genes=mutation_percent_genes, |
| 59 | + callback_generation=callback_generation) |
57 | 60 |
|
58 | 61 | # Running the GA to optimize the parameters of the function.
|
59 | 62 | ga_instance.run()
|
|
0 commit comments