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

Commitdd7a670

Browse files
authored
Add files via upload
1 parentd73195b commitdd7a670

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

‎docs/source/README_pygad_ReadTheDocs.rst

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,66 +1665,38 @@ which optimizes a linear model. Its complete code is listed below.
16651665
desired_output=44# Function output.
16661666
16671667
deffitness_func(solution,solution_idx):
1668-
# Calculating the fitness value of each solution in the current population.
1669-
# The fitness function calulates the sum of products between each input and its corresponding weight.
16701668
output= numpy.sum(solution*function_inputs)
1671-
fitness=1.0/ numpy.abs(output- desired_output)
1669+
fitness=1.0/(numpy.abs(output- desired_output)+0.000001)
16721670
return fitness
16731671
1674-
fitness_function= fitness_func
1675-
1676-
num_generations=50# Number of generations.
1677-
num_parents_mating=4# Number of solutions to be selected as parents in the mating pool.
1672+
num_generations=100# Number of generations.
1673+
num_parents_mating=10# Number of solutions to be selected as parents in the mating pool.
16781674
1679-
# To prepare the initial population, there are 2 ways:
1680-
# 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.
1681-
# 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.
1682-
sol_per_pop=8# Number of solutions in the population.
1675+
sol_per_pop=20# Number of solutions in the population.
16831676
num_genes=len(function_inputs)
16841677
1685-
init_range_low=-2
1686-
init_range_high=5
1687-
1688-
parent_selection_type="sss"# Type of parent selection.
1689-
keep_parents=1# Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
1690-
1691-
crossover_type="single_point"# Type of the crossover operator.
1692-
1693-
# Parameters of the mutation operation.
1694-
mutation_type="random"# Type of the mutation operator.
1695-
mutation_percent_genes=10# Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
1696-
16971678
last_fitness=0
1698-
defcallback_generation(ga_instance):
1679+
defon_generation(ga_instance):
16991680
global last_fitness
17001681
print("Generation ={generation}".format(generation=ga_instance.generations_completed))
1701-
print("Fitness ={fitness}".format(fitness=ga_instance.best_solution()[1]))
1702-
print("Change ={change}".format(change=ga_instance.best_solution()[1]- last_fitness))
1703-
last_fitness= ga_instance.best_solution()[1]
1682+
print("Fitness ={fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
1683+
print("Change ={change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]- last_fitness))
1684+
last_fitness= ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]
17041685
1705-
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
17061686
ga_instance= pygad.GA(num_generations=num_generations,
1707-
num_parents_mating=num_parents_mating,
1708-
fitness_func=fitness_function,
1709-
sol_per_pop=sol_per_pop,
1687+
num_parents_mating=num_parents_mating,
1688+
sol_per_pop=sol_per_pop,
17101689
num_genes=num_genes,
1711-
init_range_low=init_range_low,
1712-
init_range_high=init_range_high,
1713-
parent_selection_type=parent_selection_type,
1714-
keep_parents=keep_parents,
1715-
crossover_type=crossover_type,
1716-
mutation_type=mutation_type,
1717-
mutation_percent_genes=mutation_percent_genes,
1718-
on_generation=callback_generation)
1690+
fitness_func=fitness_func,
1691+
on_generation=on_generation)
17191692
17201693
# Running the GA to optimize the parameters of the function.
17211694
ga_instance.run()
17221695
1723-
# After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations.
17241696
ga_instance.plot_result()
17251697
17261698
# Returning the details of the best solution.
1727-
solution, solution_fitness, solution_idx= ga_instance.best_solution()
1699+
solution, solution_fitness, solution_idx= ga_instance.best_solution(ga_instance.last_generation_fitness)
17281700
print("Parameters of the best solution :{solution}".format(solution=solution))
17291701
print("Fitness value of the best solution ={solution_fitness}".format(solution_fitness=solution_fitness))
17301702
print("Index of the best solution :{solution_idx}".format(solution_idx=solution_idx))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp