Esempio n. 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);
        }
    }
        /// <summary>
        /// Create a new instance of <see cref="NeatPopulationLoader{Double}"/>.
        /// </summary>
        /// <param name="metaNeatGenome">Meta neat genome.</param>
        /// <returns>A new instance of <see cref="NeatPopulationLoader{Double}"/>.</returns>
        public static NeatPopulationLoader <double> CreateLoaderDouble(
            MetaNeatGenome <double> metaNeatGenome)
        {
            NeatGenomeLoader <double> genomeLoader = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome);

            return(new NeatPopulationLoader <double>(genomeLoader));
        }
Esempio n. 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;
        }
    }
        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);
        }
Esempio n. 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);
    }
        static NeuralNetAcyclicBenchmarks()
        {
            // TODO: Load neural nets directly, instead of loading a genome and decoding.
            var metaNeatGenome = new MetaNeatGenome <double>(12, 1, true, new ActivationFunctions.LeakyReLU());
            var genomeLoader   = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome);
            var genome         = genomeLoader.Load("data/genomes/binary11.genome");

            var genomeDecoder = new NeatGenomeDecoderAcyclic();

            __nn = (NeuralNetAcyclic)genomeDecoder.Decode(genome);

            // Set some non-zero random input values.
            var rng = RandomDefaults.CreateRandomSource();

            for (int i = 0; i < __nn.InputVector.Length; i++)
            {
                __nn.InputVector[i] = rng.NextDouble();
            }
        }
Esempio n. 7
0
    static NeuralNetCyclicBenchmarks()
    {
        // TODO: Load neural nets directly, instead of loading a genome and decoding.
        var metaNeatGenome = new MetaNeatGenome <double>(14, 4, false, new LeakyReLU());
        var genomeLoader   = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome);
        var genome         = genomeLoader.Load("data/genomes/preycapture.genome");

        var genomeDecoder = new NeatGenomeDecoderCyclic(1);

        __nn = (NeuralNetCyclic)genomeDecoder.Decode(genome);

        // Set some non-zero random input values.
        var rng    = RandomDefaults.CreateRandomSource();
        var inputs = __nn.Inputs.Span;

        for (int i = 0; i < inputs.Length; i++)
        {
            inputs[i] = rng.NextDouble();
        }
    }
Esempio n. 8
0
    private static void VerifyNeuralNetResponseInner(bool enableHardwareAcceleration)
    {
        var activationFnFactory = new DefaultActivationFunctionFactory <double>(enableHardwareAcceleration);
        var metaNeatGenome      = new MetaNeatGenome <double>(4, 1, true, activationFnFactory.GetActivationFunction("LeakyReLU"));

        // Load test genome.
        NeatGenomeLoader <double> loader = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome);
        NeatGenome <double>       genome = loader.Load("TestData/binary-three-multiplexer.genome");

        // Decode genome to a neural net.
        var genomeDecoder           = NeatGenomeDecoderFactory.CreateGenomeDecoderAcyclic();
        IBlackBox <double> blackBox = genomeDecoder.Decode(genome);

        // Evaluate the neural net.
        var evaluator = new BinaryThreeMultiplexerEvaluator();

        // Confirm the expected fitness (to a limited amount of precision to allow for small variations of floating point
        // results that can occur as a result of platform/environmental variations).
        FitnessInfo fitnessInfo = evaluator.Evaluate(blackBox);

        Assert.Equal(107.50554956432657, fitnessInfo.PrimaryFitness, 6);
    }