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); }
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 void MutateAddConnection(NeuroNet nn) { //TODO: Добавляем новую случайную связь. List <Node> avF = nn.Nodes; List <Node> avT = nn.Nodes.FindAll(x => x.ID >= Inputs); /* * * int maxT = 2*(avT.Count * (avT.Count - 1) / 2) + avT.Count + (Inputs* avT.Count); * int numAs = 0; * foreach (Connection c in nn.Connections) * { * if(c.Active) * { * numAs++; * } * } * * if(maxT < numAs) * {*/ int f = 0; int t = 0; do { f = avF[GetRandomInt(avF.Count)].ID; t = avT[GetRandomInt(avT.Count)].ID; } while (nn.Connections.Exists(x => x.From == f && x.To == t)); Connection ex = _generationConnections.Find(x => x.From == f && x.To == t); if (ex == null) { Connection con = new Connection(GetNewID(), f, t, GetRandom(-WeightDispersion, WeightDispersion)); nn.AddConnection(con); _generationConnections.Add(con); } else { nn.AddConnection(new Connection(ex)); } }