Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd73195b

Browse files
authored
Simplifying the example
Removing some comments and variables
1 parent8b82787 commitd73195b

File tree

1 file changed

+4
-28
lines changed

1 file changed

+4
-28
lines changed

‎example.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,38 @@
1212
desired_output=44# Function output.
1313

1414
deffitness_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.
1715
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.
1916
fitness=1.0/ (numpy.abs(output-desired_output)+0.000001)
2017
returnfitness
2118

22-
fitness_function=fitness_func
23-
2419
num_generations=100# Number of generations.
2520
num_parents_mating=10# Number of solutions to be selected as parents in the mating pool.
2621

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.
3022
sol_per_pop=20# Number of solutions in the population.
3123
num_genes=len(function_inputs)
3224

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-
4225
last_fitness=0
43-
defcallback_generation(ga_instance):
26+
defon_generation(ga_instance):
4427
globallast_fitness
4528
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
4629
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
4730
print("Change = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]-last_fitness))
4831
last_fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]
4932

50-
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
5133
ga_instance=pygad.GA(num_generations=num_generations,
5234
num_parents_mating=num_parents_mating,
53-
fitness_func=fitness_function,
5435
sol_per_pop=sol_per_pop,
5536
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)
6239

6340
# Running the GA to optimize the parameters of the function.
6441
ga_instance.run()
6542

66-
# After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations.
6743
ga_instance.plot_result()
6844

6945
# 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)
7147
print("Parameters of the best solution : {solution}".format(solution=solution))
7248
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
7349
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp