7

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?

7
  • are you checking if the squares of parents are closer to the number you're trying to find the square root of ? Commented Oct 11, 2012 at 7:42
  • I'm figuring out how to select parents ? Those who have highest fitness values ? Commented Oct 11, 2012 at 7:48
  • higher fitness = higher probability to get selected. Commented Oct 11, 2012 at 7:48
  • It depends on your fitness function... Which one are you using? Commented Oct 11, 2012 at 7:53
  • natural implementation would be num - (candidate * candidate) where you search for the minimal value. Commented Oct 11, 2012 at 7:53

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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).
In C++ "~" is used for bitwise complement, not sure for C. "!", a logical negation might give wrong result, 0 for non zero int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.