/// <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);
        }
Ejemplo n.º 2
0
        public static NeatGenome <double> CreateNeatGenome2(
            INeatGenomeBuilder <double> genomeBuilder)
        {
            var connGenes = new ConnectionGenes <double>(12);

            connGenes[0] = (0, 3, 0.3);
            connGenes[1] = (0, 4, 0.4);
            connGenes[2] = (0, 5, 0.5);

            connGenes[3] = (3, 6, 3.6);
            connGenes[4] = (4, 7, 4.7);
            connGenes[5] = (5, 8, 5.8);

            connGenes[6] = (6, 9, 6.9);
            connGenes[7] = (7, 10, 7.1);
            connGenes[8] = (8, 11, 8.11);

            connGenes[9]  = (9, 1, 9.1);
            connGenes[10] = (10, 1, 10.1);
            connGenes[11] = (11, 1, 11.1);

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

            return(genome);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Construct a new population with the provided genomes and accompanying objects.
 /// </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>
 /// <param name="genomeIdSeq">Genome ID sequence.</param>
 /// <param name="innovationIdSeq">Innovation ID sequence.</param>
 public NeatPopulation(
     MetaNeatGenome <T> metaNeatGenome,
     INeatGenomeBuilder <T> genomeBuilder,
     List <NeatGenome <T> > genomeList,
     Int32Sequence genomeIdSeq,
     Int32Sequence innovationIdSeq)
     : this(metaNeatGenome, genomeBuilder, genomeList, genomeIdSeq, innovationIdSeq, __defaultInnovationHistoryBufferSize)
 {
 }
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <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(
     INeatGenomeBuilder <T> genomeBuilder,
     Int32Sequence genomeIdSeq,
     Int32Sequence generationSeq)
 {
     _genomeBuilder = genomeBuilder;
     _genomeIdSeq   = genomeIdSeq;
     _generationSeq = generationSeq;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Construct a new instance.
 /// </summary>
 /// <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(
     INeatGenomeBuilder <T> genomeBuilder,
     Int32Sequence genomeIdSeq,
     Int32Sequence generationSeq,
     WeightMutationScheme <T> weightMutationScheme)
 {
     _genomeBuilder        = genomeBuilder;
     _genomeIdSeq          = genomeIdSeq;
     _generationSeq        = generationSeq;
     _weightMutationScheme = weightMutationScheme;
 }
 /// <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;
 }
Ejemplo n.º 8
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>();
        }
 /// <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);
 }
        /// <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 AddCyclicConnectionStrategy(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            Int32Sequence genomeIdSeq,
            Int32Sequence generationSeq)
        {
            _metaNeatGenome = metaNeatGenome;
            _genomeBuilder  = genomeBuilder;
            _genomeIdSeq    = genomeIdSeq;
            _generationSeq  = generationSeq;

            _weightSamplerA = UniformDistributionSamplerFactory.CreateStatelessSampler <T>(metaNeatGenome.ConnectionWeightScale, true);
            _weightSamplerB = UniformDistributionSamplerFactory.CreateStatelessSampler <T>(metaNeatGenome.ConnectionWeightScale * 0.01, true);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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="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>
 public AddNodeStrategy(
     MetaNeatGenome <T> metaNeatGenome,
     INeatGenomeBuilder <T> genomeBuilder,
     Int32Sequence genomeIdSeq,
     Int32Sequence innovationIdSeq,
     Int32Sequence generationSeq,
     AddedNodeBuffer addedNodeBuffer)
 {
     _metaNeatGenome  = metaNeatGenome;
     _genomeBuilder   = genomeBuilder;
     _genomeIdSeq     = genomeIdSeq;
     _innovationIdSeq = innovationIdSeq;
     _generationSeq   = generationSeq;
     _addedNodeBuffer = addedNodeBuffer;
 }
Ejemplo n.º 13
0
    /// <summary>
    /// Construct with the given strategy arguments.
    /// </summary>
    /// <param name="isAcyclic">Indicates that the strategy will be operating on acyclic graphs/genomes.</param>
    /// <param name="secondaryParentGeneProbability">The probability that a gene that exists only on the secondary parent is copied into the child genome.</param>
    /// <param name="genomeBuilder">A neat genome builder.</param>
    /// <param name="genomeIdSeq">Genome ID sequence; for obtaining new genome IDs.</param>
    /// <param name="generationSeq">A sequence that provides the current generation number.</param>
    public UniformCrossoverReproductionStrategy(
        bool isAcyclic,
        double secondaryParentGeneProbability,
        INeatGenomeBuilder <T> genomeBuilder,
        Int32Sequence genomeIdSeq,
        Int32Sequence generationSeq)
    {
        _isAcyclic = isAcyclic;
        _secondaryParentGeneProbability = secondaryParentGeneProbability;
        _genomeBuilder = genomeBuilder;
        _genomeIdSeq   = genomeIdSeq;
        _generationSeq = generationSeq;

        _connGeneListBuilder = new ConnectionGeneListBuilder <T>(_isAcyclic, 1024);
    }
Ejemplo n.º 14
0
        public UniformCrossoverReproductionStrategy(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            Int32Sequence genomeIdSeq,
            Int32Sequence generationSeq,
            IRandomSource rng)
        {
            _metaNeatGenome = metaNeatGenome;
            _genomeBuilder  = genomeBuilder;
            _genomeIdSeq    = genomeIdSeq;
            _generationSeq  = generationSeq;

            _rng     = rng;
            _builder = new ConnectionGeneListBuilder <T>(_metaNeatGenome.IsAcyclic, 1024);
        }
        public NeatReproductionSexual(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            Int32Sequence genomeIdSeq,
            Int32Sequence innovationIdSeq,
            Int32Sequence generationSeq,
            AddedNodeBuffer addedNodeBuffer,
            NeatReproductionSexualSettings settings,
            IRandomSourceBuilder rngBuilder)
        {
            _settings = settings;
            _rng      = rngBuilder.Create();

            _strategy = new UniformCrossoverReproductionStrategy <T>(
                metaNeatGenome, genomeBuilder,
                genomeIdSeq, generationSeq,
                rngBuilder.Create());
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Construct a new population with the provided genomes and accompanying objects.
        /// </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>
        /// <param name="genomeIdSeq">Genome ID sequence.</param>
        /// <param name="innovationIdSeq">Innovation ID sequence.</param>
        /// <param name="addedNodeHistoryBufferSize">The size to allocate for the added node history buffer.</param>
        public NeatPopulation(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            List <NeatGenome <T> > genomeList,
            Int32Sequence genomeIdSeq,
            Int32Sequence innovationIdSeq,
            int addedNodeHistoryBufferSize)
            : base(genomeList)
        {
            this.MetaNeatGenome  = metaNeatGenome ?? throw new ArgumentNullException(nameof(metaNeatGenome));
            this.GenomeBuilder   = genomeBuilder ?? throw new ArgumentNullException(nameof(genomeBuilder));
            this.GenomeIdSeq     = genomeIdSeq ?? throw new ArgumentNullException(nameof(genomeIdSeq));
            this.InnovationIdSeq = innovationIdSeq ?? throw new ArgumentNullException(nameof(innovationIdSeq));
            this.AddedNodeBuffer = new AddedNodeBuffer(addedNodeHistoryBufferSize);

            // Assert that the ID sequence objects represent an ID higher than any existing ID used by the genomes.
            Debug.Assert(ValidateIdSequences(genomeList, genomeIdSeq, innovationIdSeq));
        }
        public AddCyclicConnectionStrategy(
            MetaNeatGenome <T> metaNeatGenome,
            INeatGenomeBuilder <T> genomeBuilder,
            Int32Sequence genomeIdSeq,
            Int32Sequence innovationIdSeq,
            Int32Sequence generationSeq,
            IRandomSource rng)
        {
            _metaNeatGenome  = metaNeatGenome;
            _genomeBuilder   = genomeBuilder;
            _genomeIdSeq     = genomeIdSeq;
            _innovationIdSeq = innovationIdSeq;
            _generationSeq   = generationSeq;

            _weightDistA = ContinuousDistributionFactory.CreateUniformDistribution <T>(_metaNeatGenome.ConnectionWeightRange, true);
            _weightDistB = ContinuousDistributionFactory.CreateUniformDistribution <T>(_metaNeatGenome.ConnectionWeightRange * 0.01, true);
            _rng         = rng;
        }
        private NeatPopulationFactory(
            MetaNeatGenome <T> metaNeatGenome,
            double connectionsProportion,
            IRandomSource rng)
        {
            _metaNeatGenome = metaNeatGenome;
            _genomeBuilder  = NeatGenomeBuilderFactory <T> .Create(metaNeatGenome);

            _connectionsProportion = connectionsProportion;

            // Define the set of all possible connections between the input and output nodes (fully interconnected).
            int inputCount  = metaNeatGenome.InputNodeCount;
            int outputCount = metaNeatGenome.OutputNodeCount;

            _connectionDefArr = new DirectedConnection[inputCount * outputCount];

            // Notes.
            // Nodes are assigned innovation IDs. By convention the input nodes are assigned IDs first starting at zero, then the output nodes.
            // Thus, because all of the evolved networks have a fixed number of inputs and outputs, the IDs of these nodes are always fixed.
            int firstOutputNodeId = inputCount;

            for (int srcId = 0, i = 0; srcId < inputCount; srcId++)
            {
                for (int tgtIdx = 0; tgtIdx < outputCount; tgtIdx++)
                {
                    _connectionDefArr[i++] = new DirectedConnection(srcId, firstOutputNodeId + tgtIdx);
                }
            }

            // Init RNG and ID sequences.
            _rng         = rng;
            _genomeIdSeq = new Int32Sequence();
            int nextInnovationId = inputCount + outputCount;

            _innovationIdSeq = new Int32Sequence(nextInnovationId);

            // Init random connection weight source.
            _connWeightDist = ContinuousDistributionFactory.CreateUniformDistribution <T>(_metaNeatGenome.ConnectionWeightRange, true);
        }
Ejemplo n.º 19
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)
        {
            var settingsComplexifying = settings;
            var settingsSimplifying   = settings.CreateSimplifyingSettings();

            _mutationTypeDistributionsComplexifying = new MutationTypeDistributions(settingsComplexifying);
            _mutationTypeDistributionsSimplifying   = new MutationTypeDistributions(settingsSimplifying);
            _mutationTypeDistributionsCurrent       = _mutationTypeDistributionsComplexifying;

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

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

            _addNodeStrategy = new AddNodeStrategy <T>(metaNeatGenome, genomeBuilder, genomeIdSeq, innovationIdSeq, generationSeq, addedNodeBuffer);
        }
Ejemplo n.º 20
0
 static UniformCrossoverReproductionStrategyTestsUtils()
 {
     __metaNeatGenome = CreateMetaNeatGenome();
     __genomeBuilder  = NeatGenomeBuilderFactory <double> .Create(__metaNeatGenome);
 }