Beispiel #1
0
        public IEvolvable Crossover(IEvolvable other)
        {
            StencilSpeciesArr mate = (StencilSpeciesArr)other;

            if (this.Equals(mate))
            {
                var result = this.Clone();
                result.Mutate();
                return(result);
            }
            else
            {
                int[] cPerProcessor = (int[])cellsPerProcessor.Clone();
                int   freeCells     = this.Field.Length;

                // initialize field with -1 or cells that are equal in both mates
                Arr <int> field = new Arr <int>(this.Field.SizeX, this.Field.SizeY);
                freeCells -= fillWithEqualGenesOrMinusOne(field, this.Field, mate.Field, cPerProcessor);

                while (freeCells > 0)
                {
                    int position  = field.FreePosition(random.Next(0, freeCells), -1);
                    int processor = suitableValue(random, freeCells, this.Field, mate.Field, position, cPerProcessor);

                    field[position] = processor;
                    cPerProcessor[processor]--;
                    freeCells--;
                }

                StencilSpeciesArr result = new StencilSpeciesArr(this, field);
                result.Optimize(3);
                return(result);
            }
        }
        public void CrossoverFreePositionTest()
        {
            Random random = new Random(2014);

            Arr <int> field = new Arr <int>(new int[] { -1, -1, -1, -1, -1, -1 });

            for (int i = 0; i < 6; i++)
            {
                AssertEx.IsGreaterThanOrEqualTo(field.FreePosition(random.Next(0, 6 - i), -1), i);
                field[i] = 0;
            }

            field = new Arr <int>(new int[] { -1, -1, -1, -1, -1, -1 });
            for (int i = 0; i < 6; i++)
            {
                int position = field.FreePosition(random.Next(0, 6 - i), -1);
                field[position] = 0;
            }
            Assert.AreEqual(0, field.Sum);
        }