Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        // Update time scale based on Editor value - do this every frame so we capture changes instantly
        Time.timeScale = timeScale;
        UpdateText();

        if (running)
        {
            // If the agents are currently jumping, stop the script if the ALL enter the DeadZone
            if (agentManager.AreAgentsJumping())
            {
                if (agentManager.AllAgentsTouchedDeadZone())
                {
                    this.enabled = false;
                }
            }
            // Make the agents jump if they are not jumping
            else
            {
                agentManager.UpdateAgentJumpingStrength(ga.Population);
                ga.NewGeneration();
                bestJump = ga.BestFitness;
                agentManager.MakeAgentsJump();
            }
        }
    }
Esempio n. 2
0
    private void EndOfGeneration()
    {
        if (spawner.deadCars == spawner.cars.Length)
        {
            bestPrerformanceList.Add(spawner.bestRun.x);
            geneticAlgorithm.CalculateFitness();
            orderedCars = geneticAlgorithm.Population;
            orderedCars.Sort((DNA <float> a, DNA <float> b) =>
            {
                return(a.Fitness > b.Fitness ? -1 : 1);
            });

            medianFitness = (orderedCars[(orderedCars.Count - 1) / 2].Fitness + orderedCars[(orderedCars.Count + 1) / 2].Fitness) / 2;
            Debug.Log(medianFitness);
            geneticAlgorithm.NewGeneration(selectionProcess);
            spawner.deadCars = 0;
            NextGeneration();
            spawner.bestRun = new Vector2(0, 0);
            longestRun      = 0;
            string temp, tempB;
            temp  = "0";
            tempB = "0";
            WriteToFile(bestPrerformanceList.Count.ToString(), medianFitness.ToString(), temp, tempB, "CarPerformance.csv");
        }
    }
Esempio n. 3
0
    // Update is called once per frame
    void Update()
    {
        if (!started)
        {
            return;
        }
        geneticAglorithm.MutationRate     = mutationRate;
        geneticAglorithm.MutationVariance = mutationVariance;
        // Update time scale based on Editor value - do this every frame so we capture changes instantly
        Time.timeScale = timeScale;
        bool allLanded = false;

        for (int shipIndex = 0; shipIndex < factory.ships.Count; shipIndex++)
        {
            allLanded = factory.ships[shipIndex].GetComponent <Engine>().landed;
            if (allLanded == false)
            {
                break;
            }
        }
        if (allLanded && gensFinished != maxGens)
        {
            for (int shipIndex = 0; shipIndex < factory.ships.Count; shipIndex++)
            {
                Engine engine = factory.ships[shipIndex].GetComponent <Engine>();
                for (int geneIndex = 0; geneIndex < 3; geneIndex++)
                {
                    engine.SetGene(geneticAglorithm.Population[shipIndex].Genes[geneIndex], geneIndex);
                }
                Transform shipTransform = factory.ships[shipIndex].transform;
                shipTransform.position = new Vector3(shipTransform.position.x, factory.startY, shipTransform.position.z);
            }
            geneticAglorithm.NewGeneration();
            gensFinished++;

            for (int shipIndex = 0; shipIndex < factory.ships.Count; shipIndex++)
            {
                Engine engine = factory.ships[shipIndex].GetComponent <Engine>();
                engine.Reset();
            }
        }
        //geneticAglorithm.NewGeneration();
        //bestJump = geneticAglorithm.BestFitness;
    }
Esempio n. 4
0
    void Update()
    {
        if (running)
        {
            // Each frame create a new generation
            ga.NewGeneration();

            updateText();

            // When we find the perfect solution (e.g. match the string with the best member of the population)
            if (ga.BestFitness == 1)
            {
                // Remove button through nasty evil code here
                GameObject button = GameObject.Find("StartButton");
                if (button)
                {
                    button.SetActive(false);
                }

                this.enabled = false;
            }
        }
    }