You want to implement a genetic algorithm. Your implementation should be such that it works for any common minimization (or maximization) problem, not just the Rastrigin function. You may decide to implement binary encoded GA or real encoded GA. Both have their own applications and niche applications. But for you, I would suggest implementing Real-encoded GA. According to your question regarding what to do if the generated variable values are outside of [-5.12: 5.12], the real encoded GA and binary encoded GA will handle them differently.
Having source code is always good before you start implementing your own version. If you are looking for a C implementation, the source lab section has a Real Coded GA implementation that is widely used by us and others for our search work. I suggest you play with it and try some of the simple optimization problems posed there.
Pyevolve is a Python library for genetic algorithms and genetic programming.
Now that we talked about implementation materials, is your understanding of GA clear? If not, refer to this tutorial , which introduces GA from an optimization point of view. Please note that the explanation of crossover and mutation for binary-coded GA is not automatically transferred to the real coded GA. Real coded GA has its own subtleties that you will need to read some documents and understand them. Take your time, but with full load you should be able to easily get there.
user59634
source share