Esempio n. 1
0
    /// <summary>
    /// Breeds a new generation and saves it as a .txt file
    /// </summary>
    List <string> CreateNextGeneration()
    {
        /*Logging details about last generation's individuals*/
        GenLogManager.SaveLog(LogType.Individual);

        List <string> newGeneration = BreedNewGeneration();

        GenFilesManager.SaveGeneration(newGeneration);
        return(newGeneration);
    }
Esempio n. 2
0
    List <string> RandomizeNextGeneration()
    {
        /*Logging details about last generation's individuals*/
        GenLogManager.SaveLog(LogType.Individual);

        List <Individual> sortedList = individuals.OrderByDescending(i => i.FitnessScore).ToList();

        /*Logging*/
        GenLogManager.LogAfterSort(sortedList);
        GenLogManager.LogForGraphing(sortedList);
        GenLogManager.SaveLog(LogType.ForGraphing);

        List <string> newGeneration = RandomizeGeneration();

        /*Logging*/
        GenLogManager.LogNewGeneration(newGeneration);
        /*Logging*/
        GenLogManager.SaveLog(LogType.Progress);

        individuals.Clear();
        CurrentGeneration++;

        return(newGeneration);
    }
Esempio n. 3
0
    /// <summary>
    /// Looks at the top half of the fitness-scores from the last generation and uses
    /// a single point crossover to create a new generation
    /// </summary>
    List <string> BreedNewGeneration()
    {
        List <Individual> sortedList = individuals.OrderByDescending(i => i.FitnessScore).ToList();

        /*Logging*/
        GenLogManager.LogAfterSort(sortedList);
        GenLogManager.LogForGraphing(sortedList);
        GenLogManager.SaveLog(LogType.ForGraphing);

        List <string> newGeneration = new List <string>();
        List <string> newAttributes = new List <string>();
        List <string> newFeatures   = new List <string>();

        for (int i = 0; i < sortedList.Count / 2; i++)
        {
            newGeneration.Add(sortedList[i].Traits);

            string[] traits = sortedList[i].Traits.Split('|');
            newAttributes.Add(traits[0]);
            newFeatures.Add(traits[1]);
        }
        /*Logging*/
        GenLogManager.LogParentsToBreed(newAttributes, newFeatures);

        int numberOfParents = newAttributes.Count;

        for (int indexParentA = 0; indexParentA < numberOfParents; indexParentA += 2)
        {
            int indexParentB = indexParentA + 1;

            StringBuilder newChildA = new StringBuilder();
            StringBuilder newChildB = new StringBuilder();

            // Uniform crossover
            for (int indexParentTrait = 0; indexParentTrait < Attributes; indexParentTrait++)
            {
                int coinFlip = rand.Next(0, 2);
                if (coinFlip == 0)
                {
                    newChildA.Append(newAttributes[indexParentA][indexParentTrait]);
                    newChildB.Append(newAttributes[indexParentB][indexParentTrait]);
                }
                else
                {
                    newChildA.Append(newAttributes[indexParentB][indexParentTrait]);
                    newChildB.Append(newAttributes[indexParentA][indexParentTrait]);
                }
            }

            newChildA.Append('|');
            newChildB.Append('|');

            // Uniform crossover
            for (int i = 0; i < 2; i++)
            {
                int coinToss = rand.Next(0, 2);
                if (coinToss == 0)
                {
                    newChildA.Append(newFeatures[indexParentA][i]);
                    newChildB.Append(newFeatures[indexParentB][i]);
                }
                else
                {
                    newChildA.Append(newFeatures[indexParentB][i]);
                    newChildB.Append(newFeatures[indexParentA][i]);
                }
            }

            ConsiderMutation(newChildA);
            ConsiderMutation(newChildB);

            // If any of the children are identical to any of the parents; redo the crossover
            if (newChildA.ToString() == newGeneration[indexParentA] ||
                newChildA.ToString() == newGeneration[indexParentB])
            {
                GenLogManager.LogReBreed(newChildA.ToString());
                indexParentA -= 2;
                continue;
            }
            else if (newChildB.ToString() == newGeneration[indexParentA] ||
                     newChildB.ToString() == newGeneration[indexParentB])
            {
                GenLogManager.LogReBreed(newChildB.ToString());
                indexParentA -= 2;
                continue;
            }

            newGeneration.Add(newChildA.ToString());
            newGeneration.Add(newChildB.ToString());
        }

        newGeneration.Add(RandomizeIndividual());

        /*Logging*/
        GenLogManager.LogNewGeneration(newGeneration);

        List <string> shuffledGeneration = newGeneration.OrderBy(x => UnityEngine.Random.value).ToList();

        /*Logging*/
        GenLogManager.LogNewGeneration(shuffledGeneration);
        /*Logging*/
        GenLogManager.SaveLog(LogType.Progress);

        individuals.Clear();
        CurrentGeneration++;

        return(shuffledGeneration);
    }
Esempio n. 4
0
    void Update()
    {
        if (tutorial)
        {
            return;
        }

        if (CurrentGeneration > TotalGenerations)
        {
            GenLogManager.LogForGraphing(individuals);
            GenLogManager.SaveLog(LogType.Individual);
            endText.SetActive(true);
            readyManager.End();
            gameEnd = true;
        }

        if (gameEnd)
        {
            if (Input.GetKeyDown(KeyCode.Escape) || Input.GetButtonDown("Confirm"))
            {
                if (RandomMode)
                {
                    Application.Quit();
                }
                else
                {
                    RandomMode = true;
                    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
                }
            }
            return;
        }

        if (playerHealth.IsDead && PlayerReady)
        {
            ResetWave();
        }
        else if (playerHealth.IsDead && !PlayerReady)
        {
            readyManager.NewWave(dead: true);
            return;
        }
        else if (!PlayerReady)
        {
            return;
        }

        if (!gameStarted)
        {
            if (RandomMode)
            {
                individuals = StringsToIndividuals(RandomizeGeneration());
            }
            else
            {
                individuals = StringsToIndividuals(CreateFirstGeneration());
            }

            ResetVariables();
            gameStarted = true;

            PlayerReady = false;
            readyManager.NewWave();
            return;
        }
        else if (InstantiatedIndividuals >= GenerationSize &&
                 DeadIndividuals >= GenerationSize &&
                 CurrentGeneration <= TotalGenerations)
        {
            if (RandomMode)
            {
                individuals = StringsToIndividuals(RandomizeNextGeneration());
            }
            else
            {
                individuals = StringsToIndividuals(CreateNextGeneration());
            }

            ResetVariables();

            PlayerReady = false;
            readyManager.NewWave();
            return;
        }


        spawnTimer += Time.deltaTime;

        if (InstantiatedIndividuals - DeadIndividuals <= 0)
        {
            spawnTimer = spawnFrequency;
        }

        if (spawnTimer >= spawnFrequency)
        {
            if ((InstantiatedIndividuals - DeadIndividuals < ConcurrentIndividuals) &&
                (InstantiatedIndividuals < GenerationSize) &&
                (CurrentGeneration <= TotalGenerations))
            {
                spawnTimer = 0;
                Spawn();
            }
        }
    }