Example #1
0
        public void GetActivationFunction()
        {
            var fact  = new DefaultActivationFunctionFactory <double>(false);
            var actFn = fact.GetActivationFunction("ReLU");

            Assert.NotNull(actFn);
            Assert.Equal("SharpNeat.NeuralNets.Double.ActivationFunctions.ReLU", actFn.GetType().FullName);

            // Requesting the same activation function should yield the same instance.
            var actFn2 = fact.GetActivationFunction("ReLU");

            Assert.Same(actFn, actFn2);
        }
Example #2
0
        public void TestCppn()
        {
            var fact  = new DefaultActivationFunctionFactory <double>(false);
            var actFn = fact.GetActivationFunction("Gaussian");

            Assert.IsNotNull(actFn);
            Assert.AreEqual("SharpNeat.NeuralNet.Double.ActivationFunctions.Cppn.Gaussian", actFn.GetType().FullName);

            // Requesting the same activation function should yield the same instance.
            var actFn2 = fact.GetActivationFunction("Gaussian");

            Assert.AreSame(actFn, actFn2);
        }
Example #3
0
        public void TestHardwareAccelerated()
        {
            if (!Vector.IsHardwareAccelerated)
            {
                Assert.Inconclusive("Hardware accelerations not available. Hardware acceleration is available on supporting CPUs only, and only for x64 builds with optimization enabled (i.e. release builds).");
            }

            var fact  = new DefaultActivationFunctionFactory <double>(true);
            var actFn = fact.GetActivationFunction("ReLU");

            Assert.IsNotNull(actFn);
            Assert.AreEqual("SharpNeat.NeuralNet.Double.ActivationFunctions.Vectorized.ReLU", actFn.GetType().FullName);

            // Requesting the same activation function should yield the same instance.
            var actFn2 = fact.GetActivationFunction("ReLU");

            Assert.AreSame(actFn, actFn2);
        }
Example #4
0
        private static MetaNeatGenome <double> CreateMetaNeatGenome(int inputCount, int outputCount)
        {
            var activationFnFactory = new DefaultActivationFunctionFactory <double>();

            MetaNeatGenome <double> metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: inputCount,
                outputNodeCount: outputCount,
                isAcyclic: true,
                activationFn: activationFnFactory.GetActivationFunction("LeakyReLU"));

            return(metaNeatGenome);
        }
Example #5
0
        /// <summary>
        /// Create a <see cref="MetaNeatGenome{T}"/> based on the parameters supplied by an <see cref="INeatExperiment{T}"/>.
        /// </summary>
        /// <param name="neatExperiment">The neat experiment.</param>
        /// <returns>A new instance of <see cref="MetaNeatGenome{T}"/>.</returns>
        public static MetaNeatGenome <double> CreateMetaNeatGenome(INeatExperiment <double> neatExperiment)
        {
            // Resolve the configured activation function name to an activation function instance.
            var actFnFactory = new DefaultActivationFunctionFactory <double>(neatExperiment.EnableHardwareAcceleratedActivationFunctions);
            var activationFn = actFnFactory.GetActivationFunction(neatExperiment.ActivationFnName);

            var metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: neatExperiment.EvaluationScheme.InputCount,
                outputNodeCount: neatExperiment.EvaluationScheme.OutputCount,
                isAcyclic: neatExperiment.IsAcyclic,
                activationFn: activationFn,
                connectionWeightScale: neatExperiment.ConnectionWeightScale);

            return(metaNeatGenome);
        }
        /// <summary>
        /// Create a new instance of <see cref="NeatEvolutionAlgorithm{T}"/> for the given neat experiment.
        /// </summary>
        /// <param name="neatExperiment">A neat experiment; conveys everything required to create a new evolution algorithm instance that is ready to be run.</param>
        /// <returns>A new instance of <see cref="NeatEvolutionAlgorithm{T}"/>.</returns>
        public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm(
            INeatExperiment <double> neatExperiment)
        {
            // Create a genomeList evaluator based on the experiment's configuration settings.
            var genomeListEvaluator = CreateGenomeListEvaluator(neatExperiment);

            // Resolve the configured activation function name to an activation function instance.
            var actFnFactory = new DefaultActivationFunctionFactory <double>(neatExperiment.EnableHardwareAcceleratedActivationFunctions);
            var activationFn = actFnFactory.GetActivationFunction(neatExperiment.ActivationFnName);

            // Construct a MetaNeatGenome.
            var metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: neatExperiment.EvaluationScheme.InputCount,
                outputNodeCount: neatExperiment.EvaluationScheme.OutputCount,
                isAcyclic: neatExperiment.IsAcyclic,
                activationFn: activationFn);

            // Create an initial population of genomes.
            NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(
                metaNeatGenome,
                connectionsProportion : neatExperiment.InitialInterconnectionsProportion,
                popSize : neatExperiment.PopulationSize,
                rng : RandomDefaults.CreateRandomSource());

            // Create a speciation strategy based on the experiment's configuration settings.
            var speciationStrategy = CreateSpeciationStrategy(neatExperiment);

            // Create an instance of the default connection weight mutation scheme.
            var weightMutationScheme = WeightMutationSchemeFactory.CreateDefaultScheme(neatExperiment.ConnectionWeightScale);

            // Pull all of the parts together into an evolution algorithm instance.
            var ea = new NeatEvolutionAlgorithm <double>(
                neatExperiment.NeatEvolutionAlgorithmSettings,
                genomeListEvaluator,
                speciationStrategy,
                neatPop,
                neatExperiment.ComplexityRegulationStrategy,
                neatExperiment.ReproductionAsexualSettings,
                neatExperiment.ReproductionSexualSettings,
                weightMutationScheme);

            return(ea);
        }
Example #7
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);
    }