示例#1
0
        public NeuralNet CrossOver(MovingAgent parentOne, MovingAgent parentTwo)
        {
            NeuralNet    neuralNet        = new NeuralNet(true);
            List <float> newWeights       = new List <float>();
            List <float> parentOneWeights = parentOne.GetNetworkWeights();
            List <float> parentTwoWeights = parentTwo.GetNetworkWeights();

            int crossOverPoint;

            if (Utilities.IsRandomCrossoverPoint)
            {
                crossOverPoint = (int)Utilities.RandomMinMax(0, parentOneWeights.Count);
            }
            else
            {
                crossOverPoint = (int)(parentOneWeights.Count * Utilities.CrossoverPoint);
            }

            for (int i = 0; i < crossOverPoint; i++)
            {
                newWeights.Add(parentOneWeights[i]);
            }

            for (int i = crossOverPoint; i < parentOneWeights.Count; i++)
            {
                newWeights.Add(parentTwoWeights[i]);
            }

            neuralNet.SetWeights(ref newWeights);

            return(neuralNet);
        }
示例#2
0
        public void Mutate()
        {
            List <float> weights = new List <float>();

            weights.AddRange(neuralNet.GetWeights());

            // int mutate = (int)Utilities.RandomMinMax(0, weights.Count);
            // weights[mutate] += (Utilities.RandomMinMax(-1, 1) * Utilities.MaxPerturbation);

            for (int i = 0; i < weights.Count; ++i)
            {
                if (Utilities.RandomMinMax(0, 1) < Utilities.MutationRate)
                {
                    weights[i] += (Utilities.RandomMinMax(-1, 1) * Utilities.MaxPerturbation);
                }
            }

            neuralNet.SetWeights(ref weights);
        }