public NeuroNet MergeNeuroNets(NeuroNet a, NeuroNet b) { NeuroNet nn = new NeuroNet(Inputs, Outputs); a.Nodes.ForEach(x => { if (x.ID > Inputs + Outputs) { nn.AddNode(new Node(x.ID)); } }); a.Connections.ForEach(x => nn.AddConnection(x.ID, x.From, x.To, x.Weight, x.Active)); b.Nodes.ForEach(x => { if (!nn.Nodes.Exists(y => y.ID == x.ID)) { nn.AddNode(x); } }); b.Connections.ForEach(x => { Connection con = nn.Connections.Find(y => y.From == x.From && y.To == x.To); if (con == null) { nn.AddConnection(x); } else { con.Weight = (con.Weight / 2 + x.Weight / 2); con.ID = GetNewID(); if (con.Active && !x.Active) { con.Active = false; } else if (!con.Active == !x.Active) { if (GetRandom(0.0, 1.0) < MergeMutationChance) { x.Active = !x.Active; } } } }); return(nn); }
public NeuroNet GenerateNeuroNet(int additionalNodes = 0, int additionalConnections = 1) { NeuroNet res = new NeuroNet(Inputs, Outputs); List <Node> nn = new List <Node>(); for (int i = 0; i < additionalNodes; ++i) { Node n = new Node(res.Nodes.Count); nn.Add(n); res.AddNode(n); } List <Node> avF = res.Nodes; List <Node> avT = res.Nodes.FindAll(x => x.ID >= Inputs); for (int i = 0; i < additionalNodes; ++i) { res.AddConnection(GetNewID(), avF[GetRandomInt(avF.Count)].ID, nn[i].ID, GetRandom(-WeightDispersion, WeightDispersion)); res.AddConnection(GetNewID(), nn[i].ID, avT[GetRandomInt(avT.Count)].ID, GetRandom(-WeightDispersion, WeightDispersion)); } for (int i = 0; i < additionalConnections; ++i) { res.AddConnection(GetNewID(), avF[GetRandomInt(avF.Count)].ID, avT[GetRandomInt(avT.Count)].ID, GetRandom(-WeightDispersion, WeightDispersion)); } return(res); }