The intersection of two integers

I am currently trying to implement a very simple example of genetic algorithms.

At some point, you need to do a β€œCross-Over” (biology) with two numbers (parents) to get a β€œbaby”.

Here you can find the Cross-Over explanation:

As a "crossover" two lines (1234 and abcd β†’ 12cd and ab34)

(Second illustration, the simpler one-point Cross-Over is the one I'm trying to do.)

Chromosomes (parents and baby) are numbers, but cross-linking will work a little.

I found a solution for one of the "chromosomes", which is as follows:

  • Move bit X to the right ( >>> )
  • and then again move the bit to position X, but this time to the left ( << )

Thus, this will save the end of one of the chromosomes and start the beginning with 0s.

But I really do not know how to solve the problem of another chromosome, and then make a crossroads.

(Probably, XOR once I saved the beginning / end of chromosomes and filled the remaining 0s.)

Or should I even approach this problem from a different angle?

+7
source share
2 answers

If the fraction of the cross is p (e.g. p = .25) then this should work:

 mask1 = ((0xffff >> 16*p) << 16*p) mask2 = 0xffff ^ mask1 output1 = (input1 & mask1) ^ (input2 & mask2) output2 = (input1 & mask2) ^ (input2 & mask1) 

A few notes:

  • This is just pseudo code. You may need some videos.
  • This refers to p differently than you relate to it in your comment above. (Just replace p with 1-p to go to your definition of p.)
+4
source

The naive approach is to use 4 local variables:

 int chromatid1Start; int chromatid1End; int chromatid2Start; int chromatid2End; 

Then assign incoming chromatid1 first two variables and chromatid2 last two variables.

 chromatid1Start = chromatid1; chromatid1End = chromatid1; chromatid2Start = chromatid2; chromatid2End = chromatid2; 

Perform a right shift on the Start chromatid variables to the crossover point, then shift the same amount to the left. On chromatid variables End , moving left to the crossover point, then shift the same amount to the right.

 chromatid1Start = (chromatid1Start >> 16 * crossoverPercent) << 16 * crossoverPercent; chromatid1End = (chromatid1End << 16 * (1 - crossoverPercent)) >> 16 * (1 - crossoverPercent); chromatid2Start = (chromatid2Start >> 16 * crossoverPercent) << 16 * crossoverPercent; chromatid2End = (chromatid2End << 16 * (1 - crossoverPercent)) >> 16 * (1 - crossoverPercent); 

With this, you can go to the beginning of one from the end of the other:

 int daughterChromatid1 = chromatid1Start + chromatid2End; int daughterChromatid2 = chromatid2Start + chromatid1End; 
+1
source

All Articles