Exemplo n.º 1
0
 private void InitSnakes()
 {
     for (int i = 0; i < populationCount; i++)
     {
         fields.Add(new Field(fieldSize, fieldSize, i * (int)DateTime.Now.Ticks));
         var neuroNetwork = new NeuroNetwork(snakeInputs, hiddenLayersConfig[StaticRandom.Next(hiddenLayersConfig.Count)], snakeOutputs, snakeMemory);
         population.Add(new Snake(fields[i], neuroNetwork));
     }
 }
Exemplo n.º 2
0
        private void Initialize()
        {
            var point = new Point(StaticRandom.Next(1, Field.Width - 1), StaticRandom.Next(1, Field.Height - 1));

            while (Field[point] != Content.EMPTY)
            {
                point = new Point(StaticRandom.Next(1, Field.Width - 1), StaticRandom.Next(1, Field.Height - 1));
            }
            Field[point] = Content.SNAKE;
            body.Add(point);
        }
Exemplo n.º 3
0
        public void PlaceFood()
        {
            var point = new Point(StaticRandom.Next(Width), StaticRandom.Next(Height));
            int count = 0;

            while (this[point] != Content.EMPTY)
            {
                if (++count > 1000)
                {
                    throw new Exception();
                }
                point = new Point(StaticRandom.Next(Width), StaticRandom.Next(Height));
            }
            this[point] = Content.FOOD;
            Food.Add(point);
        }
Exemplo n.º 4
0
        public NeuroNetwork(int inputs, List <int> hiddenLayers, int outputs, int memory = 0, bool generate = true)
        {
            Inputs       = new List <Neuron>();
            HiddenLayers = new List <List <Neuron> >();
            for (int i = 0; i < hiddenLayers.Count; i++)
            {
                HiddenLayers.Add(new List <Neuron>());
            }
            Outputs = new List <Neuron>();
            Memory  = new List <Neuron>();

            if (generate)
            {
                GenerateRand(inputs, hiddenLayers, outputs, memory);
            }
            Color = Color.FromArgb(StaticRandom.Next(256), StaticRandom.Next(256), StaticRandom.Next(256));
        }
Exemplo n.º 5
0
        public Snake(Field field, NeuroNetwork neuroNetwork)
        {
            Field       = field;
            field.Snake = this;

            NeuroNetwork       = neuroNetwork;
            neuroNetwork.Snake = this;

            body      = new List <Point>();
            Energy    = 50;
            Rotation  = StaticRandom.Next(4);
            lastTurns = new List <double> {
                0, 0, 0
            };

            Initialize();
        }
Exemplo n.º 6
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++;
        }