I'm trying to find out square root of a float number using genetic algorithm.
I have initialized population of random numbers and a fitness function. How do I implement selection of parents from population and uniform crossover?
1 Answer
SELECTION
The function given by gilad (the one you are using) seems fine. Why not just follow a standard procedure ? You can find some ideas on wikipedia
CROSSOVER
If you are considering a candidate as a 32 bit vector (31 actually), then doing a uniform crossover consist in choosing bits of either parent with half probability.
The idea will be :
- Toss a coin
- If head take on parent one,
- If tails take on parent two
Programmatically an efficient way of creating a child from 2 parents will be to generate a random 32 bit number r, and given parents a and b do :
child = (r & a) | (~r & b);
2 Comments
Vatine
Hm, I would probably have gone with
| rather than +, if I am bit-fiddling, I want to be as clear as possible (and while bit-wise or and addition produces the same result in this specific case, bit-wise or is a bit clearer).Emperor Orionii
In C++ "~" is used for bitwise complement, not sure for C. "!", a logical negation might give wrong result, 0 for non zero int.
num - (candidate * candidate)where you search for the minimal value.