Ejemplo n.º 1
0
    // Function for starting the actual evolutionary progress.
    public virtual IEnumerator Evolve()
    {
        // Start by randomly generating the initial population.
        for (int i = 0; i < populationSize; i++)
        {
            m_population.Add(RandomGenome());
        }
        // Randomly generate the first best genome.
        bestGenome = RandomGenome();
        // Set the starting fitness of best to be the same as default fitness of
        // population genomes.
        bestGenome.Fitness = 100;

        // Run algorithm for the given amount of generations.
        for (int i = 0; i < generations; i++)
        {
            feedbackText.SetText("Generation " + i + " out of " + generations + "...");
            // Start simulation and wait until done.
            simulationDone = false;
            StartCoroutine(Simulate(m_population));
            yield return(new WaitUntil(() => simulationDone));

            // After simulation, start evaluation.
            Evaluate();

            // Add genomes to childpop until it's the same size as previous.
            while (m_childPop.Count < m_population.Count)
            {
                Genome parent0, parent1;
                // Select genomes given the results of simulation.
                //RouletteSelect(out parent0, out parent1);
                parent0 = TournamentSelect(m_population);
                parent1 = TournamentSelect(m_population);

                // Combine parents to retrieve two children.
                Genome child0, child1;
                Combine(out child0, out child1, parent0, parent1);

                // Finally, randomly mutate children and then add them to population.
                Mutate(child0.RootNode);
                Mutate(child1.RootNode);
                m_childPop.Add(child0);
                m_childPop.Add(child1);
            }

            // Set the previous population to the current childPop.
            m_population = m_childPop;
            // Reset childpop for next generaion.
            m_childPop = new List <Genome>();
        }
        // Save the final best tree.
        FileSaver.GetInstance().SaveTree(bestGenome.RootNode, "singleEvolved");
        feedbackText.SetText("Single evolution complete!");
        buttonCanvas.SetActive(true);

        yield return(null);
    }
Ejemplo n.º 2
0
    // Load both trees and assign them to the match simulator.
    private IEnumerator StartBattle()
    {
        singleBT = FileSaver.GetInstance().LoadTree(singleFilename);
        multiBT  = FileSaver.GetInstance().LoadTree(multiFilename);

        // Reset scores
        m_singleWon    = m_multiWon = 0;
        m_singleDamage = m_multiDamage = 0;
        m_singleHealth = m_multiHealth = 0;

        if (singleBT == null || multiBT == null)
        {
            Debug.LogError("Couldn't find requested behaviourtrees.");
            m_feedbackText.SetText("There doesn't exist a file for each algorithm. Please evolve using one of each before battling.");
            yield return(null);
        }

        originalMatchLength   = m_simulator.matchTime;
        m_simulator.matchTime = matchLength;
        // Disable button canvas before starting simulation
        m_buttonCanvas.SetActive(false);
        for (int i = 0; i < simulationMatches; i++)
        {
            m_feedbackText.SetText("Battle " + i);

            // Start match and set timescale
            battleOn = true;
            m_simulator.liveSimulationScale = battleTimeScale;

            // Start match and wait until over.
            m_simulator.SetAgentBTs(singleBT, multiBT);
            m_simulator.StartMatch();
            yield return(new WaitUntil(() => !battleOn));
        }

        ScreenCapture.CaptureScreenshot("Resultat.png");

        m_feedbackText.SetText("Single won: " + m_singleWon + ", Multi won: " + m_multiWon
                               + "\n" + "Single damage: " + m_singleDamage + ", Multi damage: " + m_multiDamage
                               + "\n" + "Single health: " + m_singleHealth + ", Multi health: " + m_multiHealth);
        m_buttonCanvas.SetActive(true);
        // Reset simulation scale and matchtime before yielding
        m_simulator.liveSimulationScale = m_simulator.simulationTimeScale;
        m_simulator.matchTime           = originalMatchLength;

        yield return(null);
    }