/// <summary>
    /// Saves the specified simulation data to a file
    /// </summary>
    /// <returns>The filename of the save file without the extensions</returns>
    public static string SaveSimulation(SimulationData data)
    {
        string contents        = data.Encode().ToString(Formatting.None);
        string creatureName    = data.CreatureDesign.Name;
        string dateString      = System.DateTime.Now.ToString("MMM dd, yyyy");
        string objectiveString = ObjectiveUtil.StringRepresentation(data.Settings.Objective);
        int    generation      = data.BestCreatures.Count + 1;
        string filename        = string.Format("{0} - {1} - {2} - Gen {3}", creatureName, objectiveString, dateString, generation);

        // Save without overwriting existing saves
        return(SaveSimulationFile(filename, contents, false));
    }
Exemple #2
0
    private static SimulationSettings DecodeV1(string encoded)
    {
        var parts    = encoded.Split('#');
        var settings = Default;

        settings.KeepBestCreatures = bool.Parse(parts[1]);
        settings.SimulationTime    = int.Parse(parts[2]);
        settings.PopulationSize    = int.Parse(parts[3]);
        settings.SimulateInBatches = bool.Parse(parts[4]);
        settings.BatchSize         = int.Parse(parts[5]);
        settings.Objective         = ObjectiveUtil.ObjectiveFromString(parts[6]);
        settings.MutationRate      = Math.Min(Math.Max(((float)int.Parse(parts[7])) / 100f, 0), 1);

        return(settings);
    }
    /// <summary>
    /// Loads the simulation from save file of format version 1.
    /// </summary>
    /// <param name="name">The name of the simualtion save.</param>
    /// <param name="content">The Content of the save file.</param>
    public static SimulationData ParseSimulationData(string name, string content, LegacySimulationLoader.SplitOptions splitOptions)
    {
        var creatureName = name.Split('-')[0].Replace(" ", "");

        if (string.IsNullOrEmpty(creatureName))
        {
            creatureName = "Unnamed";
        }

        var components = content.Split(splitOptions.SPLIT_ARRAY, System.StringSplitOptions.None);

        // extract the save data from the file contents.
        var objectiveType = ObjectiveUtil.ObjectiveForNumber(int.Parse(components[0].Replace(Environment.NewLine, "")));

        var timePerGen = int.Parse(components[1].Replace(Environment.NewLine, ""));

        var creatureData   = components[2];
        var creatureDesign = CreatureSerializer.ParseCreatureDesign(creatureData, creatureName);

        var bestChromosomesData = new List <string>(components[3].Split(splitOptions.NEWLINE_SPLIT, StringSplitOptions.None));
        var bestChromosomes     = new List <ChromosomeData>();

        foreach (var chromosomeData in bestChromosomesData)
        {
            if (chromosomeData != "")
            {
                // Parse the chromosome data
                var parts           = chromosomeData.Split(':');
                var chromosome      = parts[0];
                var fitness         = float.Parse(parts[1]);
                var chromosomeStats = new ChromosomeStats(chromosome, new CreatureStats());
                chromosomeStats.stats.fitness = fitness;
                var data = new StringChromosomeData(chromosome, chromosomeStats.stats);
                bestChromosomes.Add(data.ToChromosomeData());
            }
        }

        var chromosomeComponents = components[4].Split(splitOptions.NEWLINE_SPLIT, StringSplitOptions.None);
        var currentChromosomes   = new List <float[]>();

        foreach (var chromosome in chromosomeComponents)
        {
            if (chromosome != "")
            {
                currentChromosomes.Add(ConversionUtils.BinaryStringToFloatArray(chromosome));
            }
        }

        var currentGeneration = bestChromosomes.Count + 1;

        var settings = new SimulationSettings();

        settings.Objective      = objectiveType;
        settings.SimulationTime = timePerGen;
        settings.PopulationSize = currentChromosomes.Count;

        var networkSettings  = NeuralNetworkSettings.Default;
        var sceneDescription = DefaultSimulationScenes.DefaultSceneForObjective(settings.Objective);

        sceneDescription.PhysicsConfiguration = ScenePhysicsConfiguration.Legacy;

        int lastSimulatedV2Generation = bestChromosomes.Count;

        return(new SimulationData(
                   settings, networkSettings, creatureDesign,
                   sceneDescription, bestChromosomes,
                   currentChromosomes.ToArray(),
                   lastSimulatedV2Generation
                   ));
    }