コード例 #1
0
        public static NeuralNet ReadFromFile(string path, NeuralNet net)
        {
            string[]      allWeightss = File.ReadAllLines(path);
            List <string> keyList     = new List <string>(net.weights.Keys);
            int           i           = 0;

            foreach (var key in keyList)
            {
                net.weights[key] = float.Parse(allWeightss[i]);
                i++;
            }
            return(net);
        }
コード例 #2
0
ファイル: NeuralNet.cs プロジェクト: TomerMir/SnakeAI
 public NeuralNet CrossWith(NeuralNet net)
 {
     List<string> keyList = new List<string>(weights.Keys);
     NeuralNet LastNet = new NeuralNet(layers);
     float thisValue;
     float inputValue;
     foreach (var key in keyList)
     {
         thisValue = this.weights[key];
         inputValue = net.weights[key];
         LastNet.weights[key] = (thisValue + inputValue) / 2;
     }
     return LastNet;
 }
コード例 #3
0
        public void PlayBest(NeuralNet net)
        {
            int   tmpFood = 1;
            Snake snake   = new Snake();
            int   genNum1 = food.Generate(0);

            if (genNum1 == 0)
            {
                tmpFood++;
            }
            else
            {
                tmpFood += genNum1 + 1;
            }
            while (snake.IsLoose() == false)
            {
                snake.addFirst();
                snake.removeLast();

                float[] inputArr = new float[6] {
                    snake.IsFoodForward(), snake.IsFoodRight(), snake.IsFoodLeft(), snake.IsClearForward(), snake.IsClearRight(), snake.IsClearLeft()
                };
                string Direction = net.GetDirection(inputArr).ToString();

                if (Direction == "Left")
                {
                    snake.MoveLeft();
                }
                else if (Direction == "Right")
                {
                    snake.MoveRight();
                }
                else
                {
                    snake.KeepForward();
                }
                snake.hunger--;
                if (Board.board[snake.nextPlace.X, snake.nextPlace.Y].whatsIn == Place.WhatsInEnum.Food)
                {
                    Place tmpPlace = new Place();
                    tmpPlace.X       = snake.snake.Last.Value.X;
                    tmpPlace.Y       = snake.snake.Last.Value.Y;
                    tmpPlace.whatsIn = Place.WhatsInEnum.Snake;
                    snake.snake.AddLast(tmpPlace);
                    snake.score++;
                    snake.hunger += 50;
                    int genNum = food.Generate(tmpFood);
                    if (genNum == 0)
                    {
                        tmpFood++;
                    }
                    else
                    {
                        tmpFood += genNum + 1;
                    }
                }
                Console.Clear();
                _board.Print();
                Thread.Sleep(10);
            }
        }