コード例 #1
0
        /// <summary>
        /// Create a new instance of <see cref="NeatEvolutionAlgorithm{T}"/> for the given neat experiment, and neat population.
        /// </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>
        /// <param name="neatPop">A pre constructed/loaded neat population; this must be compatible with the provided neat experiment, otherwise an exception will be thrown.</param>
        /// <returns>A new instance of <see cref="NeatEvolutionAlgorithm{T}"/>.</returns>
        public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm(
            INeatExperiment <double> neatExperiment,
            NeatPopulation <double> neatPop)
        {
            // Validate MetaNeatGenome and NeatExperiment are compatible; normally the former should have been created based on the latter, but this is not enforced.
            MetaNeatGenome <double> metaNeatGenome = neatPop.MetaNeatGenome;

            ValidateCompatible(neatExperiment, metaNeatGenome);

            // Create a genomeList evaluator based on the experiment's configuration settings.
            var genomeListEvaluator = CreateGenomeListEvaluator(neatExperiment);

            // 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);
        }
コード例 #2
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);
        }
    }
コード例 #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;
        }
    }
コード例 #4
0
        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);
        }
コード例 #5
0
    public void Simple()
    {
        var metaNeatGenome = new MetaNeatGenome <double>(3, 2, true, new ReLU());
        var genomeBuilder  = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

        // Simple acyclic graph.
        var connGenes = new ConnectionGenes <double>(4);

        connGenes[0] = (0, 3, 0.0);
        connGenes[1] = (1, 3, 1.0);
        connGenes[2] = (2, 3, 2.0);
        connGenes[3] = (2, 4, 3.0);

        // Wrap in a genome.
        var genome = genomeBuilder.Create(0, 0, connGenes);

        // Note. The genome builder creates a digraph representation of the genome and attaches/caches it on the genome object.
        var acyclicDigraph = (DirectedGraphAcyclic)genome.DirectedGraph;

        Assert.NotNull(acyclicDigraph);

        // The graph should be unchanged from the input connections.
        CompareConnectionLists(connGenes, acyclicDigraph.ConnectionIds, genome.ConnectionIndexMap);

        // Check the node count.
        Assert.Equal(5, acyclicDigraph.TotalNodeCount);
    }
コード例 #6
0
 /// <summary>
 /// Construct with the given NEAT genome metadata.
 /// </summary>
 /// <param name="metaNeatGenome">NEAT genome metadata.</param>
 /// <param name="validateAcyclic">Enable acyclic graph validation.</param>
 /// <remarks>
 /// If the caller can guarantee that calls to Create() will provide acyclic graphs only, then
 /// <paramref name="validateAcyclic"/> can be set to false to avoid the cost of the cyclic graph check (which is relatively expensive to perform).
 /// </remarks>
 public NeatGenomeBuilderAcyclic(MetaNeatGenome <T> metaNeatGenome, bool validateAcyclic)
 {
     Debug.Assert(metaNeatGenome is not null && metaNeatGenome.IsAcyclic);
     _metaNeatGenome     = metaNeatGenome;
     _graphDepthAnalysis = new AcyclicGraphDepthAnalysis(validateAcyclic);
     _workingIdSet       = new HashSet <int>();
 }
コード例 #7
0
        public void Simple_DefinedNodes()
        {
            var metaNeatGenome = new MetaNeatGenome <double>(0, 10, false, new ReLU());
            var genomeBuilder  = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

            // Simple acyclic graph.
            var connGenes = new ConnectionGenes <double>(4);

            connGenes[0] = (10, 13, 0.0);
            connGenes[1] = (11, 13, 1.0);
            connGenes[2] = (12, 13, 2.0);
            connGenes[3] = (12, 14, 3.0);

            // Wrap in a genome.
            var genome = genomeBuilder.Create(0, 0, connGenes);

            // Note. The genome builder creates a digraph representation of the genome and attaches/caches it on the genome object.
            var digraph = genome.DirectedGraph;

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connGenes, digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.AreEqual(15, digraph.TotalNodeCount);
        }
コード例 #8
0
        /// <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));
        }
コード例 #9
0
        public void Simple_DefinedNodes_NodeIdGap()
        {
            var metaNeatGenome = new MetaNeatGenome <double>(0, 10, false, new ReLU());
            var genomeBuilder  = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

            // Simple acyclic graph.
            var connGenes = new ConnectionGenes <double>(4);

            connGenes[0] = (100, 103, 0.0);
            connGenes[1] = (101, 103, 1.0);
            connGenes[2] = (102, 103, 2.0);
            connGenes[3] = (102, 104, 3.0);

            // Wrap in a genome.
            var genome = genomeBuilder.Create(0, 0, connGenes);

            // Note. The genome builder creates a digraph representation of the genome and attaches/caches it on the genome object.
            var digraph = genome.DirectedGraph;

            // The gaps in the node IDs should be removed such that node IDs form a contiguous span starting from zero.
            var connArrExpected   = new DirectedConnection[4];
            var weightArrExpected = new double[4];

            connArrExpected[0] = new DirectedConnection(10, 13); weightArrExpected[0] = 0.0;
            connArrExpected[1] = new DirectedConnection(11, 13); weightArrExpected[1] = 1.0;
            connArrExpected[2] = new DirectedConnection(12, 13); weightArrExpected[2] = 2.0;
            connArrExpected[3] = new DirectedConnection(12, 14); weightArrExpected[3] = 3.0;

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connArrExpected, digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.AreEqual(15, digraph.TotalNodeCount);
        }
コード例 #10
0
        public static NeatGenome <double> CreateNeatGenome(
            MetaNeatGenome <double> metaNeatGenome,
            INeatGenomeBuilder <double> genomeBuilder)
        {
            var connGenes = new ConnectionGenes <double>(12);

            connGenes[0] = (0, 3, 0.1);
            connGenes[1] = (0, 4, 0.2);
            connGenes[2] = (0, 5, 0.3);

            connGenes[3] = (3, 6, 0.4);
            connGenes[4] = (4, 7, 0.5);
            connGenes[5] = (5, 8, 0.6);

            connGenes[6] = (6, 9, 0.7);
            connGenes[7] = (7, 10, 0.8);
            connGenes[8] = (8, 11, 0.9);

            connGenes[9]  = (9, 1, 1.0);
            connGenes[10] = (10, 1, 1.1);
            connGenes[11] = (11, 1, 1.2);

            var genome = genomeBuilder.Create(0, 0, connGenes);

            return(genome);
        }
コード例 #11
0
        private NeatGenome <double> CreateGenome1(MetaNeatGenome <double> metaNeatGenome)
        {
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

            // Define a genome that matches the one defined in example1.genome.
            var connGenes = new ConnectionGenes <double>(12);

            connGenes[0]  = (0, 5, 0.5);
            connGenes[1]  = (0, 7, 0.7);
            connGenes[2]  = (0, 3, 0.3);
            connGenes[3]  = (1, 5, 1.5);
            connGenes[4]  = (1, 7, 1.7);
            connGenes[5]  = (1, 3, 1.3);
            connGenes[6]  = (1, 6, 1.6);
            connGenes[7]  = (1, 8, 1.8);
            connGenes[8]  = (1, 4, 1.4);
            connGenes[9]  = (2, 6, 2.6);
            connGenes[10] = (2, 8, 2.8);
            connGenes[11] = (2, 4, 2.4);

            // Ensure the connections are sorted correctly.
            connGenes.Sort();

            // Wrap in a genome.
            NeatGenome <double> genome = genomeBuilder.Create(0, 0, connGenes);

            return(genome);
        }
コード例 #12
0
    /// <summary>
    /// Create a digraph from the a set of connection genes.
    /// </summary>
    /// <typeparam name="T">Neural net numeric data type.</typeparam>
    /// <param name="metaNeatGenome">Genome metadata.</param>
    /// <param name="connGenes">Connection genes.</param>
    /// <param name="nodeIndexByIdMap">A mapping from node IDs to node indexes.</param>
    /// <returns>A new instance of <see cref="DirectedGraph"/>.</returns>
    public static DirectedGraph CreateDirectedGraph <T>(
        MetaNeatGenome <T> metaNeatGenome,
        ConnectionGenes <T> connGenes,
        INodeIdMap nodeIndexByIdMap)
        where T : struct
    {
        // Extract/copy the neat genome connectivity graph into a ConnectionIds structure.
        // Notes.
        // The array contents will be manipulated, so copying this avoids modification of the genome's
        // connection gene list.
        // The IDs are substituted for node indexes here.
        CopyAndMapIds(
            connGenes._connArr,
            nodeIndexByIdMap,
            out ConnectionIds connIds);

        // Construct a new DirectedGraph.
        var digraph = new DirectedGraph(
            metaNeatGenome.InputNodeCount,
            metaNeatGenome.OutputNodeCount,
            nodeIndexByIdMap.Count,
            connIds);

        return(digraph);
    }
コード例 #13
0
        /// <summary>
        /// Construct a new instance.
        /// </summary>
        /// <param name="metaNeatGenome">NeatGenome metadata.</param>
        /// <param name="genomeBuilder">NeatGenome builder.</param>
        /// <param name="genomeIdSeq">Genome ID sequence; for obtaining new genome IDs.</param>
        /// <param name="innovationIdSeq">Innovation ID sequence; for obtaining new innovation IDs.</param>
        /// <param name="generationSeq">Generation sequence; for obtaining the current generation number.</param>
        /// <param name="addedNodeBuffer">A history buffer of added nodes.</param>
        /// <param name="settings">Asexual reproduction settings.</param>
        /// <param name="weightMutationScheme">Connection weight mutation scheme.</param>
        public NeatReproductionAsexual(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            Int32Sequence genomeIdSeq,
            Int32Sequence innovationIdSeq,
            Int32Sequence generationSeq,
            AddedNodeBuffer addedNodeBuffer,
            NeatReproductionAsexualSettings settings,
            WeightMutationScheme <T> weightMutationScheme)
        {
            _settings = settings;

            // Instantiate reproduction strategies.
            _mutateWeightsStrategy    = new MutateWeightsStrategy <T>(metaNeatGenome, genomeBuilder, genomeIdSeq, generationSeq, weightMutationScheme);
            _deleteConnectionStrategy = new DeleteConnectionStrategy <T>(metaNeatGenome, genomeBuilder, genomeIdSeq, generationSeq);

            // Add connection mutation; select acyclic/cyclic strategy as appropriate.
            if (metaNeatGenome.IsAcyclic)
            {
                _addConnectionStrategy = new AddAcyclicConnectionStrategy <T>(
                    metaNeatGenome, genomeBuilder,
                    genomeIdSeq, innovationIdSeq, generationSeq);
            }
            else
            {
                _addConnectionStrategy = new AddCyclicConnectionStrategy <T>(
                    metaNeatGenome, genomeBuilder,
                    genomeIdSeq, innovationIdSeq, generationSeq);
            }

            _addNodeStrategy = new AddNodeStrategy <T>(metaNeatGenome, genomeBuilder, genomeIdSeq, innovationIdSeq, generationSeq, addedNodeBuffer);
        }
コード例 #14
0
        public void CreatePopulation()
        {
            MetaNeatGenome <double> metaNeatGenome = new MetaNeatGenome <double>(
                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);
            }
        }
コード例 #15
0
 public NeatPopulation(
     MetaNeatGenome <T> metaNeatGenome,
     List <NeatGenome <T> > genomeList,
     Int32Sequence genomeIdSeq,
     Int32Sequence innovationIdSeq)
     : this(metaNeatGenome, genomeList, genomeIdSeq, innovationIdSeq, __defaultInnovationHistoryBufferSize, __defaultInnovationHistoryBufferSize)
 {
 }
コード例 #16
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));
    }
コード例 #17
0
    public void DepthNodeReorderTest()
    {
        var metaNeatGenome = new MetaNeatGenome <double>(2, 2, true, new ReLU());
        var genomeBuilder  = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

        // Define graph connections.
        var connGenes = new ConnectionGenes <double>(5);

        connGenes[0] = (0, 4, 0.0);
        connGenes[1] = (4, 5, 1.0);
        connGenes[2] = (5, 2, 2.0);
        connGenes[3] = (1, 2, 3.0);
        connGenes[4] = (2, 3, 4.0);
        connGenes.Sort();

        // Wrap in a genome.
        var genome = genomeBuilder.Create(0, 0, connGenes);

        // Note. The genome builder creates a digraph representation of the genome and attaches/caches it on the genome object.
        var acyclicDigraph = (DirectedGraphAcyclic)genome.DirectedGraph;

        Assert.NotNull(acyclicDigraph);

        // Simulate the actual weight array that would occur in e.g. a WeightedDirectedGraphAcyclic or NeuralNetAcyclic.
        double[] weightArrActual = new double[connGenes._weightArr.Length];
        for (int i = 0; i < weightArrActual.Length; i++)
        {
            weightArrActual[i] = connGenes._weightArr[genome.ConnectionIndexMap[i]];
        }

        // The nodes should have IDs allocated based on depth, i.e. the layer they are in.
        // And connections should be ordered by source node ID.
        var connArrExpected   = new DirectedConnection[5];
        var weightArrExpected = new double[5];

        connArrExpected[0] = new DirectedConnection(0, 2); weightArrExpected[0] = 0.0;
        connArrExpected[1] = new DirectedConnection(1, 4); weightArrExpected[1] = 3.0;
        connArrExpected[2] = new DirectedConnection(2, 3); weightArrExpected[2] = 1.0;
        connArrExpected[3] = new DirectedConnection(3, 4); weightArrExpected[3] = 2.0;
        connArrExpected[4] = new DirectedConnection(4, 5); weightArrExpected[4] = 4.0;

        // Compare actual and expected connections.
        CompareConnectionLists(connArrExpected, weightArrExpected, acyclicDigraph.ConnectionIds, weightArrActual);

        // Test layer info.
        LayerInfo[] layerArrExpected = new LayerInfo[5];
        layerArrExpected[0] = new LayerInfo(2, 2);
        layerArrExpected[1] = new LayerInfo(3, 3);
        layerArrExpected[2] = new LayerInfo(4, 4);
        layerArrExpected[3] = new LayerInfo(5, 5);
        layerArrExpected[4] = new LayerInfo(6, 5);
        Assert.Equal(5, acyclicDigraph.LayerArray.Length);

        // Check the node count.
        Assert.Equal(6, acyclicDigraph.TotalNodeCount);
    }
コード例 #18
0
        private static MetaNeatGenome <double> CreateMetaNeatGenome()
        {
            MetaNeatGenome <double> metaNeatGenome = new MetaNeatGenome <double>(
                inputNodeCount: 3,
                outputNodeCount: 1,
                isAcyclic: true,
                activationFn: new SharpNeat.NeuralNet.Double.ActivationFunctions.ReLU());

            return(metaNeatGenome);
        }
コード例 #19
0
 /// <summary>
 /// Create a new instance of <see cref="INeatGenomeBuilder{T}"/>.
 /// </summary>
 /// <param name="metaNeatGenome">Neat genome metadata.</param>
 /// <returns>A new instance of <see cref="INeatGenomeBuilder{T}"/>.</returns>
 public static INeatGenomeBuilder <T> Create(
     MetaNeatGenome <T> metaNeatGenome)
 {
     if (metaNeatGenome.IsAcyclic)
     {
         return(new NeatGenomeAcyclicBuilder <T>(metaNeatGenome));
     }
     // else
     return(new NeatGenomeBuilder <T>(metaNeatGenome));
 }
コード例 #20
0
 /// <summary>
 /// Create a new instance of <see cref="INeatGenomeBuilder{T}"/>.
 /// </summary>
 /// <param name="metaNeatGenome">Neat genome metadata.</param>
 /// <param name="validateAcyclic">Enable acyclic graph validation.</param>
 /// <returns>A new instance of <see cref="INeatGenomeBuilder{T}"/>.</returns>
 /// <remarks>
 /// If the caller can guarantee that calls to Create() will provide acyclic graphs only when metaNeatGenome.IsAcyclic is true, then
 /// <paramref name="validateAcyclic"/> can be set to false to avoid the cost of the cyclic graph check (which is relatively expensive to perform).
 /// </remarks>
 public static INeatGenomeBuilder <T> Create(
     MetaNeatGenome <T> metaNeatGenome,
     bool validateAcyclic = false)
 {
     if (metaNeatGenome.IsAcyclic)
     {
         return(new NeatGenomeBuilderAcyclic <T>(metaNeatGenome, validateAcyclic));
     }
     // else
     return(new NeatGenomeBuilderCyclic <T>(metaNeatGenome));
 }
コード例 #21
0
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <param name="metaNeatGenome">NEAT genome metadata.</param>
 /// <param name="genomeBuilder">NeatGenome builder.</param>
 /// <param name="genomeIdSeq">Genome ID sequence; for obtaining new genome IDs.</param>
 /// <param name="generationSeq">Generation sequence; for obtaining the current generation number.</param>
 public DeleteConnectionStrategy(
     MetaNeatGenome <T> metaNeatGenome,
     INeatGenomeBuilder <T> genomeBuilder,
     Int32Sequence genomeIdSeq,
     Int32Sequence generationSeq)
 {
     _metaNeatGenome = metaNeatGenome;
     _genomeBuilder  = genomeBuilder;
     _genomeIdSeq    = genomeIdSeq;
     _generationSeq  = generationSeq;
 }
コード例 #22
0
        //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);
        }
コード例 #23
0
        protected NeatGenomeLoader(
            MetaNeatGenome <T> metaNeatGenome,
            int connCountEstimate)
        {
            _metaNeatGenome = metaNeatGenome ?? throw new ArgumentNullException(nameof(metaNeatGenome));
            _genomeBuilder  = NeatGenomeBuilderFactory <T> .Create(metaNeatGenome);

            _activationFnName = metaNeatGenome.ActivationFn.GetType().Name;
            _connList         = new List <DirectedConnection>(connCountEstimate);
            _weightList       = new List <T>(connCountEstimate);
            _actFnList        = new List <ActivationFunctionRow>();
        }
コード例 #24
0
        public NeatPopulation(
            MetaNeatGenome <T> metaNeatGenome,
            List <NeatGenome <T> > genomeList)
            : base(genomeList)
        {
            GetMaxObservedIds(genomeList, out int maxGenomeId, out int maxInnovationId);

            this.MetaNeatGenome  = metaNeatGenome;
            this.GenomeIdSeq     = new Int32Sequence(maxGenomeId + 1);
            this.InnovationIdSeq = new Int32Sequence(maxInnovationId + 1);
            this.AddedNodeBuffer = new AddedNodeBuffer(__defaultInnovationHistoryBufferSize);
        }
コード例 #25
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);
        }
コード例 #26
0
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <param name="metaNeatGenome">NeatGenome metadata.</param>
 /// <param name="genomeBuilder">NeatGenome builder.</param>
 /// <param name="genomeIdSeq">Genome ID sequence; for obtaining new genome IDs.</param>
 /// <param name="generationSeq">Generation sequence; for obtaining the current generation number.</param>
 /// <param name="settings">Sexual reproduction settings.</param>
 public NeatReproductionSexual(
     MetaNeatGenome <T> metaNeatGenome,
     INeatGenomeBuilder <T> genomeBuilder,
     Int32Sequence genomeIdSeq,
     Int32Sequence generationSeq,
     NeatReproductionSexualSettings settings)
 {
     _strategy = new UniformCrossoverReproductionStrategy <T>(
         metaNeatGenome.IsAcyclic,
         settings.SecondaryParentGeneProbability,
         genomeBuilder,
         genomeIdSeq, generationSeq);
 }
コード例 #27
0
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <param name="metaNeatGenome">NEAT genome metadata.</param>
 /// <param name="genomeBuilder">NeatGenome builder.</param>
 /// <param name="genomeIdSeq">Genome ID sequence; for obtaining new genome IDs.</param>
 /// <param name="generationSeq">Generation sequence; for obtaining the current generation number.</param>
 /// <param name="weightMutationScheme">Connection weight mutation scheme.</param>
 public MutateWeightsStrategy(
     MetaNeatGenome <T> metaNeatGenome,
     INeatGenomeBuilder <T> genomeBuilder,
     Int32Sequence genomeIdSeq,
     Int32Sequence generationSeq,
     WeightMutationScheme <T> weightMutationScheme)
 {
     _metaNeatGenome       = metaNeatGenome;
     _genomeBuilder        = genomeBuilder;
     _genomeIdSeq          = genomeIdSeq;
     _generationSeq        = generationSeq;
     _weightMutationScheme = weightMutationScheme;
 }
コード例 #28
0
    private static void AssertNodeCounts(
        MetaNeatGenome <T> metaNeatGenome,
        int[] hiddenNodeIdArr,
        INodeIdMap nodeIndexByIdMap,
        DirectedGraph digraph)
    {
        int totalNodeCount = metaNeatGenome.InputNodeCount + metaNeatGenome.OutputNodeCount + hiddenNodeIdArr.Length;

        Debug.Assert(digraph.InputCount == metaNeatGenome.InputNodeCount);
        Debug.Assert(digraph.OutputCount == metaNeatGenome.OutputNodeCount);
        Debug.Assert(digraph.TotalNodeCount == totalNodeCount);
        Debug.Assert(nodeIndexByIdMap.Count == totalNodeCount);
    }
コード例 #29
0
    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();
    }
コード例 #30
0
        /// <summary>
        /// Construct a new population with the provided genomes.
        /// </summary>
        /// <param name="metaNeatGenome">NeatGenome metadata.</param>
        /// <param name="genomeBuilder">NeatGenome builder.</param>
        /// <param name="genomeList">A list of genomes that will make up the population.</param>
        public NeatPopulation(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            List <NeatGenome <T> > genomeList)
            : base(genomeList)
        {
            GetMaxObservedIds(genomeList, out int maxGenomeId, out int maxInnovationId);

            this.MetaNeatGenome  = metaNeatGenome ?? throw new ArgumentNullException(nameof(metaNeatGenome));
            this.GenomeBuilder   = genomeBuilder ?? throw new ArgumentNullException(nameof(genomeBuilder));
            this.GenomeIdSeq     = new Int32Sequence(maxGenomeId + 1);
            this.InnovationIdSeq = new Int32Sequence(maxInnovationId + 1);
            this.AddedNodeBuffer = new AddedNodeBuffer(__defaultInnovationHistoryBufferSize);
        }