public void SaveAndLoadGenome() { var metaNeatGenome = new MetaNeatGenome <double>(3, 2, true, new ReLU()); var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome); // Simple acyclic graph. var connGenes = new ConnectionGenes <double>(6); connGenes[0] = (0, 3, 0.123); connGenes[1] = (1, 3, 1.234); connGenes[2] = (2, 3, -0.5835); connGenes[3] = (2, 4, 5.123456789); connGenes[4] = (2, 5, 2.5); connGenes[5] = (5, 4, 5.4); // Wrap in a genome. NeatGenome <double> genome = genomeBuilder.Create(0, 0, connGenes); // Create a memory stream to save the genome into. using (MemoryStream ms = new MemoryStream(1024)) { // Save the genome. NeatGenomeSaver <double> .Save(genome, ms); // Load the genome. ms.Position = 0; NeatGenomeLoader <double> loader = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome); NeatGenome <double> genomeLoaded = loader.Load(ms); // Compare the original genome with the loaded genome. IOTestUtils.CompareGenomes(genome, genomeLoaded); } }
private void saveBestGenomeToolStripMenuItem_Click(object sender, EventArgs e) { // Get the current best genome. NeatGenome <double> bestGenome = _neatPop?.BestGenome; if (bestGenome is null) { return; } // Ask the user to select a file path and name to save to. string filepath = SelectFileToSave("Save best genome", "genome", "(*.genome)|*.genome"); if (string.IsNullOrEmpty(filepath)) { return; } // Save the genome. try { NeatGenomeSaver <double> .Save(bestGenome, filepath); } catch (Exception ex) { __log.ErrorFormat("Error saving genome; [{0}]", ex.Message); } }
public void SaveGenome_FrenchLocale() { // Store the current/default culture info. Thread currentThread = Thread.CurrentThread; CultureInfo defaultCulture = currentThread.CurrentCulture; // Change the default culture to French (which uses e.g. a comma as a decimal separator). CultureInfo frenchCulture = new("fr-FR"); Thread.CurrentThread.CurrentCulture = frenchCulture; try { // Manually build a genome. var metaNeatGenome = new MetaNeatGenome <double>(3, 2, true, new ReLU()); NeatGenome <double> genomeBuilt = CreateGenome1(metaNeatGenome); // Save the genome into a MemoryStream. using MemoryStream ms = new(); NeatGenomeSaver <double> .Save(genomeBuilt, ms); // Load the saved genome. ms.Position = 0; NeatGenomeLoader <double> loader = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome); NeatGenome <double> genomeLoaded = loader.Load(ms); // Compare the original genome with the loaded one. IOTestUtils.CompareGenomes(genomeLoaded, genomeBuilt); } finally { // Restore the current thread's default culture; otherwise we may break other unit tests that use this thread. Thread.CurrentThread.CurrentCulture = defaultCulture; } }
/// <summary> /// Save the given population of genomes to a single zip archive file. /// </summary> /// <param name="genomeList">The population of genomes to save.</param> /// <param name="parentPath">Path to an existing folder to create the zip archive within.</param> /// <param name="name">The name of the zip archive (without the .zip extension, which will be appended by default).</param> /// <param name="compressionLevel">ZIP archive compression level.</param> public static void SaveToZipArchive( IList <NeatGenome <T> > genomeList, string parentPath, string name, CompressionLevel compressionLevel) { ArgumentNullException.ThrowIfNull(parentPath); ArgumentNullException.ThrowIfNull(name); // Check if the specified parent folder exists. if (!Directory.Exists(parentPath)) { throw new IOException($"parentPath does not exist [{parentPath}]"); } // Append zip extension for filenames that do not yet have it. // For all other extensions we leave them in place. If it isn't .zip then we assume the caller does this with good reason. string extension = Path.GetExtension(name); if (extension == string.Empty) { name += ".zip"; } // Check if the specified population zip archive name exists. string popZipPath = Path.Combine(parentPath, name); if (File.Exists(popZipPath)) { throw new IOException($"The specified population zip archive already exists [{popZipPath}]"); } string nameWithoutExt = Path.GetFileNameWithoutExtension(name); // Create a new zip archive. using FileStream fs = new(popZipPath, FileMode.CreateNew); using ZipArchive zipArchive = new(fs, ZipArchiveMode.Create); // Loop the genomes; add each one in turn to the zip archive. foreach (var genome in genomeList) { // Build the genome's entry name. string entryName = Path.Combine(nameWithoutExt, genome.Id.ToString("D6") + ".genome"); // Create an new zip entry. ZipArchiveEntry zipEntry = zipArchive.CreateEntry(entryName, compressionLevel); using (Stream zipEntryStream = zipEntry.Open()) { // Serialize the genome into the zip entry. NeatGenomeSaver <T> .Save(genome, zipEntryStream); } } }
public void SaveGenome() { // Manually build a genome. var metaNeatGenome = new MetaNeatGenome <double>(3, 2, true, new ReLU()); NeatGenome <double> genomeBuilt = CreateGenome1(metaNeatGenome); // Save the genome into a MemoryStream. using MemoryStream ms = new(); NeatGenomeSaver <double> .Save(genomeBuilt, ms); // Load the saved genome. ms.Position = 0; NeatGenomeLoader <double> loader = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome); NeatGenome <double> genomeLoaded = loader.Load(ms); // Compare the two genomes. IOTestUtils.CompareGenomes(genomeLoaded, genomeBuilt); }
/// <summary> /// Save the given population of genomes to the specified path on the local filesystem. /// </summary> /// <param name="genomeList">The population of genomes to save.</param> /// <param name="parentPath">Path to an existing folder to create the new population folder.</param> /// <param name="name">The name to assign to the population folder.</param> /// <remarks> /// A population is a collection of genomes. The genomes each serialized individually as when saving a single genome. /// The genome files are then either created in a new folder that contains all genomes for the population, or in a single /// zip archive file. /// </remarks> public static void SaveToFolder( IList <NeatGenome <T> > genomeList, string parentPath, string name) { ArgumentNullException.ThrowIfNull(parentPath); ArgumentNullException.ThrowIfNull(name); // Check if the specified parent folder exists. if (!Directory.Exists(parentPath)) { throw new IOException($"parentPath does not exist [{parentPath}]"); } // Check if the specified population folder name exists. string popFolderPath = Path.Combine(parentPath, name); if (Directory.Exists(popFolderPath)) { throw new IOException($"The specified population folder already exists [{popFolderPath}]"); } // Create the population folder. DirectoryInfo popDirInfo = Directory.CreateDirectory(popFolderPath); string popDirPath = popDirInfo.FullName; // Loop the genomes; save each one in turn. foreach (var genome in genomeList) { // Build the genome's filepath. string genomePath = Path.Combine(popDirPath, genome.Id.ToString("D6") + ".genome"); // Save the genome. NeatGenomeSaver <T> .Save(genome, genomePath); } }