示例#1
0
    private void OnePointCrossover(Genotype parent1, Genotype parent2, ref Genotype offspring1, ref Genotype offspring2)
    {
        var genotypeParent1 = parent1.GetRawGenotype();
        var genotypeParent2 = parent2.GetRawGenotype();
        int crossoverPoint  = genotypeParent1.Count;

        if (Random.Range(0.0f, 1.0f) < crossoverRate)
        {
            crossoverPoint = Random.Range(0, genotypeParent1.Count);

            List <float[]> genotypeA = new List <float[]>();
            List <float[]> genotypeB = new List <float[]>();

            for (int i = 0; i < genotypeParent1.Count; ++i)
            {
                if (i < crossoverPoint)
                {
                    genotypeA.Add(genotypeParent1[i]);
                    genotypeB.Add(genotypeParent2[i]);
                }
                else
                {
                    genotypeA.Add(genotypeParent2[i]);
                    genotypeB.Add(genotypeParent1[i]);
                }
            }

            offspring1.SetRawGenotype(genotypeA);
            offspring2.SetRawGenotype(genotypeB);
        }
    }