public void Mutate() { #if false int ran = NeuralNet.RandomGenerator.Next(1, 8); if (ran == 1) { return; } else if (ran > 4) { InputSynapses.ForEach(syn => { if (NeuralNet.RandomGenerator.Next(1, 2) == 1) { syn.Weight *= 1 + (NeuralNet.RandomGenerator.NextDouble() - 0.5) / 10; } }); } else if (ran == 2) { InputSynapses.ForEach(syn => { if (NeuralNet.RandomGenerator.Next(1, 5) == 1) { syn.Weight *= -1; } }); } else if (ran == 3) { Bias *= 1 + (NeuralNet.RandomGenerator.NextDouble() - 0.5) / 5; } else if (ran == 4) { Bias *= -1; } #else InputSynapses.ForEach(syn => { var ran = NeuralNet.RandomGenerator.NextDouble(); if (ran < Constants.Con.mutate_percent_of_synapses_uniform) { syn.Weight += NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.mutation_power_synapse); } else if (ran < Constants.Con.mutate_percent_of_synapses_uniform + Constants.Con.mutate_percent_of_synapses_new_values) { syn.Weight = NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.init_stdDev_synapse); } syn.Weight = Math.Max(-30, Math.Min(30, syn.Weight)); }); {//bias var ran = NeuralNet.RandomGenerator.NextDouble(); if (ran < Constants.Con.mutate_percent_of_biases_uniform) { Bias += NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.mutation_power_bias); } else if (ran < Constants.Con.mutate_percent_of_synapses_uniform + Constants.Con.mutate_percent_of_biases_new_values) { Bias = NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.init_stdDev_bias); } Bias = Math.Max(-30, Math.Min(30, Bias)); } #endif }
//public void UpdateWeights(double learnRate, double momentum) //{ // var prevDelta = BiasDelta; // BiasDelta = learnRate * Gradient; // Bias += BiasDelta + momentum * prevDelta; // foreach (var synapse in InputSynapses) // { // prevDelta = synapse.WeightDelta; // synapse.WeightDelta = learnRate * Gradient * synapse.InputNeuron.Value; // synapse.Weight += synapse.WeightDelta + momentum * prevDelta; // } //} #endregion public void ResetNeuron() { Initialization(); InputSynapses.ForEach(synapse => synapse.Initialization()); }