public static void AddNodeMutation(EvolveNeuroNet neuroNet) { List <Connection> connections = neuroNet.ToConnectionList(); if (connections.Count != 0) { bool found = false; foreach (var conn in connections) { if (conn.IsActive) { found = true; } } if (found) { int index = 0; found = false; do { index = Random.Range(0, connections.Count); if (connections[index].IsActive) { found = true; } } while (!found); connections[index].Deactivate(); Neuron neuron = neuroNet.AddNeuron(); neuroNet.AddConnection(connections[index].Source, neuron); neuroNet.AddConnection(neuron, connections[index].Target); List <Neuron> list = new List <Neuron>(); list.Add(neuron); neuroNet.HiddenLayer.Add(list); } RebuildNet(neuroNet); } }
public static void AddConnectionMutation(EvolveNeuroNet neuroNet) { List <Neuron> neurons = neuroNet.ToNeuronList(); int from = 0; int to = 0; bool found = false; do { from = Random.Range(0, neurons.Count); if (neuroNet.IsOutput(neurons[from])) { found = false; } else { found = true; } if (found) { to = Random.Range(0, neurons.Count); if (to == from) { found = false; } else { if (neuroNet.IsInput(neurons[to])) { found = false; } else { found = true; } foreach (var conn in neurons[from].Connections) { if (conn.Target.Index == neurons[to].Index) { found = false; } } } } } while (!found); neuroNet.AddConnection(neurons[from], neurons[to]); RebuildNet(neuroNet); }