Exemplo n.º 1
0
        private void BinaryCrossover(BinaryGene gene1, BinaryGene gene2, out Gene child1, out Gene child2)
        {
            int crossPoint = _randomSeed.Next(gene1.GeneValue.Length);

            bool[] a = new bool[gene1.GeneValue.Length];
            bool[] b = new bool[gene1.GeneValue.Length];

            do
            {
                for (int i = 0; i < crossPoint; i++)
                {
                    a[i] = gene1.GeneValue[i];
                    b[i] = gene2.GeneValue[i];
                }

                for (int i = crossPoint; i < gene1.GeneValue.Length; i++)
                {
                    a[i] = gene2.GeneValue[i];
                    b[i] = gene1.GeneValue[i];
                }
            } while (!GeneConstraint.ConstraintFunction(a) || !GeneConstraint.ConstraintFunction(b));

            child1 = new BinaryGene(a, _randomSeed, gene1.GeneConstraint);
            child2 = new BinaryGene(b, _randomSeed, gene2.GeneConstraint);
        }
Exemplo n.º 2
0
        private void BinaryCrossover(BinaryGene gene1, BinaryGene gene2, out Gene child1, out Gene child2)
        {
            int crossPoint = _randomSeed.Next(gene1.GeneValue.Length);

            bool[] a = new bool[gene1.GeneValue.Length];
            bool[] b = new bool[gene1.GeneValue.Length];

            do
            {
                for (int i = 0; i < crossPoint; i++)
                {
                    a[i] = gene1.GeneValue[i];
                    b[i] = gene2.GeneValue[i];
                }

                for (int i = crossPoint; i < gene1.GeneValue.Length; i++)
                {
                    a[i] = gene2.GeneValue[i];
                    b[i] = gene1.GeneValue[i];
                }

            } while (!GeneConstraint.ConstraintFunction(a) || !GeneConstraint.ConstraintFunction(b));

            child1 = new BinaryGene(a, _randomSeed, gene1.GeneConstraint);
            child2 = new BinaryGene(b, _randomSeed, gene2.GeneConstraint);

        }