Exemplo n.º 1
0
    void Update()
    {
        timeCurrent = Time.timeSinceLevelLoad;

        Debug.Log("RUNNING GENERATION: " + testGeneration.generationIndex);
        Debug.Log("RUNNING INDIVIDUAL: " + currentIndex);

        rawirDataArray  = SimpleCarController.returnRawIRdata();
        rawldrDataArray = SimpleCarController.returnRawLDRdata();

        ldrDataArray = SimpleCarController.returnLDRdata();
        irDataArray  = SimpleCarController.returnIRdata();


        if (simulationRunning)
        {
            // IF we have passed the start screen...
            if (pastStartScreen)
            {
                // If this is the first run after passing the start screen...
                if (firstRun)
                {
                    trialTime = MainMenu.INPUTtrialLength;
                    Debug.Log("THIS IS THE TRIAL TIME: " + trialTime);
                    // Create a generation.
                    testGeneration.setGenerationParameters(MainMenu.INPUTavgGenomeSize, MainMenu.INPUTnumberOfGenerations, MainMenu.INPUTnumberOfIndividuals);
                    testGeneration.createStartGeneration();
                }
                if (!(firstGenerationRun))
                {
                    if (timeCurrent < trialTime)
                    {
                        evaluateTrialFitness(rawldrDataArray, rawirDataArray, testGeneration.collectionOfIndividuals[currentIndex].paramsCollection.chosenSensorArray);
                        beginRun(ldrDataArray, irDataArray);
                    }
                    else if (timeCurrent >= trialTime)
                    {
                        SimpleCarController.stopMovement();
                        Debug.Log("DONE RUNNING INDIVIDUAL: " + currentIndex);
                        testGeneration.collectionOfIndividuals[currentIndex].fitnessScore = finalFitnessCalculation();
                        int numberOfindividuals = testGeneration.numberOfIndividualsInGeneration;

                        if (currentIndex < numberOfindividuals)
                        {
                            // testGeneration.collectionOfIndividuals[currentIndex].fitnessScore = finalFitnessCalculation();
                            SimpleCarController.stopMovement();
                            reset();
                        }
                        else if (currentIndex >= numberOfindividuals)
                        {
                            if (testGeneration.generationIndex < testGeneration.numberOfGenerations)
                            {
                                Debug.Log("Starting next generation");
                                testGeneration.createNewGeneration();
                                currentIndex = 0;
                                SceneManager.LoadScene("Experiment");
                            }
                            else if (testGeneration.generationIndex >= testGeneration.numberOfGenerations)
                            {
                                Debug.Log("END OF SIMULATION REACHED, NUMBER OF GENERATIONS EVALUATED: " + testGeneration.numberOfGenerations);
                                simulationRunning = false;
                            }
                        }
                    }
                }
                else
                {
                    totalIterations = 0;
                }
                firstGenerationRun = false;
            }
        }
        else
        {
            Debug.Log("Simulation has finished!");
        }
    }