/// <summary> /// Loads the simulation from save file with the format version 2 /// </summary> /// <param name="name">The name of the simulation 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); var simulationSettings = SimulationSettings.Decode(components[1]); var networkSettings = NeuralNetworkSettings.Decode(components[2]); var creatureData = components[3]; var creatureDesign = CreatureSerializer.ParseCreatureDesign(creatureData); creatureDesign.Name = creatureName; var bestChromosomesData = new List <string>(components[4].Split(splitOptions.NEWLINE_SPLIT, StringSplitOptions.None)); var bestChromosomes = new List <ChromosomeData>(); foreach (var chromosomeData in bestChromosomesData) { if (chromosomeData != "") { var stats = ChromosomeStats.FromString(chromosomeData); var data = new StringChromosomeData(stats.chromosome, stats.stats); bestChromosomes.Add(data.ToChromosomeData()); } } var chromosomeComponents = components[5].Split(splitOptions.NEWLINE_SPLIT, StringSplitOptions.None); var currentChromosomes = new List <float[]>(); foreach (var chromosome in chromosomeComponents) { if (chromosome != "") { currentChromosomes.Add(ConversionUtils.BinaryStringToFloatArray(chromosome)); } } var sceneDescription = DefaultSimulationScenes.DefaultSceneForObjective(simulationSettings.Objective); sceneDescription.PhysicsConfiguration = ScenePhysicsConfiguration.Legacy; return(new SimulationData( simulationSettings, networkSettings, creatureDesign, sceneDescription, bestChromosomes, currentChromosomes.ToArray(), bestChromosomes.Count )); }
private void TryImport(OpenedFile[] files) { // Whether at least one file was successfully imported var successfulImport = false; // Whether at least one .creat file failed to be imported var failedImport = false; CreatureDesign lastImportedDesign = null; foreach (var file in files) { var extension = file.Extension.ToLower(); if (extension.Equals(".creat")) { var nameFromFile = CreatureSerializer.EXTENSION_PATTERN.Replace(file.Name, ""); var encoded = file.ToUTF8String(); try { var design = CreatureSerializer.ParseCreatureDesign(encoded, nameFromFile); CreatureSerializer.SaveCreatureDesign(design, false); lastImportedDesign = design; } catch { failedImport = true; Debug.LogError(string.Format("Failed to parse .creat file contents: {0}", encoded)); continue; } successfulImport = true; } } RefreshCache(); try { viewController.Refresh(); } catch {} if (successfulImport) { importIndicator.FadeInOut(); CloseAndLoadOnNextFrame(lastImportedDesign.Name); } if (failedImport) { failedImportIndicator.FadeInOut(1.8f); } }
/// <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 )); }