Exemple #1
0
        /// <summary>
        /// Internal invocation the mutation operator.
        /// </summary>
        /// <param name="chromosome">The chromosome.</param>
        /// <returns>The new DNA.</returns>
        protected override IChromosome InternalInvoke(NeuralChromosome chromosome)
        {
            var innovationTracker = NeuralChromosome.GetInnovationTracker(chromosome.InnovationTrackerName);
            var genes             = chromosome.CloneGenes();
            var nodeGenes         = genes.Item1;
            var edgeGenes         = genes.Item2;

            //No edge genes in the genotype yet. Return original chromosome.
            if (edgeGenes.Count == 0)
            {
                return(new NeuralChromosome(nodeGenes, edgeGenes, chromosome.InnovationTrackerName,
                                            chromosome.C1, chromosome.C2, chromosome.C3, chromosome.ActivationFunction));
            }

            var index        = Session.Instance.Stochastic.Next(edgeGenes.Count);
            var selectedEdge = edgeGenes[index];
            var sourceNode   = selectedEdge.Input;
            var targetNode   = selectedEdge.Output;

            selectedEdge.Enabled = false;

            var nodeId  = innovationTracker.GetNextNodeInnovationNumber(selectedEdge.ID);
            var newNode = new NeuralChromosome.NodeGene(nodeId, NeuralChromosome.NodeGene.NodeType.Hidden);

            var edgeId1  = innovationTracker.GetNextEdgeInnovationNumber(sourceNode.ID, newNode.ID);
            var newEdge1 = new NeuralChromosome.EdgeGene(edgeId1, selectedEdge.Input, newNode, 1, true);

            var edgeId2  = innovationTracker.GetNextEdgeInnovationNumber(newNode.ID, targetNode.ID);
            var newEdge2 = new NeuralChromosome.EdgeGene(edgeId2, newNode, selectedEdge.Output, selectedEdge.Weight, true);

            nodeGenes.Add(newNode);
            edgeGenes.Add(newEdge1);
            edgeGenes.Add(newEdge2);

            return(new NeuralChromosome(nodeGenes, edgeGenes, chromosome.InnovationTrackerName,
                                        chromosome.C1, chromosome.C2, chromosome.C3, chromosome.ActivationFunction));
        }
Exemple #2
0
 /// <summary>
 /// Constructs an edge gene.
 /// </summary>
 /// <remarks>This constructor is for neural chromosomes.</remarks>
 /// <param name="id">The edge gene id.</param>
 /// <param name="input">The input node gene.</param>
 /// <param name="output">The output node gene.</param>
 /// <param name="weight">The edge genge weight.</param>
 /// <param name="enabled">A value indicating if the edge gene is enabled.</param>
 /// <returns></returns>
 public static NeuralChromosome.EdgeGene ConstructEdgeGene(uint id,
                                                           NeuralChromosome.NodeGene input, NeuralChromosome.NodeGene output, double weight, bool enabled)
 {
     return(new NeuralChromosome.EdgeGene(id, input, output, weight, enabled));
 }