Beispiel #1
0
        private void CrossPixels(ref BitArray firstParent, ref BitArray secondParent)
        {
            var firstChild  = new BitArray(8);
            var secondChild = new BitArray(8);
            int onesCount   = new Random().Next(0, 8);
            var crossArray  = BitArrayGenerator.GenerateBitArray(8, onesCount);

            for (int i = 0; i < crossArray.Length; i++)
            {
                if (crossArray[i] == true)
                {
                    firstChild[i]  = firstParent[i];
                    secondChild[i] = secondParent[i];
                }
                else
                {
                    firstChild[i]  = secondParent[i];
                    secondChild[i] = firstParent[i];
                }
            }
            firstParent  = firstChild;
            secondParent = secondChild;
        }
        public override void Execute(PixelCollection pixelCollection, double mutationRate)
        {
            var generator = new Random();

            lock (pixelCollection)
            {
                foreach (var pixel in pixelCollection)
                {
                    if (generator.WillEventHappen(mutationRate / probabilityFixer))
                    {
                        var blueBits   = NumberConverter.ToBitArray(pixel.Blue);
                        var redBits    = NumberConverter.ToBitArray(pixel.Red);
                        var greenBits  = NumberConverter.ToBitArray(pixel.Green);
                        var randomBits = BitArrayGenerator.GenerateBitArray(blueBits.Count, onesCount: 4);
                        blueBits    = blueBits.Xor(randomBits);
                        redBits     = redBits.Xor(randomBits);
                        greenBits   = greenBits.Xor(randomBits);
                        pixel.Blue  = NumberConverter.ToInt(blueBits);
                        pixel.Red   = NumberConverter.ToInt(redBits);
                        pixel.Green = NumberConverter.ToInt(greenBits);
                    }
                }
            }
        }