private static void MutateSynapseFromNeuron(Ann ann) { if (HelperCountSynapses(ann) == 0) { return; } Neuron synapseNeuron = ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)]; if (synapseNeuron.synapses.Count == 0) { return; } Synapse synapse = synapseNeuron.synapses[Util.rand.Next(synapseNeuron.synapses.Count)]; int fromLayer = synapse.FromLayer; Neuron[] neuronsInLayer = ann.GetNeuronsForLayer(fromLayer); Neuron neuron = neuronsInLayer.Length != 0 ? neuronsInLayer[Util.rand.Next(neuronsInLayer.Length)] : null; int newNeuronPosition = neuron != null ? neuron.NeuronPosition : 0; if (!HelperDoesSynapseExist(ann, synapse.FromLayer, newNeuronPosition)) { synapse.FromNeuron = newNeuronPosition; } }
private static void MutateSynapseRemoveRandom(Ann ann) { if (HelperCountSynapses(ann) == 0) { return; } Neuron neuron = ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)]; if (neuron.synapses.Count == 0) { return; } neuron.synapses.RemoveAt(Util.rand.Next(neuron.synapses.Count)); }
private static void MutateSynapseWeightIncreaseRandom(Ann ann) { if (HelperCountSynapses(ann) == 0) { return; } Neuron synapseNeuron = ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)]; if (synapseNeuron.synapses.Count == 0) { return; } Synapse synapse = synapseNeuron.synapses[Util.rand.Next(synapseNeuron.synapses.Count)]; synapse.Weight += Util.rand.NextDouble(); }
private static void MutateSynapseAddRandom(Ann ann) { if (ann.hiddenNeurons.Count == 0) { return; } int[] layers = ann.GetAllLayers(); int fromLayer = layers.Length != 0 ? layers[Util.rand.Next(layers.Length)] : 0; int[] fromNeuronPositions = ann.GetNeuronsForLayer(fromLayer).Select(x => x.NeuronPosition).ToArray(); int fromNeuron = fromNeuronPositions[Util.rand.Next(fromNeuronPositions.Length)]; Neuron synapseNeuron = ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)]; if (!HelperDoesSynapseExist(ann, fromLayer, fromNeuron)) { synapseNeuron.synapses.Add(new Synapse(fromLayer, fromNeuron)); } }
private static int HelperCountSynapses(Ann ann) { int synapseCount = 0; foreach (Neuron neuron in ann.GetAllNeurons()) { foreach (Synapse synapse in neuron.synapses) { synapseCount++; } } return(synapseCount); }
private static void MutateSynapseFromLayer(Ann ann) { if (HelperCountSynapses(ann) == 0) { return; } Neuron synapseNeuron = ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)]; if (synapseNeuron.synapses.Count == 0) { return; } Synapse synapse = synapseNeuron.synapses[Util.rand.Next(synapseNeuron.synapses.Count)]; int layer = ann.GetAllLayers()[Util.rand.Next(ann.GetAllLayers().Length)]; if (!HelperDoesSynapseExist(ann, layer, synapse.FromNeuron)) { synapse.FromLayer = layer; } }
private static void HelperCleanUpBackwardsSynapses(Ann ann) { foreach (Neuron neuron in ann.GetAllNeurons()) { List <Synapse> synapseChecklist = new List <Synapse>(); foreach (Synapse synapse in neuron.synapses) { if (synapse.FromLayer > neuron.Layer) { synapseChecklist.Add(synapse); } } neuron.synapses = synapseChecklist; } }
private static bool HelperDoesSynapseExist(Ann ann, int fromLayer, int fromNeuron) { bool exists = false; foreach (Neuron neuron in ann.GetAllNeurons()) { foreach (Synapse synapse in neuron.synapses) { if (synapse.FromLayer == fromLayer && synapse.FromNeuron == fromNeuron) { exists = true; } } } return(exists); }
private static void HelperCleanUpDuplicates(Ann ann) { List <Neuron> neuronCheckList = new List <Neuron>(); foreach (Neuron neuron in ann.GetAllNeurons()) { List <Synapse> synapseCheckList = new List <Synapse>(); foreach (Synapse synapse in neuron.synapses) { bool foundOnChecklist = false; foreach (Synapse synapseFromCheckList in synapseCheckList) { if (synapse.FromLayer == synapseFromCheckList.FromLayer && synapse.FromNeuron == synapseFromCheckList.FromNeuron) { foundOnChecklist = true; } } if (!foundOnChecklist) { synapseCheckList.Add(synapse); } } neuron.synapses = synapseCheckList; } foreach (Neuron neuron in ann.hiddenNeurons) { bool foundOnChecklist = false; foreach (Neuron neuronFromCheckList in neuronCheckList) { if (neuron.Layer == neuronFromCheckList.Layer && neuron.NeuronPosition == neuronFromCheckList.NeuronPosition) { foundOnChecklist = true; } } if (!foundOnChecklist) { neuronCheckList.Add(neuron); } } ann.hiddenNeurons = neuronCheckList; }
private static void MutateNeuronBiasDecreaseRandom(Ann ann) { ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)].Bias -= Util.rand.NextDouble() * 100; }
private static void MutateNeuronBiasRandomize(Ann ann) { ann.GetAllNeurons()[Util.rand.Next(ann.GetAllNeurons().Count)].Bias = Util.rand.NextDouble(); }