1

As the title says, i want to use fitness scalling methods, such as sigma scalling, for i'm having problems with premature genetic conversion in non-optimal solutions.

I know that PyGAD has a on_fitness method that is called after the fitness values for all my individuals are calculated, and think i could use to solve my problem with it, but could not verify. Can i use it for this purpose?

Thanks for your input.

yna ponte
  • 41
  • 5

1 Answers1

1

Well I don't know if this is the best way, but here is how I solved my problem.

With the release of PyGAD 2.19, a new constructor option was added, the fitness_batch_size. As it's name sujests, you can now pass a batch of individuals at once to one execution of your fitness function, like so:

# with fitness_batch_size = None(or 1)

Solution = [2.184, 0,...,3.2819]

# with 1 < fitness_batch_size <= sol_per_pop
Solution = [[2.184, 0,...,3.2819], [1.234, 2.345,..., 5],...,[...]]

With this in mind, now to scale my fitness, I have to adjust fitness_batch_size = sol_per_pop and generate all fitness values at once. Afterwards, it's matter of passing all values through some kind of scaling and returning this result as my fitness values.

If you know a better way, please post it here. Thanks in advance.

yna ponte
  • 41
  • 5
  • 1
    This is the best solution. Using the `on_fitness()` function works too but it only costs an additional function call to edit the fitness values. – Ahmed Gad Apr 26 '23 at 03:47
  • So it can actually be done with the on_fitness() huh. I though it wasn't possible due to what i saw in exemples from PyGAD, where it only shows some print() as a result, didn't know you could also return something. Thank you. – yna ponte May 02 '23 at 14:28
  • 1
    `on_fitness()` does not allow you to return anything. But I mean you can change the latest population fitness there by editing the last_generation_fitness attribute. – Ahmed Gad May 03 '23 at 03:16
  • 1
    Oh, ok.Thank you for clarifying. Never thought about doing that. – yna ponte May 05 '23 at 11:52