示例#1
0
    /// <summary>
    /// Creates a new generation by using GenerateBoxes and GenerateBoats/Pirates.
    /// Previous generations will be removed and the best parents will be selected and used to create the new generation.
    /// The best parents (top 1) of the generation will be stored as a Prefab in the [savePrefabsAt] folder. Their name
    /// will use the [generationCount] as an identifier.
    /// </summary>
    public void MakeNewGeneration()
    {
        GenerateBoxes();

        //Fetch parents
        _activeBoats.RemoveAll(item => item == null);
        _activeBoats.Sort();

        Vector3 shipData = GetShipData();

        if (_activeBoats.Count == 0)
        {
            GenerateBoats(_boatParents);
        }
        _boatParents = new BoatLogic[boatParentSize];
        for (int i = 0; i < boatParentSize; i++)
        {
            _boatParents[i] = _activeBoats[i];
        }

        BoatLogic lastBoatWinner = _activeBoats[0];

        lastBoatWinner.name += "Gen-" + generationCount;
        lastBoatWinnerData   = lastBoatWinner.GetData();
        PrefabUtility.SaveAsPrefabAsset(lastBoatWinner.gameObject, savePrefabsAt + lastBoatWinner.name + ".prefab");

        _activePirates.RemoveAll(item => item == null);
        _activePirates.Sort();

        Vector3 pirateData = GetPirateData();

        _pirateParents = new PirateLogic[pirateParentSize];
        for (int i = 0; i < pirateParentSize; i++)
        {
            _pirateParents[i] = _activePirates[i];
        }

        PirateLogic lastPirateWinner = _activePirates[0];

        lastPirateWinner.name += "Gen-" + generationCount;
        lastPirateWinnerData   = lastPirateWinner.GetData();
        PrefabUtility.SaveAsPrefabAsset(lastPirateWinner.gameObject, savePrefabsAt + lastPirateWinner.name + ".prefab");

        _dataCollector.AddData(shipData, pirateData);

        //Winners:
        Debug.Log("Last winner boat had: " + lastBoatWinner.GetPoints() + " points!" + " Last winner pirate had: " + lastPirateWinner.GetPoints() + " points!");

        GenerateObjects(_boatParents, _pirateParents);
    }
    //Similarly generate abled pirates
    private void GenerateAbledPirates(PirateLogic[] abledPirateParents)
    {
        _activeAbledPirates = new List <PirateLogic>();
        List <GameObject> objects = abledPirateGenerator.RegenerateObjects();

        foreach (GameObject obj in objects)
        {
            PirateLogic pirate = obj.GetComponent <PirateLogic>();
            if (pirate != null)
            {
                _activeAbledPirates.Add(pirate);
                if (abledPirateParents != null)
                {
                    PirateLogic pirateParent = abledPirateParents[Random.Range(0, abledPirateParents.Length)];
                    pirate.Birth(pirateParent.GetData());
                }

                pirate.Mutate(mutationFactor, mutationChance);
                pirate.AwakeUp();
            }
        }
    }
示例#3
0
    private void GeneratePiratesArithmeticCrossover(PirateLogic[] pirateParents)
    {
        _activePirates = new List <PirateLogic>();
        List <GameObject> objects = pirateGenerator.RegenerateObjects();

        foreach (GameObject obj in objects)
        {
            PirateLogic pirate = obj.GetComponent <PirateLogic>();
            if (pirate != null)
            {
                _activePirates.Add(pirate);
                if (pirateParents != null)
                {
                    PirateLogic firstPirateParent  = pirateParents[Random.Range(0, pirateParents.Length)];
                    PirateLogic secondPirateParent = pirateParents[Random.Range(0, pirateParents.Length)];
                    pirate.BirthArithmeticCrossover(firstPirateParent.GetData(), secondPirateParent.GetData());
                }

                pirate.Mutate(mutationFactor, mutationChance);
                pirate.AwakeUp();
            }
        }
    }
    /// <summary>
    /// Creates a new generation by using GenerateBoxes and GenerateBoats/Pirates.
    /// Previous generations will be removed and the best parents will be selected and used to create the new generation.
    /// The best parents (top 1) of the generation will be stored as a Prefab in the [savePrefabsAt] folder. Their name
    /// will use the [generationCount] as an identifier.
    /// </summary>
    public void MakeNewGeneration()
    {
        GenerateBoxes();

        //Fetch parents
        _activeBoats.RemoveAll(item => item == null);
        _activeBoats.Sort();
        if (_activeBoats.Count == 0)
        {
            GenerateBoats(_boatParents);
        }
        _boatParents = new BoatLogic[boatParentSize];
        for (int i = 0; i < boatParentSize; i++)
        {
            _boatParents[i] = _activeBoats[i];
        }

        BoatLogic lastBoatWinner = _activeBoats[0];

        lastBoatWinner.name += "Gen-" + generationCount;
        lastBoatWinnerData   = lastBoatWinner.GetData();
        PrefabUtility.SaveAsPrefabAsset(lastBoatWinner.gameObject, savePrefabsAt + lastBoatWinner.name + ".prefab");
        boatRoundScores = 0;
        foreach (BoatLogic boat in _activeBoats)
        {
            boatRoundScores += boat.GetPoints();
        }
        boatTotalScores += boatRoundScores;
        _activePirates.RemoveAll(item => item == null);
        _activePirates.Sort();
        _pirateParents = new PirateLogic[pirateParentSize];
        for (int i = 0; i < pirateParentSize; i++)
        {
            _pirateParents[i] = _activePirates[i];
        }

        PirateLogic lastPirateWinner = _activePirates[0];

        pirateRoundScores = 0;
        foreach (PirateLogic pirate in _activePirates)
        {
            pirateRoundScores += pirate.GetPoints();
        }
        pirateTotalScores     += pirateRoundScores;
        lastPirateWinner.name += "Gen-" + generationCount;
        lastPirateWinnerData   = lastPirateWinner.GetData();
        PrefabUtility.SaveAsPrefabAsset(lastPirateWinner.gameObject, savePrefabsAt + lastPirateWinner.name + ".prefab");
        //Winners:

        //Storing results
        string path = "Assets/Overallresults.txt";
        //Write some text to the test.txt file
        StreamWriter writer = new StreamWriter(path, true);

        writer.WriteLine("Generations count" + generationCount + "||Total boats:" + boatTotalScores + "||TotalPiratesScores:" + pirateTotalScores);
        writer.Close();
        path   = "Assets/BoatsTotalScores.txt";
        writer = new StreamWriter(path, true);
        writer.WriteLine(boatTotalScores);
        writer.Close();
        path   = "Assets/PiratesTotalScores.txt";
        writer = new StreamWriter(path, true);
        writer.WriteLine(pirateTotalScores);
        writer.Close();
        path   = "Assets/BoatsRoundScores.txt";
        writer = new StreamWriter(path, true);
        writer.WriteLine(boatRoundScores);
        writer.Close();
        path   = "Assets/PiratesRoundScores.txt";
        writer = new StreamWriter(path, true);
        writer.WriteLine(pirateRoundScores);
        writer.Close();

        Debug.Log("Last winner boat had: " + lastBoatWinner.GetPoints() + " points!" + " Last winner pirate had: " + lastPirateWinner.GetPoints() + " points!");
        Debug.Log("Total boats:" + boatTotalScores + "||Round boats:" + boatRoundScores + "||TotalPiratesScores:" + pirateTotalScores + "||PiratesRoundScores" + pirateRoundScores);
        GenerateObjects(_boatParents, _pirateParents);
    }