Exemplo n.º 1
0
        public void Crossover(ref TestCitizen mate, int slicePosA = -1, int slicePosB = -1)
        {
            Random rng = new Random();

            slicePosA = (slicePosA < 0) ? rng.Next(Genes.Count - 1) : slicePosA;
            slicePosB = (slicePosB < 0) ? rng.Next(mate.Genes.Count - 1) : slicePosB;

            // Determines if the input parameters are valid

            if (slicePosB >= mate.Genes.Count || slicePosA >= Genes.Count)
            {
                throw new InvalidOperationException("slice position is greater than the maximum index of either the mate, or the subject!");
            }

            // Initializes each cut List

            List <bool> cutA = new List <bool>(Genes.Count - (slicePosA + 1));
            List <bool> cutB = new List <bool>(mate.Genes.Count - (slicePosB + 1));

            // Removes the data from each subject at the splice Pos

            for (int i = 0; i < Genes.Count - (slicePosA + 1); i++)
            {
                cutA.Add(Genes[Genes.Count - 1]);
                Genes.RemoveAt(Genes.Count - 1);
            }

            cutA.Reverse();

            for (int i = 0; i < mate.Genes.Count - (slicePosB + 1); i++)
            {
                cutB.Add(mate.Genes[mate.Genes.Count - 1]);
                mate.Genes.RemoveAt(mate.Genes.Count - 1);
            }

            cutB.Reverse();

            // Adds the cut material back into the opposite subject

            foreach (bool b in cutA)
            {
                mate.Genes.Add(b);
            }

            foreach (bool b in cutB)
            {
                Genes.Add(b);
            }
        }