예제 #1
0
        private void crossover()
        {
            List <Bird> parents = selectParents();

            System.Random random = new System.Random();
            PopulationMembers.Clear();

            for (int i = 0; i < PopulationSize; i++)
            {
                GameObject newChild         = GameObject.Instantiate(Bird, new Vector3(-7.5f, 1.0f, 0.0f), Quaternion.identity);
                Bird       newChildInstance = newChild.GetComponent <Bird>();
                newChildInstance.onBecomeInvisible   += BirdDeath;
                newChildInstance.onHit               += BirdDeath;
                newChildInstance.PiperendererInstance = piperendererInstance;

                Matrix <double> crossedHiddenLayer1 = parents[0].Brain.HiddenLayers[0].Weights +
                                                      random.NextDouble() * (parents[1].Brain.HiddenLayers[0].Weights -
                                                                             parents[0].Brain.HiddenLayers[0].Weights);

                Matrix <double> crossedHiddenLayer2 = parents[0].Brain.HiddenLayers[1].Weights +
                                                      random.NextDouble() * (parents[1].Brain.HiddenLayers[1].Weights -
                                                                             parents[0].Brain.HiddenLayers[1].Weights);

                Matrix <double> crossedOutputLayer = parents[0].Brain.OutputLayer.Weights +
                                                     random.NextDouble() * (parents[1].Brain.OutputLayer.Weights -
                                                                            parents[0].Brain.OutputLayer.Weights);

                newChildInstance.Brain.HiddenLayers[0].Weights = crossedHiddenLayer1;
                newChildInstance.Brain.HiddenLayers[1].Weights = crossedHiddenLayer2;
                newChildInstance.Brain.OutputLayer.Weights     = crossedOutputLayer;
                PopulationMembers.Add(newChild, newChildInstance);
            }
        }
예제 #2
0
 public void InitializePopulation()
 {
     for (int i = 0; i < PopulationSize; i++)
     {
         GameObject bird         = GameObject.Instantiate(Bird, new Vector3(-7.5f, 1.0f, 0.0f), Quaternion.identity);
         Bird       birdInstance = bird.GetComponent <Bird>();
         birdInstance.onBecomeInvisible   += BirdDeath;
         birdInstance.onHit               += BirdDeath;
         birdInstance.PiperendererInstance = piperendererInstance;
         PopulationMembers.Add(bird, bird.GetComponent <Bird>());
     }
 }