示例#1
0
    /// <summary>
    /// Creates a new generation by using GenerateBoxes and GenerateBoats.
    /// Previous generations will be removed and the best parents will be selected and used to create the new generation.
    /// The best parents (top 1) / all the parents 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();

        //Winner:
        BoatLogic lastBoatWinner = _activeBoats[0];

        Debug.Log("Last winner boat had: " + lastBoatWinner.GetPoints() + " points!");
        scoreWriter.WriteLine(roundCount + " " + lastBoatWinner.GetPoints());


        //we save the generation sorted by how well each boat performed or we save just the best boat
        if (saveCompleteGenerations)
        {
            string guid          = AssetDatabase.CreateFolder(savePrefabsAt, "Complete Gen-" + roundCount);
            string newFolderPath = AssetDatabase.GUIDToAssetPath(guid);
            for (int i = 0; i < _activeBoats.Count; i++)
            {
                _activeBoats[i].name = "(" + (i + 1) + ")" + _activeBoats[i].name + "Gen-" + roundCount;
                PrefabUtility.SaveAsPrefabAsset(_activeBoats[i].gameObject, newFolderPath + "/" + _activeBoats[i].name + ".prefab");
            }
        }
        else
        {
            lastBoatWinner.name += "Gen-" + roundCount;
            lastBoatWinnerData   = lastBoatWinner.GetData();
            PrefabUtility.SaveAsPrefabAsset(lastBoatWinner.gameObject, savePrefabsAt + "/" + lastBoatWinner.name + ".prefab");
        }

        if (_activeBoats.Count == 0)
        {
            GenerateBoats();
            return;
        }

        _boatParents = new BoatLogic[boatParentSize];
        if (!stochastic)
        {
            for (int i = 0; i < boatParentSize; i++)
            {
                _boatParents[i] = _activeBoats[i];
            }
        }
        else
        {
            stochasticSearch();
        }

        GenerateBoats(_boatParents);
    }
示例#2
0
    /// <summary>
    /// Plays a new game with the stored boats after storing the result of this one
    /// </summary>
    public void SaveGameAndPlayAgain()
    {
        _activeBoats.Sort();

        //Winner:
        BoatLogic lastBoatWinner = _activeBoats[0];

        Debug.Log("Last winner boat had: " + lastBoatWinner.GetPoints() + " points! " + "It was tagged " + lastBoatWinner.tag);

        Dictionary <string, Tuple <float, int> > aux = new Dictionary <string, Tuple <float, int> >(); //stores the tag, the total points and the mumber of boats

        //We save the scores in dictionary to the easily write them in the file
        foreach (BoatLogic boat in _activeBoats)
        {
            if (!aux.ContainsKey(boat.tag))
            {
                aux.Add(boat.tag, new Tuple <float, int>(0, 0));
            }
            aux[boat.tag] = new Tuple <float, int>(aux[boat.tag].Item1 + boat.GetPoints(), aux[boat.tag].Item2 + 1);
        }

        scoreWriter.Write(roundCount);
        foreach (KeyValuePair <string, Tuple <float, int> > value in aux)
        {
            scoreWriter.Write(" " + value.Key + ":" + value.Value.Item1 / value.Value.Item2);
        }
        scoreWriter.WriteLine();

        GenerateBoxes();
        instantiateStoredBoats();
    }
示例#3
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);
    }
    /// <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);
    }