-
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pygad | ||
import numpy | ||
|
||
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. | ||
desired_output = 44 # Function output. | ||
|
||
def fitness_func(ga_instance, solution, solution_idx): | ||
output = numpy.sum(solution*function_inputs) | ||
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) | ||
return fitness | ||
|
||
last_fitness = 0 | ||
def on_generation(ga_instance): | ||
global last_fitness | ||
print(f"Generation = {ga_instance.generations_completed}") | ||
print(f"Fitness = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]}") | ||
print(f"Change = {ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness}") | ||
last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] | ||
|
||
if __name__ == '__main__': | ||
ga_instance = pygad.GA(num_generations=100, | ||
num_parents_mating=10, | ||
sol_per_pop=20, | ||
num_genes=len(function_inputs), | ||
fitness_func=fitness_func, | ||
on_generation=on_generation, | ||
# parallel_processing=['process', 2], | ||
parallel_processing=['thread', 2] | ||
) | ||
|
||
# Running the GA to optimize the parameters of the function. | ||
ga_instance.run() | ||
|
||
# Returning the details of the best solution. | ||
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness) | ||
print(f"Parameters of the best solution : {solution}") | ||
print(f"Fitness value of the best solution = {solution_fitness}") | ||
print(f"Index of the best solution : {solution_idx}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters