private static void MutateNeuronAddRandom(Ann ann) { int[] layers = ann.GetHiddenLayers(); int layer = layers.Length != 0 ? layers[Util.rand.Next(layers.Length)] : 0; int[] neuronPositions = ann.GetNeuronsForLayer(layer).Select(x => x.NeuronPosition).ToArray(); int neuronId = 0; bool keepSearching = true; while (keepSearching) { if (neuronPositions.Contains(neuronId)) { neuronId++; } else { keepSearching = false; } } if (!HelperDoesNeuronExist(ann, layer, neuronId)) { ann.hiddenNeurons.Add(new Neuron(layer, neuronId)); } }
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)]; int toLayer = layers.Length != 0 ? layers[Util.rand.Next(layers.Length)] : 0; int[] toNeuronPositions = ann.GetNeuronsForLayer(toLayer).Select(x => x.NeuronPosition).ToArray(); int toNeuron = toNeuronPositions[Util.rand.Next(toNeuronPositions.Length)]; if (toLayer != fromLayer && !HelperDoesSynapseExist(ann, fromLayer, fromNeuron, toLayer, toNeuron)) { ann.synapses.Add(new Synapse(fromLayer, fromNeuron, toLayer, toNeuron)); } }
private static void MutateSynapseToNeuron(Ann ann) { if (ann.synapses.Count == 0) { return; } Synapse synapse = ann.synapses[Util.rand.Next(ann.synapses.Count)]; int toLayer = synapse.ToLayer; Neuron[] neuronsInLayer = ann.GetNeuronsForLayer(toLayer); Neuron neuron = neuronsInLayer.Length != 0 ? neuronsInLayer[Util.rand.Next(neuronsInLayer.Length)] : null; int newNeuronPosition = neuron != null ? neuron.NeuronPosition : 0; if (!HelperDoesSynapseExist(ann, synapse.FromLayer, synapse.FromNeuron, synapse.ToLayer, newNeuronPosition)) { synapse.ToNeuron = newNeuronPosition; } }