Skip to content

Commit bec8b70

Browse files
authored
Update example.py
1 parent 5748cdf commit bec8b70

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

example.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ def fitness_func(solution, solution_idx):
2323
num_generations = 50 # Number of generations.
2424
num_parents_mating = 4 # Number of solutions to be selected as parents in the mating pool.
2525

26+
# To prepare the initial population, there are 2 ways:
27+
# 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.
28+
# 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.
2629
sol_per_pop = 8 # Number of solutions in the population.
2730
num_genes = len(function_inputs)
2831

2932
init_range_low = -2
3033
init_range_high = 5
3134

32-
3335
parent_selection_type = "sss" # Type of parent selection.
3436
keep_parents = 1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
3537

@@ -39,9 +41,12 @@ def fitness_func(solution, solution_idx):
3941
mutation_type = "random" # Type of the mutation operator.
4042
mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
4143

44+
last_fitness = 0
4245
def callback_generation(ga_instance):
43-
print("Generation :", ga_instance.generations_completed)
44-
print("Fitness of the best solution :", ga_instance.best_solution()[1])
46+
global last_fitness
47+
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
48+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
49+
print("Change = {change}".format(change=ga_instance.best_solution()[1] - last_fitness))
4550

4651
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
4752
ga_instance = pygad.GA(num_generations=num_generations,
@@ -65,16 +70,17 @@ def callback_generation(ga_instance):
6570
ga_instance.plot_result()
6671

6772
# Returning the details of the best solution.
68-
best_solution, best_solution_fitness = ga_instance.best_solution()
69-
print("Parameters of the best solution :", best_solution)
70-
print("Fitness value of the best solution :", best_solution_fitness, "\n")
73+
solution, solution_fitness, solution_idx = ga_instance.best_solution()
74+
print("Parameters of the best solution : {solution}".format(solution=solution))
75+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
76+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
77+
78+
print("Best solution reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))
7179

7280
# Saving the GA instance.
7381
filename = 'genetic' # The filename to which the instance is saved. The name is without extension.
7482
ga_instance.save(filename=filename)
7583

7684
# Loading the saved GA instance.
7785
loaded_ga_instance = pygad.load(filename=filename)
78-
print("The saved instance of the genetic algorithm is loaded successfully.")
7986
loaded_ga_instance.plot_result()
80-
print(loaded_ga_instance.best_solution())

0 commit comments

Comments
 (0)