示例#1
0
        /// <summary>
        /// Mutates the genome by creating a new connection.
        /// </summary>
        public void Mutate_Link()
        {
            //Get first node to connect. It is random.
            NodeGene nodeGene_a = NodeGenes.RandomValue(Random).Take(1).ElementAt(0);


            IEnumerable <NodeGene> temp_subset = NodeGenes.Values.Where(a => a.NodeGenePattern.X > nodeGene_a.NodeGenePattern.X);

            if (temp_subset.Count() == 0)
            {
                return; //TODO handle this too
            }


            NodeGene nodeGene_b = temp_subset.ElementAt(Random.Next(temp_subset.Count()));  //Get a random gene with a higher X value.


            ConnectionGene connectionGene = Pedigree.Create_ConnectionGene(nodeGene_a, nodeGene_b, Pedigree.Mutation_WeightRandom * (Random.NextDouble() * 2 - 1),
                                                                           true);

            if (ConnectionGenes.ContainsKey(connectionGene.ConnectionGenePattern.InnovationNumber)) //Can only happen if it already existed in the tracker.
            {
                return;                                                                             //TODO think of how to handle this, maybe have a retry somewhere?
            }


            ConnectionGenes.Add(connectionGene.ConnectionGenePattern.InnovationNumber, connectionGene);
        }
示例#2
0
        /// <summary>
        /// Mutates a random connection splitting it with a node.
        /// </summary>
        public void Mutate_Node()
        {
            if (NodeGenes.Count >= Pedigree.MaxNodes)
            {
                return; //Do nothing if we have max nodes.
            }
            else if (ConnectionGenes.Count == 0)
            {
                return; //Literally cannot make a node.
            }


            ConnectionGene connectionGene = ConnectionGenes.RandomValue(Random).Take(1).ElementAt(0);

            if (connectionGene == null)
            {
                return;
            }


            NodeGene from = NodeGenes[connectionGene.ConnectionGenePattern.From.InnovationNumber];
            NodeGene to   = NodeGenes[connectionGene.ConnectionGenePattern.To.InnovationNumber];

            NodeGene created = Pedigree.Create_NodeGene(connectionGene);

            if (NodeGenes.ContainsKey(created.NodeGenePattern.InnovationNumber))
            {
                return; //TODO maybe retry here as well?
            }

            NodeGenes.Add(created.NodeGenePattern.InnovationNumber, created);


            ConnectionGene created_connectionGene_1 = Pedigree.Create_ConnectionGene(from, created, 1, true); //Default weight of 1.
            ConnectionGene created_connectionGene_2 = Pedigree.Create_ConnectionGene(created, to, connectionGene.Weight, connectionGene.Enabled);

            ConnectionGenes.Remove(connectionGene.ConnectionGenePattern.InnovationNumber);

            ConnectionGenes.Add(created_connectionGene_1.ConnectionGenePattern.InnovationNumber, created_connectionGene_1);
            ConnectionGenes.Add(created_connectionGene_2.ConnectionGenePattern.InnovationNumber, created_connectionGene_2);
        }
示例#3
0
        /// <summary>
        /// Mutates a random node by giving it a random activation function other than the one it has right now.
        /// </summary>
        public void Mutate_ActivationFunction()
        {
            NodeGene nodeGene = NodeGenes.RandomValue(Random).Take(1).ElementAt(0);

            nodeGene.ActivationFunction = Pedigree.GetRandomActivationFunction(nodeGene.ActivationFunction);
        }