ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.79k stars 451 forks source link

Get number of fitness evaluation #202

Closed yasirroni closed 1 year ago

yasirroni commented 1 year ago

In comparing various algorithm performance, I'm more interested in number of fitness evaluation compared to number of generation, due to heavy or costly fitness function used.

Is it possible to plot best fitness vs number of fitness evaluation? Function to use on_stop based on number of evaluation also would be nice.

If this feature is not yet supported, can you point me to location that I can update and make PR? Thanks.

ahmedfgad commented 1 year ago

The pygad.visualize.Plot class has all visualization methods in PyGAD.

There is already a method to plot the best fitness vs the generation number (i.e. it shows the best fitness across each generation).

yasirroni commented 1 year ago

@ahmedfgad No, not best fitness vs the generation number. I want best fitness vs number calling fitness function. Currently, my workaround is to make fitness function call a class that track how many that class is called. Thanks.

This use case is interesting when calling fitness function actually cost lots of time or even money.

ahmedfgad commented 1 year ago

I usually do this by simply creating a counter variable outside the fitness function and increase it for each call.

c = 0
def fitness_func(...):
    ...
    c += 1
yasirroni commented 1 year ago

That was neat and cleaver Python workaround. Maybe we can close this.

If anyone interested, maybe we can add that to the default caller that will call fitness function and also support native plot for fitness value vs number of fitness evaluation? But that's not priority for now. Thanks.