public void Mutate_RemoveNeurons() { var neuronMap = new Dictionary <int, RMP_NeuronGene>(); //Remove Neurons if (NeuronRemoveChance.Value > Pseudo.Random.NextDouble()) { int count = 0; foreach (var neuron in HiddenNeuronGenes) { neuron.Name += "-" + count.ToString(); neuronMap.Add(count, neuron); count++; } int removeCount = 1; for (int n = 0; n < removeCount; n++) { int neuronIndex; do { neuronIndex = Pseudo.Random.Next(HiddenNeuronGenes.Count); } while (HiddenNeuronGenes[neuronIndex] == null); var gene = HiddenNeuronGenes[neuronIndex]; HiddenNeuronGenes[neuronIndex] = null; neuronMap.Remove(neuronIndex); } HiddenNeuronGenes.Clear(); HiddenNeuronGenes.AddRange(from key in neuronMap.Keys orderby Convert.ToInt32(key) select neuronMap[key]); foreach (RMP_NeuronGene neuronGene in neuronMap.Values.Concat(this.Inputs)) { foreach (var connection in new List <RMP_ConnectionGene>(neuronGene.Connections)) { if (!connection.IsOutputConnection) { if (neuronMap.ContainsKey(connection.Target)) { var target = neuronMap[connection.Target]; connection.Target = HiddenNeuronGenes.IndexOf(target); } else { neuronGene.Connections.Remove(connection); } } } } } }
public void Mutate_RemoveNeurons() { var neuronMap = new Dictionary <int, SpNeuronGene>(); //Remove Neurons if (NeuronRemoveChance.Value > Pseudo.Random.NextDouble()) { int count = 0; foreach (var neuron in HiddenNeuronGenes) { neuronMap.Add(count, neuron); count++; } int removeCount = Pseudo.Random.Next((int)Math.Round(HiddenNeuronGenes.Count * MaxNeuronRemoving.Value, 0)); for (int n = 0; n < removeCount; n++) { int neuronIndex; do { neuronIndex = Pseudo.Random.Next(HiddenNeuronGenes.Count); } while (HiddenNeuronGenes[neuronIndex] == null); var gene = HiddenNeuronGenes[neuronIndex]; HiddenNeuronGenes[neuronIndex] = null; neuronMap.Remove(neuronIndex); } HiddenNeuronGenes.Clear(); HiddenNeuronGenes.AddRange(from key in neuronMap.Keys orderby key select neuronMap[key]); foreach (var neuronGene in neuronMap.Values.Concat(EyeRNeuronGenes).Concat(EyeGNeuronGenes).Concat(EyeBNeuronGenes).Concat(InputNeuronGenes)) { foreach (var connection in new List <SpConnectionGene>(neuronGene.Connections)) { if (!connection.IsOutputConnection && neuronMap.ContainsKey(connection.Target)) { var target = neuronMap[connection.Target]; connection.Target = HiddenNeuronGenes.IndexOf(target); } else { neuronGene.Connections.Remove(connection); } } } } }