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

Caculating the total number of fitness function calls#162

Answeredbyahmedfgad
TiMo3654 asked this question inQ&A
Discussion options

Based on my understanding so far, one can calculate the number of total fitness function calls by multiplying the number of solutions per population with the number of generations plus the function calls for an initial population.

So with a sol_per_pop = 50 and num_generations = 20, I would expect 50 + 21 *49 = 1079 fitness function calls in total.

But by printing a message every time the fitness function is called, I noticed that the total count amounts to 2059 function calls.

In another case, the object returned 8 completed generations and the cost function was called 932 times not my expected 491 times.

My implementation is like this:

num_parents_mating      = 4sol_per_pop             = 50num_genes               = 10init_range_low          = 0init_range_high         = 1parent_selection_type   = "sss"keep_parents            = 1crossover_type          = "single_point"mutation_type           = "random"mutation_percent_genes  = 10gene_space              = {'low' :  0, 'high':   1}def check_for_termination(ga_instance):       if ga_instance.best_solution()[1] == np.float64(-0.0):        return "stop"def genetic_optimizer(fun, num_generations):    ga_instance = pygad.GA(num_generations=num_generations,                       num_parents_mating=num_parents_mating,                       fitness_func=fun,                       sol_per_pop=sol_per_pop,                       num_genes=num_genes,                       init_range_low=init_range_low,                       init_range_high=init_range_high,                       parent_selection_type=parent_selection_type,                       keep_parents=keep_parents,                       crossover_type=crossover_type,                       mutation_type=mutation_type,                       mutation_percent_genes=mutation_percent_genes,                       gene_space = gene_space,                       callback_generation= check_for_termination)        ga_instance.run()    solution, solution_fitness, solution_idx = ga_instance.best_solution()    num_completed_generations = ga_instance.generations_completed    print('Completed Generation: ' + str(num_completed_generations))    return solution_fitness, solution, num_completed_generations

Any help, on how to calculate the total number of function calls is greatly appreciated or why I might see different call numbers than exspected.

You must be logged in to vote

Hi@TiMo3654,

The reason for doubling the number of calls to the fitness function is calling thebest_solution() method in the generation callback function.

defcheck_for_termination(ga_instance):ifga_instance.best_solution()[1]==np.float64(-0.0):return"stop"

When this method is called without passing thepop_fitness parameter, then it works as by calculating the fitness of the current population by calling the fitness function.

As you have 50 solutions, then one call to thebest_solution() method makes 50 calls to the fitness function. For 20 generations, then it makes50*20 calls. So, the overall total number of calls is50*20 + 50*20 + 50 = 2050.

In PyGAD, the fitn…

Replies: 1 comment

Comment options

Hi@TiMo3654,

The reason for doubling the number of calls to the fitness function is calling thebest_solution() method in the generation callback function.

defcheck_for_termination(ga_instance):ifga_instance.best_solution()[1]==np.float64(-0.0):return"stop"

When this method is called without passing thepop_fitness parameter, then it works as by calculating the fitness of the current population by calling the fitness function.

As you have 50 solutions, then one call to thebest_solution() method makes 50 calls to the fitness function. For 20 generations, then it makes50*20 calls. So, the overall total number of calls is50*20 + 50*20 + 50 = 2050.

In PyGAD, the fitness of the current population is already calculated and saved in thelast_generation_fitness instance attribute. You can pass it to thepop_fitness parameter. This is the new callback function.

defcheck_for_termination(ga_instance):ifga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]==np.float64(-0.0):return"stop"

Hope this helps. Please let me know if you have any other questions.

You must be logged in to vote
0 replies
Answer selected byTiMo3654
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
questionFurther information is requested
2 participants
@TiMo3654@ahmedfgad

[8]ページ先頭

©2009-2025 Movatter.jp