예제 #1
0
    public void DecreaseBird(NeuralNetwork net, double fitness)
    {
        alive--;

        if (fitness >= fitnessThreshold)
        {
            //this is one of the best networks
            bestNetworks.Add(net);
        }
        else
        {
            if (fitness >= minAcceptedFitness || alive == 0 || alive == 1 || alive == 2)
            {
                current.Add(net);
            }
        }

        if (alive == 0)
        {
            GAController best = new GAController(bestNetworks);
            GAController cur  = new GAController(current);
            // we need to start a new generation
            generation++;
            gen.text = $"Generation: {generation}";
            obstaclesSpawner.ClearObstacles();
            int should_spawn = populationPerGeneration;

            if (bestNetworks.Count >= 2)
            {
                // 80% of the population
                int p = (int)(0.8f * should_spawn);
                should_spawn -= p;
                for (int i = 0; i < p; i++)
                {
                    NeuralNetwork network = best.CrossOver();
                    Spawn(network);
                }
                Debug.Log("Spawned From The Best:" + p);
            }
            if (current.Count >= 2)
            {
                int p = (int)(0.2f * should_spawn);
                should_spawn -= p;
                for (int i = 0; i < p; i++)
                {
                    NeuralNetwork network = cur.CrossOver();
                    Spawn(network);
                }
                Debug.Log("Spawned From The Current :" + p);
            }

            Spawn(should_spawn);
            Debug.Log("Spawned Randomly:" + should_spawn);

            current.Clear();
        }
    }