Exemplo n.º 1
0
        public void NeuroShake(double rate)
        {
            for (int i = 0; i < HiddenLayers.Count; i++)
            {
                for (int j = 0; j < HiddenLayers[i].Count; j++)
                {
                    for (int w = 0; w < HiddenLayers[i][j].Weights.Length; w++)
                    {
                        if (StaticRandom.NextDouble() < rate)
                        {
                            HiddenLayers[i][j].Weights[w] = GetRandomWeight();
                        }
                    }
                }
            }

            for (int i = 0; i < Outputs.Count; i++)
            {
                for (int w = 0; w < Outputs[i].Weights.Length; w++)
                {
                    if (StaticRandom.NextDouble() < rate)
                    {
                        Outputs[i].Weights[w] = GetRandomWeight();
                    }
                }
            }

            for (int i = 0; i < Memory.Count; i++)
            {
                for (int w = 0; w < Memory[i].Weights.Length; w++)
                {
                    if (StaticRandom.NextDouble() < rate)
                    {
                        Memory[i].Weights[w] = GetRandomWeight();
                    }
                }
            }

            /*for (int i = 0; i < 10 * rate; i++)
             * {
             * if (R.NextDouble() > 0.3)
             * {
             * var j = R.Next(HiddenLayers.Count);
             * var k = R.Next(HiddenLayers[j].Count);
             * var prevLayer = j == 0 ? Inputs : HiddenLayers[j - 1];
             * var l = R.Next(prevLayer.Count);
             * HiddenLayers[j][k].Weights[l] = GetRandomWeight();
             * }
             * else if (R.NextDouble() > 0.5)
             * {
             * var j = R.Next(Memory.Count);
             * var k = R.Next(HiddenLayers[HiddenLayers.Count - 1].Count);
             * Memory[j].Weights[k] = GetRandomWeight();
             * }
             * else
             * {
             * var j = R.Next(Outputs.Count);
             * var k = R.Next(HiddenLayers[HiddenLayers.Count - 1].Count);
             * Outputs[j].Weights[k] = GetRandomWeight();
             * }
             * }*/
        }
Exemplo n.º 2
0
 public static double GetRandomWeight()
 {
     return(StaticRandom.NextDouble() * 2 - 1);
 }
Exemplo n.º 3
0
        private void CreatePopulation()
        {
            dead.Sort(new SnakeComparer());
            dead.Reverse();
            if (topSnake == null || dead[0].Fitness > topSnake.Fitness)
            {
                topSnake2 = topSnake;
                topSnake  = dead[0];
            }
            if (topSnake2 == null)
            {
                topSnake2 = dead[1];
            }
            scores["maxFitness"]  = Math.Max(scores["maxFitness"], dead[0].Fitness);
            scores["lastFitness"] = dead[0].Fitness;
            if (dead[0].Fitness == 0)
            {
                throw new Exception();
            }

            var fieldCount = 0;

            /*for (int i = 0; i < populationCount * 0.0; i++)
             * {
             * var neuroNetwork = dead[0].NeuroNetwork.Copy();
             * neuroNetwork.NeuroShake(shakeRate);
             * population.Add(new Snake(fields[fieldCount], neuroNetwork));
             * fieldCount++;
             * }*/
            for (int i = 0; i < populationCount * 0.3; i++)
            {
                var neuroNetwork = topSnake.NeuroNetwork.Copy();
                neuroNetwork.Cross(dead[0].NeuroNetwork);
                neuroNetwork.NeuroShake(shakeRate);
                population.Add(new Snake(fields[fieldCount], neuroNetwork));
                fieldCount++;
            }
            for (int i = 0; i < populationCount * 0.3; i++)
            {
                var neuroNetwork = topSnake.NeuroNetwork.Copy();
                neuroNetwork.NeuroShake(shakeRate);
                population.Add(new Snake(fields[fieldCount], neuroNetwork));
                fieldCount++;
            }
            for (int i = 0; i < populationCount * 0.2; i++)
            {
                var neuroNetwork = topSnake2.NeuroNetwork.Copy();
                neuroNetwork.NeuroShake(shakeRate);
                population.Add(new Snake(fields[fieldCount], neuroNetwork));
                fieldCount++;
            }
            for (int i = 0; i < populationCount * 0.2; i++)
            {
                var neuroNetwork = new NeuroNetwork(snakeInputs, hiddenLayersConfig[StaticRandom.Next(hiddenLayersConfig.Count)], snakeOutputs, snakeMemory);
                population.Add(new Snake(fields[fieldCount], neuroNetwork));
                fieldCount++;
            }
            foreach (var field in fields)
            {
                field.ReviewFood();
            }
            dead.Clear();
            populationNumber++;
        }