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.89k stars 464 forks source link

solution_idx #77

Open ckattenberg opened 2 years ago

ckattenberg commented 2 years ago

Why does the fitness function need solution_idx as an input parameter if it doesn't do anything with it?

ahmedfgad commented 2 years ago

The solution_idx holds the solution index in its population.

One usage is updating the neural network corresponding to the solution: https://github.com/ahmedfgad/NeuralGenetic/blob/82e73d92dc1a32ad17f982cfa8230d6eb4d17321/example_XOR_classification.py#L9

It can be used for debugging purposes.

xshang93 commented 1 year ago

Just a quick follow up question, I wonder why it always gives me the solution_idx of 0?

ahmedfgad commented 1 year ago

@xshang93, most of the time the best solution is the first solution in the population. This is why the index 0 is returned almost frequently.

For some cases, it is something different. This only happens if a better solution is found in the last generation.

I advise you to run a code with few generations. You will likely find a different index than 0.