/// <summary> /// Writes a list of genomes to the save file fitting the experiment name and the ExperimentFileType. /// </summary> private static bool WriteGenomes(INeatExperiment experiment, IList <NeatGenome> genomeList, ExperimentFileType fileType) { XmlWriterSettings _xwSettings = new XmlWriterSettings(); _xwSettings.Indent = true; string filePath = GetSaveFilePath(experiment.Name, fileType); DirectoryInfo dirInf = new DirectoryInfo(Application.persistentDataPath); if (!dirInf.Exists) { Debug.Log("ExperimentIO - Creating subdirectory"); dirInf.Create(); } try { using (XmlWriter xw = XmlWriter.Create(filePath, _xwSettings)) { NeatGenomeXmlIO.WriteComplete(xw, genomeList, false); Debug.Log("Successfully saved the genomes of the '" + fileType.ToString() + "' for the experiment '" + experiment.Name + "' to the location:\n" + filePath); } } catch (Exception e1) { Debug.Log("Error saving the genomes of the '" + fileType.ToString() + "' for the experiment '" + experiment.Name + "' to the location:\n" + filePath); return(false); } return(true); }
/// <summary> /// Loads a list of genomes from the save file fitting the experiment name and the ExperimentFileType. /// </summary> private static List <NeatGenome> ReadGenomes(INeatExperiment experiment, ExperimentFileType fileType, bool createNewGenesIfNotLoadable = true) { List <NeatGenome> genomeList = null; NeatGenomeFactory genomeFactory = (NeatGenomeFactory)experiment.CreateGenomeFactory(); string filePath = GetSaveFilePath(experiment.Name, fileType); try { using (XmlReader xr = XmlReader.Create(filePath)) { genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory); if (genomeList != null && genomeList.Count > 0) { Utility.Log("Successfully loaded the genomes of the '" + fileType.ToString() + "' for the experiment '" + experiment.Name + "' from the location:\n" + filePath); } } } catch (Exception e1) { Utility.Log("Error loading genome from file, could not find the file at: " + filePath + "\n" + e1.Message); if (createNewGenesIfNotLoadable) { genomeList = genomeFactory.CreateGenomeList(experiment.DefaultPopulationSize, 0); } } return(genomeList); }