Example #1
0
        public override void Mutate(Random random, ExecutionEnvironment.Arr <int> field, int mutations)
        {
            ExecutionEnvironment.Arr <int> f = field.Clone();
            int validPositionCount           = 0;
            int pos = 0;

            for (int x = 0; x < field.W; x++)
            {
                for (int y = 0; y < field.H; y++)
                {
                    if (8 > f.SimilarNeighbors(x, y, IncludeFieldBorders))
                    {
                        f[pos] = -1;
                        validPositionCount++;
                    }
                    pos++;
                }
            }

            mutations = Math.Min(mutations, validPositionCount);
            for (int i = 0; i < mutations; i++)
            {
                field.Swap(f.FreePosition(random.Next(0, validPositionCount), -1), f.FreePosition(random.Next(0, validPositionCount), -1));
            }
        }
Example #2
0
        public override void Mutate(Random random, ExecutionEnvironment.Arr <int> field, int mutations)
        {
            for (int i = 0; i < mutations; i++)
            {
                ExecutionEnvironment.Arr <int> f = field.Clone();
                int validPositionCount           = 0;
                int pos = 0;
                for (int y = 0; y < field.H; y++)
                {
                    for (int x = 0; x < field.W; x++)
                    {
                        if (8 > f.SimilarNeighbors(x, y, IncludeFieldBorders))
                        {
                            f[pos] = -1;
                            validPositionCount++;
                        }
                        pos++;
                    }
                }

                int steps     = random.Next(1, validPositionCount);
                int position  = f.FreePosition(random.Next(0, validPositionCount), -1);
                int xPos      = position % field.W;
                int yPos      = position / field.W;
                int processor = f[position];
                for (int i2 = 0; i2 < steps; i2++)
                {
                    f[position] = -1;
                    int next = f.NextPosition(random, processor, -1, xPos, yPos);
                    if (next == -1)
                    {
                        break;
                    }
                    field.Swap(position, next);
                    position = next;
                }
            }
        }