Beispiel #1
0
    private void Update()
    {
        Debug.Log(NotACarController.totalLost);
        if (NotACarController.totalLost == numOfCars)
        {
            for (int i = 0; i < carsParentTr.childCount; i++)
            {
                carsParentTr.GetChild(i).GetComponent <NotACarController>().Restart();
            }

            NotACarController.weightsList.Clear();
            NotACarController.totalLost = 0;

            ga.CreateNewGeneration();

            generationText.text = "Generation: " + ga.Generation;

            for (int i = 0; i < ga.Population.Count; i++)
            {
                NotACarController.weightsList.Add(InitWeights(ga.Population[i]));
            }
            //StartCoroutine(NewGeneration(0.5f)); // Create the new generation after some time
        }

        // Show the travel distance and lap of the best car
        float highestFitness = 0;
        int   bestCarID      = 0;

        for (int i = 0; i < carsParentTr.childCount; i++)
        {
            if (carControllers[i].fitness > highestFitness)
            {
                highestFitness = carControllers[i].fitness;
                bestCarID      = i;
            }
        }

        Camera.main.GetComponent <CameraController>().camRider = carsParentTr.GetChild(bestCarID);

        numOfLapsText.text  = "Lap Number: " + carControllers[bestCarID].numOfLaps.ToString();
        bestTravelText.text = "Best #" + (bestCarID + 1) + " - Total: " + carControllers[bestCarID].totalTravel.ToString("F0") + " Meters" +
                              " + " + carControllers[bestCarID].totalBonus;
        speedText.text = "Speed: " + carControllers[bestCarID].currSpeed.ToString("F0");

        // If we click a button, save the weights and stop the simulation

        //if (Input.GetKeyDown(KeyCode.Escape))
    }
    //  Add x dead aliens to the backup GA, then create a new generation.
    //  If it exceeds cap, remove the excess then give all dead aliens the new DNA.
    private void SwapGABacktoFront(List <GameObject> _deadAliens)
    {
        for (int i = 0; i < _deadAliens.Count; i++)
        {
            gaB.population.Add(_deadAliens[i].GetComponent <Alien>().dna);
        }

        print("GENERATION " + gaB.generation + ": FITNESS " + FindDifficulty());
        gaB.CreateNewGeneration();


        for (int j = 0; j < _deadAliens.Count; j++)
        {
            _deadAliens[j].GetComponent <Alien>().dna              = gaB.population[j];
            _deadAliens[j].GetComponent <Alien>().movesUsed        = 0;
            _deadAliens[j].GetComponent <Alien>().alive            = false;
            _deadAliens[j].GetComponent <AlienController>().killed = false;
            SpawnAliensInRound();
        }
        gaB.population.Clear();
    }