|
7 | 7 | What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function.
|
8 | 8 | """
|
9 | 9 |
|
10 |
| -# Equation inputs. |
11 | 10 | function_inputs = [4,-2,3.5,5,-11,-4.7]
|
12 |
| -# Equation output. |
13 |
| -function_output = 44 |
| 11 | +function_output = 44 # Function output. |
14 | 12 |
|
| 13 | +num_generations = 50 # Number of generations. |
15 | 14 | sol_per_pop = 8 # Number of solutions in the population.
|
16 | 15 | num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool.
|
17 |
| -num_generations = 50 # Number of generations. |
18 | 16 |
|
19 | 17 | # Parameters of the mutation operation.
|
20 |
| -mutation_percent_genes=10 # Percentage of genes to mutate. |
| 18 | +mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists. |
| 19 | +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. |
| 20 | + |
| 21 | +parent_selection_type = "tournament" # Type of parent selection. |
| 22 | + |
| 23 | +crossover_type = "two_points" # Type of the crossover operator. |
| 24 | + |
| 25 | +mutation_type = "scramble" # Type of the mutation operator. |
| 26 | + |
| 27 | +keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing. |
21 | 28 |
|
22 | 29 | # Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
|
23 | 30 | ga_instance = ga.GA(num_generations=num_generations,
|
24 | 31 | sol_per_pop=sol_per_pop,
|
25 | 32 | num_parents_mating=num_parents_mating,
|
26 | 33 | function_inputs=function_inputs,
|
27 | 34 | function_output=function_output,
|
28 |
| - mutation_percent_genes=10) |
| 35 | + mutation_percent_genes=mutation_percent_genes, |
| 36 | + mutation_num_genes=mutation_num_genes, |
| 37 | + parent_selection_type=parent_selection_type, |
| 38 | + crossover_type=crossover_type, |
| 39 | + mutation_type=mutation_type, |
| 40 | + keep_parents=keep_parents, |
| 41 | + K_tournament=3) |
29 | 42 |
|
30 |
| -# Training the GA to optimize the parameters of the function. |
31 |
| -ga_instance.train() |
| 43 | +# Running the GA to optimize the parameters of the function. |
| 44 | +ga_instance.run() |
32 | 45 |
|
33 | 46 | # After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations.
|
34 | 47 | ga_instance.plot_result()
|
35 | 48 |
|
36 | 49 | # Returning the details of the best solution.
|
37 | 50 | best_solution, best_solution_fitness = ga_instance.best_solution()
|
38 | 51 | print("Parameters of the best solution :", best_solution)
|
39 |
| -print("Fitness value of the best solution :", best_solution_fitness) |
| 52 | +print("Fitness value of the best solution :", best_solution_fitness, "\n") |
| 53 | + |
| 54 | +# Saving the GA instance. |
| 55 | +filename = 'genetic' # The filename to which the instance is saved. The name is without extension. |
| 56 | +ga_instance.save(filename=filename) |
| 57 | + |
| 58 | +# Loading the saved GA instance. |
| 59 | +loaded_ga_instance = ga.load(filename=filename) |
| 60 | +loaded_ga_instance.plot_result() |
| 61 | +print(loaded_ga_instance.best_solution()) |
0 commit comments