public virtual object Clone() { // NNode::NNode(NNode *n,Trait *t) NEATNode myobj = null; try { myobj = (NEATNode)MemberwiseClone(); myobj.NodeId = NodeId; myobj.Type = Type; myobj.GeneticNodeLabel = GeneticNodeLabel; myobj.ActivationCount = 0; myobj.LastActivation = 0; myobj.PreviousLastActivation = 0; myobj.Override = false; myobj.OverrideValue = 0; myobj.Frozen = false; myobj.functionType = FunctionType.SIGMOID; myobj.ActiveSum = 0; myobj.Activation = 0; myobj.ActiveFlag = false; myobj.IsTraversed = false; myobj.InnerLevel = 0; } catch (CloneNotSupportedException e) // never happens { throw new InvalidOperationException(); } return(myobj); }
/** * Return a clone of this node, but with a empty incomingGenes list. */ public object EmptyClone() { NEATNode myobj = (NEATNode)Clone(); myobj.IncomingGenes = new List <NEATGene>(); return(myobj); }
/** * This checks a POTENTIAL link between start from fromNode to toNode to use * count and threshold to jump out in the case of an infinite loop. */ public static bool[] HasPath(IEvolutionState state, NEATNode toNode, NEATNode fromNode, int threshold) { var results = new bool[2]; int level = 0; var set = new HashSet <NEATNode>(); // for keeping track of the visiting nodes HasPath(state, toNode, fromNode, set, level, threshold, results); return(results); }
/** * The setup method initializes a "meaningless" gene that does not specify * any connection. */ public override void Setup(IEvolutionState state, IParameter paramBase) { Weight = 0.0; // node id 1-indexed InNodeId = 0; OutNodeId = 0; InNode = null; OutNode = null; IsRecurrent = false; InnovationNumber = 0; MutationNumber = 0.0; TimeDelay = false; Enable = true; Frozen = false; }
/** * Create the phenotype (network) from the genotype (genome). One main task * of method is to link the incomingGenes for each nodes. */ public void BuildNetwork(NEATIndividual ind) { Individual = ind; ((List <NEATNode>)Nodes).AddRange(Individual.Nodes); List <NEATNode> inputList = new List <NEATNode>(); List <NEATNode> outputList = new List <NEATNode>(); // NOTE: original code clone the node, thus organism and network each // have a node instance // but we do not clone it here foreach (NEATNode node in Individual.Nodes) { // we are rebuild the network, we clear all the node incomingGenes // as we will rebuild it later node.ClearIncoming(); // Check for input or output designation of node if (node.GeneticNodeLabel == NEATNode.NodePlace.INPUT) { inputList.Add(node); } else if (node.GeneticNodeLabel == NEATNode.NodePlace.BIAS) { inputList.Add(node); } else if (node.GeneticNodeLabel == NEATNode.NodePlace.OUTPUT) { outputList.Add(node); } } ((List <NEATNode>)Inputs).AddRange(inputList); ((List <NEATNode>)Outputs).AddRange(outputList); // prepare the incomingGenes for each node foreach (Gene g in Individual.genome) { // only deal with enabled nodes NEATGene link = (NEATGene)g; if (link.Enable) { NEATNode outNode = link.OutNode; outNode.IncomingGenes.Add(link); } } }
/** Reset the gene with given parameters. */ public void Reset(double w, int iNodeId, int oNodeId, bool recur, int innov, double mutNum) { // Gene::Gene(double w, NNode *inode, NNode *onode, bool recur, double // innov, double mnum) Weight = w; InNodeId = iNodeId; OutNodeId = oNodeId; InNode = null; OutNode = null; IsRecurrent = recur; InnovationNumber = innov; MutationNumber = mutNum; TimeDelay = false; Enable = true; Frozen = false; }
public override bool Equals(Object obj) { NEATNode n = (NEATNode)obj; if (NodeId != n.NodeId) { return(false); } for (int i = 0; i < IncomingGenes.Count; i++) { if (!n.IncomingGenes[i].Equals(IncomingGenes[i])) { return(false); } } return(true); }
/** The helper function to check if there is a path from fromNode to toNode. */ public static void HasPath(IEvolutionState state, NEATNode toNode, NEATNode fromNode, HashSet <NEATNode> set, int level, int threshold, bool[] results) { if (level > threshold) { // caught in infinite loop results[0] = false; results[1] = false; return; } if (toNode.NodeId == fromNode.NodeId) { results[0] = true; results[1] = true; } else { // Check back on all links... // But skip links that are already recurrent // (We want to check back through the forward flow of signals only foreach (NEATGene link in toNode.IncomingGenes) { if (!link.IsRecurrent) { if (!set.Contains(link.InNode)) { set.Add(link.InNode); HasPath(state, link.InNode, fromNode, set, level + 1, threshold, results); if (results[0] && results[1]) { return; } } } } set.Add(toNode); results[0] = true; results[1] = false; } }
/** Return the depth of this node in the network. */ public int Depth(int d, NEATNetwork network, int maxDepth) { if (d > 100) { // original code use these number in code, need to find a good way // to justify these return(10); } // Base case if (this.Type == NodeType.SENSOR) { return(d); } d++; // recursion int curDepth = 0; // The depth of current node for (int i = 0; i < IncomingGenes.Count; ++i) { NEATNode node = IncomingGenes[i].InNode; if (!node.IsTraversed) { node.IsTraversed = true; curDepth = node.Depth(d, network, maxDepth); node.InnerLevel = curDepth - d; } else { curDepth = d + node.InnerLevel; } maxDepth = Math.Max(curDepth, maxDepth); } return(maxDepth); }
/** Add a new output node. */ public void AddOutput(NEATNode node) { Outputs.Add(node); }
/** Add a new input node. */ public void AddInput(NEATNode node) { Inputs.Add(node); }