public NodeData(NodeGene nodeGene) { id = nodeGene.Id; x = nodeGene.x; y = nodeGene.y; type = nodeGene.NodeType; }
//public NodeGene NodeGene_AddOnConnection(ConnectionGene conn) //{ // //var nodeA //} /// <summary> /// Create and add new node gene to list. /// </summary> /// <param name="nodeId">Id number of node.</param> /// <param name="nodeId">Type of node.</param> /// <returns>New node gene.</returns> private NodeGene NodeGene_Add(int nodeId, NodeGene.NODE_TYPE type) { NodeGene node = new NodeGene(nodeId, type); nodeGeneList.Add(node); return(node); }
public Node(NodeGene node, HashSet <Connection> conns) { id = node.Id; type = node.NodeType; Reset(); connIn = new HashSet <Connection>(conns.Where(x => x.outNode == id)); connOut = new HashSet <Connection>(conns.Where(x => x.inNode == id)); }
private IEnumerable <NodeGene> GetNodeGenes(IEnumerable <NodeData> nd) { foreach (var item in nd) { NodeGene node = new NodeGene(item.id, item.type); node.x = item.x; node.y = item.y; yield return(node); } }
private void Mutate_Node(Random rand) { //UnityEngine.Debug.Log("starting mutate node"); if (ConnectionGeneSet.Count > 0) { ConnectionGene conn = ConnectionGeneSet.OrderBy(x => rand.Next()).FirstOrDefault(); //for(int i=0;i<ConnectionGeneSet.Count;i++) //{ //ConnectionGene conn = ConnectionGeneSet.ElementAt(i); NodeGene nodeFrom = NodeGeneSet.Where(x => x.Id.Equals(conn.NodeIn)).FirstOrDefault(), nodeTo = NodeGeneSet.Where(x => x.Id.Equals(conn.NodeOut)).FirstOrDefault(); NodeGene node = pool.NodeGene_Get(conn.ReplaceIndex); node.x = (nodeFrom.x + nodeTo.x) * .5f; node.y = (nodeFrom.y + nodeTo.y) * (.4f + rand.Next(1, 20) * .01f); node.y = (node.y >= 1f) ? 0.99f : (node.y <= 0f) ? 0.01f : node.y; ConnectionGene connA = pool.ConnectionGene_GetOrCreate(conn.NodeIn, node.Id), connB = pool.ConnectionGene_GetOrCreate(node.Id, conn.NodeOut); connA.RandomizeWeight(); connA.IsEnabled = true; connB.IsEnabled = conn.IsEnabled; connB.Weight = conn.Weight; connGeneSet.Add(connA); connGeneSet.Add(connB); conn.IsEnabled = false; nodeGeneSet.Add(node); //} // UnityEngine.Debug.Log("mutate node succesfull"); } }
//public void ExecuteMutation(int n,Random rand) //{ // switch (n) // { // case 0: // { // Mutate_Link(rand); // }break; // case 1: // { // Mutate_Node(rand); // }break; // case 2: // { // Mutate_WeightShift(rand); // }break; // case 3: // { // Mutate_WeightRandomize(rand); // }break; // case 4: // { // Mutate_EnableDisable(rand); // }break; // } //} private void Mutate_Link(Random rand) { for (int i = 0; i < 100; i++) { NodeGene node1 = NodeGeneSet.Select(x => x).OrderBy(x => rand.Next()).FirstOrDefault(), node2 = NodeGeneSet.Select(x => x).OrderBy(x => rand.Next()).FirstOrDefault(); if (node1.Id != node2.Id && (node2.NodeType == NodeGene.NODE_TYPE.HIDDEN || node2.NodeType == NodeGene.NODE_TYPE.OUTPUT)) { ConnectionGene conn; if (node1.x < node2.x) { conn = pool.ConnectionGene_GetOrCreate(node1.Id, node2.Id); } else { continue; } if (ConnectionGeneSet.Contains(conn)) { continue; } conn.RandomizeWeight(); conn.IsEnabled = true; ConnectionGeneSet.Add(conn); break; } } }