/// <summary> /// Gets the hash code of a potential connection gene pattern. Based off the innovation numbers of the given node gene patterns. /// </summary> /// <param name="pedigree">The owning pedigree of the potential connection gene pattern.</param> /// <param name="from">The from node of the potential connection gene pattern.</param> /// <param name="to">The to node of the potential connection gene pattern.</param> /// <returns>The hash code.</returns> /// <exception cref="ArgumentNullException">When from/to is null.</exception> public static int GetHashCode(Pedigree pedigree, NodeGenePattern from, NodeGenePattern to) { Helpers.ThrowOnNull(from, "from"); Helpers.ThrowOnNull(to, "to"); return(from.InnovationNumber * pedigree.MaxNodes + to.InnovationNumber); }
/// <summary> /// Constructs a connection gene pattern with the given innovation number, from/to nodes, and replacing number. /// </summary> /// <param name="pedigree">The owning pedigree of this connection gene pattern.</param> /// <param name="innovation_number">The innovation number.</param> /// <param name="from">The NodeGene to connect from.</param> /// <param name="to">The NodeGene to connect to.</param> /// <param name="replacing_number">The innovation number of the node that replaces this connection.</param> /// <exception cref="ArgumentNullException">When from/to is null.</exception> protected internal ConnectionGenePattern(Pedigree pedigree, int innovation_number, NodeGenePattern from, NodeGenePattern to, int replacing_number) : base(pedigree, innovation_number) { Helpers.ThrowOnNull(from, "from"); Helpers.ThrowOnNull(to, "to"); From = from; To = to; ReplacingNumber = replacing_number; }
/// <summary> /// Adds the given node gene pattern if not already there. /// </summary> /// <param name="nodeGenePattern">The node gene pattern to add.</param> /// <returns>True if added. False if already contained.</returns> /// <exception cref="ArgumentNullException">When the node gene pattern is null.</exception> public bool AddNodeGenePattern(NodeGenePattern nodeGenePattern) { Helpers.ThrowOnNull(nodeGenePattern, "nodeGenePattern"); if (nodeGenePatterns.ContainsKey(nodeGenePattern.InnovationNumber)) { return(false); } nodeGenePatterns.Add(nodeGenePattern.InnovationNumber, nodeGenePattern); return(true); }
/// <summary> /// Creates a node gene with the intention of splitting the given connection gene. Gives it a random known activation function. /// </summary> /// <remarks> /// If the node gene pattern does not yet exist, it will be created. Its innovation number will be the replacing number in the given connection gene's pattern. The X will be the /// average of the Xs in the given connection gene pattern's From/To nodes. /// </remarks> /// <param name="connectionGene">The connection gene with the intention of splitting.</param> /// <returns>The created node gene.</returns> /// <exception cref="ArgumentNullException">When the connection gene is null.</exception> public NodeGene Create_NodeGene(ConnectionGene connectionGene) { Helpers.ThrowOnNull(connectionGene, "connectionGene"); NodeGenePattern pattern; if (nodeGenePatterns.ContainsKey(connectionGene.ConnectionGenePattern.ReplacingNumber)) { pattern = nodeGenePatterns[connectionGene.ConnectionGenePattern.ReplacingNumber]; } else { pattern = new NodeGenePattern(this, connectionGene.ConnectionGenePattern.ReplacingNumber, (connectionGene.ConnectionGenePattern.From.X + connectionGene.ConnectionGenePattern.To.X) / 2); nodeGenePatterns.Add(pattern.InnovationNumber, pattern); } return(new NodeGene(pattern, GetRandomActivationFunction())); }
/// <summary> /// Creates a node gene with the given pattern and activation function. /// </summary> /// <param name="nodeGenePattern">The node gene pattern of the node gene.</param> /// <param name="activationFunction">The activation function of the node gene.</param> /// <returns>The created node gene.</returns> /// <exception cref="ArgumentNullException">When the node gene pattern or activation function is null.</exception> public NodeGene Create_NodeGene(NodeGenePattern nodeGenePattern, Node.ActivationFunction activationFunction) { return(new NodeGene(nodeGenePattern, activationFunction)); }