Skip to content

Commit 76dd778

Browse files
authored
Saving and loading the genetic algorithm
An instance of the genetic algorithm is saved using the pickle library and then loaded to resotre its state.
1 parent 5b91eb1 commit 76dd778

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

example.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,55 @@
77
What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function.
88
"""
99

10-
# Equation inputs.
1110
function_inputs = [4,-2,3.5,5,-11,-4.7]
12-
# Equation output.
13-
function_output = 44
11+
function_output = 44 # Function output.
1412

13+
num_generations = 50 # Number of generations.
1514
sol_per_pop = 8 # Number of solutions in the population.
1615
num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool.
17-
num_generations = 50 # Number of generations.
1816

1917
# 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.
2128

2229
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
2330
ga_instance = ga.GA(num_generations=num_generations,
2431
sol_per_pop=sol_per_pop,
2532
num_parents_mating=num_parents_mating,
2633
function_inputs=function_inputs,
2734
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)
2942

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()
3245

3346
# After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations.
3447
ga_instance.plot_result()
3548

3649
# Returning the details of the best solution.
3750
best_solution, best_solution_fitness = ga_instance.best_solution()
3851
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

Comments
 (0)