public Connection GetConnection(NetworkIndex index) { WorkingNeuron neuron = neurons[index.LayerIndex][index.NeuronIndex] as WorkingNeuron; if (neuron != null) { return(neuron.GetConnections()[index.ConnectionIndex]); } else { return(null); } }
/// <summary> /// Mixes two networks together, meaning taking 50% of the /// otherNetwork connections and set the weights of those in this network /// </summary> /// <param name="otherNetwork">The network to be mixed in</param> /// <param name="mixRate">Percentage of the otherNetwork to use</param> public void MixNetwork(NeuronalNetwork otherNetwork, float mixRate) { List <NetworkIndex> mixIndices = new List <NetworkIndex>(); int totalCount = 0; var maxLayerIndex = Math.Min(neurons.Count, otherNetwork.neurons.Count); for (int layerIndex = 1; layerIndex < maxLayerIndex; layerIndex++) { int maxNeuronIndex = Math.Min(neurons[layerIndex].Count, otherNetwork.neurons[layerIndex].Count); for (int neuronIndex = 0; neuronIndex < maxNeuronIndex; neuronIndex++) { totalCount += Math.Min( ((WorkingNeuron)neurons[layerIndex][neuronIndex]).GetConnections().Count, ((WorkingNeuron)otherNetwork.neurons[layerIndex][neuronIndex]).GetConnections().Count); } } while (mixIndices.Count < totalCount * mixRate) { NetworkIndex index = new NetworkIndex(); do { index.LayerIndex = Simulation.RandomInt(1, maxLayerIndex); index.NeuronIndex = Simulation.RandomInt(Math.Min(neurons[index.LayerIndex].Count, otherNetwork.neurons[index.LayerIndex].Count)); index.ConnectionIndex = Simulation.RandomInt(Math.Min( ((WorkingNeuron)neurons[index.LayerIndex][index.NeuronIndex]).GetConnections().Count, ((WorkingNeuron)otherNetwork.neurons[index.LayerIndex][index.NeuronIndex]).GetConnections().Count)); } while (mixIndices.Contains(index)); mixIndices.Add(index); } foreach (NetworkIndex index in mixIndices) { Connection toBeChanged = GetConnection(index); Connection toBeUsed = otherNetwork.GetConnection(index); toBeChanged.weight = toBeUsed.weight; } }