Ejemplo n.º 1
0
        /// <summary>
        /// Mutates a genome by breaking a connection up into two separate connections.
        /// </summary>
        /// <param name="genome">The genome to be modified.</param>
        /// <param name="innovationsSeen">A list of previously seen innovations.</param>
        /// <param name="rando">A random number generator.</param>
        public static void MutateAddNode(Genome genome, InnovationPool innovationsSeen, Random rando)
        {
            List <Gene> possibleConnections = new List <Gene>(genome.Genes);

            possibleConnections.RemoveAll(x => x.Frozen || NodePool.FindById(x.link.InNode).Type == NodeType.BIAS);

            if (possibleConnections.Count == 0)
            {
                return;
            }

            //TODO: Note in original algorithm saying uniform distribution is not optimal here.
            Gene geneToSplit = possibleConnections[rando.Next(possibleConnections.Count)];

            geneToSplit.Frozen = true;

            ActivationStyle       style      = ActivationFunctions.ChooseActivationStyle(rando);
            InnovationInformation innovation = new InnovationInformation(geneToSplit.link, style);

            int firstConId  = -1;
            int secondConId = -1;

            int registeredInnovationId = innovationsSeen.FindByInnovation(innovation);

            if (registeredInnovationId == -1)
            {
                int newNodeId = NodePool.Add(new NodeInformation(NodeType.HIDDEN, style));

                ConnectionInformation firstConnect = new ConnectionInformation(geneToSplit.link.InNode, newNodeId);
                firstConId = ConnectionPool.Add(firstConnect);

                ConnectionInformation secondConnect = new ConnectionInformation(newNodeId, geneToSplit.link.OutNode);
                secondConId = ConnectionPool.Add(secondConnect);

                innovation.NewNodeDetails.NewNodeId          = newNodeId;
                innovation.NewNodeDetails.FirstConnectionId  = firstConId;
                innovation.NewNodeDetails.SecondConnectionId = secondConId;
                innovationsSeen.Add(innovation);
            }
            else
            {
                InnovationInformation registeredInnovation = innovationsSeen.FindById(registeredInnovationId);
                firstConId  = registeredInnovation.NewNodeDetails.FirstConnectionId;
                secondConId = registeredInnovation.NewNodeDetails.SecondConnectionId;
            }

            genome.Genes.Add(new Gene(ConnectionPool.FindById(firstConId), firstConId, 1.0, false));
            genome.Genes.Add(new Gene(ConnectionPool.FindById(secondConId), secondConId, geneToSplit.Weight, false));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieves the activation function from the given style.
        /// </summary>
        /// <param name="style">The style corresponding to the function.</param>
        /// <returns>The desired activation function.</returns>
        public static Func <double, double> GetActivationFunction(ActivationStyle style)
        {
            switch (style)
            {
            case ActivationStyle.SigmoidBasic:
                return(Sigmoid);

            case ActivationStyle.SigmoidNEAT:
                return(SigmoidNEAT);

            case ActivationStyle.ReLU:
                return(ReLU);

            case ActivationStyle.None:
                return(UnitLine);

            default:
                throw new Exception("ActivationFunctions.GetActivationFunction style not found.");
                break;
            }

            return(Sigmoid);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Basic constructor.
 /// </summary>
 /// <param name="type">The type of the node.</param>
 /// <param name="aFun">The function to be used in activation.</param>
 public NodeInformation(NodeType type, ActivationStyle style)
 {
     Type  = type;
     Style = style;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructs a MutationStyle.AddNode innovation.
 /// </summary>
 /// <param name="conInfo">The original connection being broke.</param>
 /// <param name="style">The ActivationStyle of the new node.</param>
 public InnovationInformation(ConnectionInformation conInfo, ActivationStyle style)
 {
     InnovationType        = MutationStyle.AddNode;
     ConnectionInformation = conInfo;
     Style = style;
 }