|
12 | 12 | desired_output = 44 # Function output.
|
13 | 13 |
|
14 | 14 | def fitness_func(solution, solution_idx):
|
15 |
| - # Calculating the fitness value of each solution in the current population. |
16 |
| - # The fitness function calulates the sum of products between each input and its corresponding weight. |
17 | 15 | output = numpy.sum(solution*function_inputs)
|
18 |
| - # The value 0.000001 is used to avoid the Inf value when the denominator numpy.abs(output - desired_output) is 0.0. |
19 | 16 | fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
|
20 | 17 | return fitness
|
21 | 18 |
|
22 |
| -fitness_function = fitness_func |
23 |
| - |
24 | 19 | num_generations = 100 # Number of generations.
|
25 | 20 | num_parents_mating = 10 # Number of solutions to be selected as parents in the mating pool.
|
26 | 21 |
|
27 |
| -# To prepare the initial population, there are 2 ways: |
28 |
| -# 1) Prepare it yourself and pass it to the initial_population parameter. This way is useful when the user wants to start the genetic algorithm with a custom initial population. |
29 |
| -# 2) Assign valid integer values to the sol_per_pop and num_genes parameters. If the initial_population parameter exists, then the sol_per_pop and num_genes parameters are useless. |
30 | 22 | sol_per_pop = 20 # Number of solutions in the population.
|
31 | 23 | num_genes = len(function_inputs)
|
32 | 24 |
|
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 or when mutation_type is None. |
41 |
| - |
42 | 25 | last_fitness = 0
|
43 |
| -def callback_generation(ga_instance): |
| 26 | +def on_generation(ga_instance): |
44 | 27 | global last_fitness
|
45 | 28 | print("Generation = {generation}".format(generation=ga_instance.generations_completed))
|
46 | 29 | print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
|
47 | 30 | print("Change = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
|
48 | 31 | last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]
|
49 | 32 |
|
50 |
| -# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor. |
51 | 33 | ga_instance = pygad.GA(num_generations=num_generations,
|
52 | 34 | num_parents_mating=num_parents_mating,
|
53 |
| - fitness_func=fitness_function, |
54 | 35 | sol_per_pop=sol_per_pop,
|
55 | 36 | num_genes=num_genes,
|
56 |
| - parent_selection_type=parent_selection_type, |
57 |
| - keep_parents=keep_parents, |
58 |
| - crossover_type=crossover_type, |
59 |
| - mutation_type=mutation_type, |
60 |
| - mutation_percent_genes=mutation_percent_genes, |
61 |
| - on_generation=callback_generation) |
| 37 | + fitness_func=fitness_func, |
| 38 | + on_generation=on_generation) |
62 | 39 |
|
63 | 40 | # Running the GA to optimize the parameters of the function.
|
64 | 41 | ga_instance.run()
|
65 | 42 |
|
66 |
| -# After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations. |
67 | 43 | ga_instance.plot_result()
|
68 | 44 |
|
69 | 45 | # Returning the details of the best solution.
|
70 |
| -solution, solution_fitness, solution_idx = ga_instance.best_solution() |
| 46 | +solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness) |
71 | 47 | print("Parameters of the best solution : {solution}".format(solution=solution))
|
72 | 48 | print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
|
73 | 49 | print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
|
|
0 commit comments