コード例 #1
0
        public void MutateWeightsRandom(int mutations)
        {
            MyRandom myrand = new MyRandom(-0.3, 0.3);

            for (int i = 0; i < mutations; ++i)
            {
                int layer = myrand.NextInt(0, 10);


                if (layer <= 6)
                {
                    //select neuron
                    Neuron neuron = null;
                    while (neuron == null)
                    {
                        neuron = hiddenLayer.ElementAt(myrand.NextInt(0, hiddenLayer.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);
                }
                else if (layer >= 7)
                {
                    //select neuron
                    Neuron neuron = null;
                    while (neuron == null)
                    {
                        neuron = outputLayer.ElementAt(myrand.NextInt(0, outputLayer.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);
                }
            }
        }
コード例 #2
0
        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);
            }
        }