Example #1
0
    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(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);
        }
    }
    public void SaveAndLoadPopulationToZipArchive()
    {
        // Create a test population.
        NeatPopulation <double> pop = NestGenomeTestUtils.CreateNeatPopulation();

        // Build path to test population folder.
        string parentPath = Path.Combine(Directory.GetCurrentDirectory(), "test-pops");

        // Delete folder if it already exists.
        if (Directory.Exists(parentPath))
        {
            Directory.Delete(parentPath, true);
        }

        // Create an empty parent folder to save populations into.
        Directory.CreateDirectory(parentPath);

        // Save the population to the unit test output folder.
        NeatPopulationSaver <double> .SaveToZipArchive(pop.GenomeList, parentPath, "pop2", System.IO.Compression.CompressionLevel.Optimal);

        // Load the population.
        NeatPopulationLoader <double> loader         = NeatPopulationLoaderFactory.CreateLoaderDouble(pop.MetaNeatGenome);
        string populationZipPath                     = Path.Combine(parentPath, "pop2.zip");
        List <NeatGenome <double> > genomeListLoaded = loader.LoadFromZipArchive(populationZipPath);

        // Compare the loaded genomes with the original genome list.
        IOTestUtils.CompareGenomeLists(pop.GenomeList, genomeListLoaded);
    }
Example #3
0
    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;
        }
    }
Example #4
0
    public void LoadGenome()
    {
        var metaNeatGenome = new MetaNeatGenome <double>(3, 2, true, new ReLU());

        // Load test genome.
        NeatGenomeLoader <double> loader       = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome);
        NeatGenome <double>       genomeLoaded = loader.Load("TestData/example1.genome");

        // Manually build an equivalent genome.
        NeatGenome <double> genomeBuilt = CreateGenome1(metaNeatGenome);

        // Compare the two genomes.
        IOTestUtils.CompareGenomes(genomeLoaded, genomeBuilt);
    }
Example #5
0
    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);
    }