示例#1
0
 void Generate(List <Genome> champs)
 {
     _genCount++;
     ClearField();
     for (int i = 0; i < _population; i++)
     {
         var flappy = Instantiate(_flappy, _flappyParent);
         flappy.transform.localPosition = Vector3.zero;
         _alives.Add(flappy);
         var    aIController = flappy.GetComponent <FlappyAIController>();
         Genome genome       = new Genome();
         if (i < champs.Count)
         {
             genome.CloneFrom(champs[i]);
             flappy.SetChampion();
         }
         else if (i < champs.Count + _randomCount)
         {
         }
         else if (_contestants.Count > 0)
         {
             MakeBaby(ref genome);
         }
         aIController.SetGenome(genome);
     }
     _contestants.Clear();
 }
示例#2
0
    private void MakeBaby(ref Genome genome)
    {
        float rand = UnityEngine.Random.value;

        if (rand < (1 - _chanceOfCrossover))
        {
            var player = SelectPlayer();
            genome.CloneFrom(player._genome);
        }
        else
        {
            // rest of the time do crossover
            var parent1 = SelectPlayer();
            var parent2 = SelectPlayer();

            //the crossover function expects the highest fitness parent to be the object and the lowest as the argument
            if (parent1._score < parent2._score)
            {
                genome.Crossover(parent1._genome, parent2._genome);
            }
            else
            {
                genome.Crossover(parent2._genome, parent1._genome);
            }
        }
        genome.Mutate();
    }