/// <summary> /// Randomizes the weights of all connections between all neurons in range of -5.0 to 5.0 /// </summary> public void RandomizeWeights() { MyRandom r = new MyRandom(-0.5, 0.5); for (int l = layers.Count - 1; l > 0; --l) { foreach (Neuron n in layers.ElementAt(l)) { WorkingNeuron wn = n as WorkingNeuron; if (wn != null) { List <Connection> con = wn.connections; foreach (Connection c in con) { c.SetWeight(r.NextDouble()); } } } } }
public void MutateWeightsRandom(int mutations) { MyRandom myrand = new MyRandom(-0.3, 0.3); for (int i = 0; i < mutations; ++i) { int layer = myrand.NextInt(1, this.layers.Count - 1); //select neuron Neuron neuron = null; while (neuron == null) { neuron = layers.ElementAt(layer).ElementAt(myrand.NextInt(0, layers.ElementAt(layer).Count - 1)) as WorkingNeuron; } //select connection index int index = myrand.NextInt(0, ((WorkingNeuron)neuron).connections.Count - 1); double w = ((WorkingNeuron)neuron).connections.ElementAt(index).GetWeight(); double change = myrand.NextDouble(-0.3, 0.3); change *= w; ((WorkingNeuron)neuron).connections[index].SetWeight(w + change); } }