private static NeatPopulation <double> CreateNeatPopulation(
            int count,
            double defaultFitness,
            IRandomSource rng)
        {
            MetaNeatGenome <double> metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: 3,
                outputNodeCount: 2,
                isAcyclic: true,
                activationFn: new NeuralNets.Double.ActivationFunctions.ReLU());

            NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(metaNeatGenome, 1.0, count, rng);

            Assert.Equal(count, neatPop.GenomeList.Count);
            Assert.Equal(count, neatPop.GenomeIdSeq.Peek);

            // Assign the default fitness to all genomes.
            var genomeList = neatPop.GenomeList;

            for (int i = 0; i < count; i++)
            {
                var genome = genomeList[i];
                genome.FitnessInfo = new FitnessInfo(defaultFitness);
            }

            // Init species.
            InitialiseSpecies(neatPop);

            return(neatPop);
        }
    /// <summary>
    /// Create a new instance of <see cref="NeatEvolutionAlgorithm{T}"/> for the given neat experiment.
    /// </summary>
    /// <param name="neatExperiment">A neat experiment instance; this 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);

        // Create a MetaNeatGenome.
        var metaNeatGenome = CreateMetaNeatGenome(neatExperiment);

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

        // 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);
    }
Exemple #3
0
        public void CreatePopulation()
        {
            MetaNeatGenome <double> metaNeatGenome = new(
                inputNodeCount : 3,
                outputNodeCount : 2,
                isAcyclic : true,
                activationFn : new NeuralNets.Double.ActivationFunctions.ReLU());

            int count = 10;
            NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(metaNeatGenome, 1.0, count, RandomDefaults.CreateRandomSource());

            Assert.Equal(count, neatPop.GenomeList.Count);
            Assert.Equal(count, neatPop.GenomeIdSeq.Peek);

            // The population factory assigns the same innovation IDs to matching structures in the genomes it creates.
            // In this test there are 5 nodes and 6 connections in each genome, and they are each identifiably
            // the same structure in each of the genomes (e.g. input 0 or whatever) and so have the same innovation ID
            // across all of the genomes.
            // Thus in total although we created N genomes there are only 5 innovation IDs allocated (5 nodes).
            Assert.Equal(5, neatPop.InnovationIdSeq.Peek);

            // Loop the created genomes.
            for (int i = 0; i < count; i++)
            {
                var genome = neatPop.GenomeList[i];
                Assert.Equal(i, genome.Id);
                Assert.Equal(0, genome.BirthGeneration);

                TestGenome(genome);
            }
        }
Exemple #4
0
    /// <summary>
    /// Create a new NeatPopulation with randomly initialised genomes.
    /// Genomes are randomly initialised by giving each a random subset of all possible connections between the input and output layer.
    /// </summary>
    /// <param name="metaNeatGenome">Genome metadata, e.g. the number of input and output nodes that each genome should have.</param>
    /// <param name="connectionsProportion">The proportion of possible connections between the input and output layers, to create in each new genome.</param>
    /// <param name="popSize">Population size. The number of new genomes to create.</param>
    /// <returns>A new instance of <see cref="NeatPopulation{T}"/>.</returns>
    public static NeatPopulation <T> CreatePopulation(
        MetaNeatGenome <T> metaNeatGenome,
        double connectionsProportion, int popSize)
    {
        var factory = new NeatPopulationFactory <T>(metaNeatGenome, connectionsProportion, RandomDefaults.CreateRandomSource());

        return(factory.CreatePopulation(popSize));
    }
        //private IGenomeCollectionEvaluator<NeatGenome<double>> CreateGenomeListEvaluator()
        //{
        //    var genomeDecoder = new NeatGenomeAcyclicDecoder(false);
        //    var phenomeEvaluator = new BinaryElevenMultiplexerEvaluator();
        //    var genomeCollectionEvaluator = new SerialGenomeListEvaluator<NeatGenome<double>, IPhenome<double>>(genomeDecoder, phenomeEvaluator);
        //    return genomeListEvaluator;
        //}

        private static NeatPopulation <double> CreatePopulation(
            MetaNeatGenome <double> metaNeatGenome,
            int popSize)
        {
            NeatPopulation <double> pop = NeatPopulationFactory <double> .CreatePopulation(
                metaNeatGenome,
                connectionsProportion : 1.0,
                popSize : popSize,
                rng : RandomDefaults.CreateRandomSource());

            return(pop);
        }
    private void btnCreateRandomPop_Click(object sender, EventArgs e)
    {
        INeatExperiment <double> neatExperiment = GetNeatExperiment();
        MetaNeatGenome <double>  metaNeatGenome = NeatUtils.CreateMetaNeatGenome(neatExperiment);

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

        // Update UI.
        UpdateUIState();
    }
Exemple #7
0
        public void CreateGenome()
        {
            var metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: 10,
                outputNodeCount: 20,
                isAcyclic: true,
                activationFn: new NeuralNets.Double.ActivationFunctions.ReLU());

            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

            int count = 100;
            NeatPopulation <double> pop = NeatPopulationFactory <double> .CreatePopulation(metaNeatGenome, 0.1, count, RandomDefaults.CreateRandomSource());

            var generationSeq = new Int32Sequence();

            var strategy = new UniformCrossoverReproductionStrategy <double>(
                pop.MetaNeatGenome.IsAcyclic,
                0.02,
                genomeBuilder,
                pop.GenomeIdSeq, generationSeq);

            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            var cyclicGraphCheck = new CyclicGraphCheck();

            for (int i = 0; i < 1000; i++)
            {
                // Randomly select two parents from the population.
                var genome1 = pop.GenomeList[rng.Next(count)];
                var genome2 = pop.GenomeList[rng.Next(count)];

                var childGenome = strategy.CreateGenome(genome1, genome2, rng);

                // The connection genes should be sorted.
                Assert.True(SortUtils.IsSortedAscending <DirectedConnection>(childGenome.ConnectionGenes._connArr));

                // The child genome should describe an acyclic graph, i.e. the new connection should not have
                // formed a cycle in the graph.
                var digraph = childGenome.DirectedGraph;
                Assert.False(cyclicGraphCheck.IsCyclic(digraph));

                // The child genome node IDs should be a superset of those from parent1 + parent2.
                var childNodeIdSet = GetNodeIdSet(childGenome);
                var parentIdSet    = GetNodeIdSet(genome1);
                parentIdSet.IntersectWith(GetNodeIdSet(genome2));

                Assert.True(childNodeIdSet.IsSupersetOf(parentIdSet));
            }
        }
Exemple #8
0
        private static NeatPopulation <double> CreateNeatPopulation(
            int count,
            int inputNodeCount,
            int outputNodeCount,
            double connectionsProportion)
        {
            MetaNeatGenome <double> metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: inputNodeCount,
                outputNodeCount: outputNodeCount,
                isAcyclic: true,
                activationFn: new SharpNeat.NeuralNet.Double.ActivationFunctions.ReLU());

            NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(metaNeatGenome, 1.0, count, RandomDefaults.CreateRandomSource());

            return(neatPop);
        }
        private static Species <double> CreateTestSpecies(int genomeCount)
        {
            // Create the species genomes; we use NeatPopulationFactory for this.
            MetaNeatGenome <double> metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: 1,
                outputNodeCount: 1,
                isAcyclic: true,
                activationFn: new SharpNeat.NeuralNets.Double.ActivationFunctions.ReLU());

            NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(metaNeatGenome, 1.0, genomeCount, RandomDefaults.CreateRandomSource());

            // Create the species object, and assign all of the created genomes to it.
            var species = new Species <double>(0, new ConnectionGenes <double>(0));

            species.GenomeList.AddRange(neatPop.GenomeList);

            // Return the species.
            return(species);
        }
        /// <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);
        }
Exemple #11
0
        public void TestInitialConnections()
        {
            MetaNeatGenome<double> metaNeatGenome = new MetaNeatGenome<double>(
                inputNodeCount: 100,
                outputNodeCount: 200,
                isAcyclic: true,
                activationFn: new SharpNeat.NeuralNet.Double.ActivationFunctions.ReLU());

            NeatPopulation<double> neatPop = NeatPopulationFactory<double>.CreatePopulation(metaNeatGenome, 0.5, 1, RandomDefaults.CreateRandomSource());
            NeatGenome<double> genome = neatPop.GenomeList[0];

            Assert.AreEqual(10000, genome.ConnectionGenes.Length);
            Assert.IsTrue(SortUtils.IsSortedAscending(genome.ConnectionGenes._connArr));

            CalcWeightMinMaxMean(genome.ConnectionGenes._weightArr, out double min, out double max, out double mean);

            Assert.IsTrue(min < -genome.MetaNeatGenome.ConnectionWeightRange * 0.98);
            Assert.IsTrue(max > genome.MetaNeatGenome.ConnectionWeightRange * 0.98);
            Assert.IsTrue(Math.Abs(mean) < 0.1);
        }
    private static NeatPopulation <double> CreateNeatPopulation(
        int populationSize,
        int speciesCount,
        int inputNodeCount,
        int outputNodeCount,
        double connectionsProportion)
    {
        MetaNeatGenome <double> metaNeatGenome = new(
            inputNodeCount : inputNodeCount,
            outputNodeCount : outputNodeCount,
            isAcyclic : true,
            activationFn : new NeuralNets.Double.ActivationFunctions.ReLU());

        NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(metaNeatGenome, connectionsProportion, populationSize, RandomDefaults.CreateRandomSource());

        neatPop.SpeciesArray = new Species <double> [speciesCount];

        for (int i = 0; i < speciesCount; i++)
        {
            neatPop.SpeciesArray[i] = new Species <double>(i, null);
        }

        return(neatPop);
    }