示例#1
0
 public void Train()
 {
     for (int i =0; i < iterations;i++)
     {
         while (snake.GameOver==false)
         {
             SnakeGame temp = (SnakeGame)snake.Clone();  
             
         }
     }
 }
示例#2
0
        public void Train()
        {
            int       t    = 0;
            SnakeGame temp = new SnakeGame((SnakeGame)snake.Clone());

            while (t < iterations)
            {
                Console.WriteLine("Iteration:" + t);
                List <NeuralNetwork> newPop = new List <NeuralNetwork>();
                while (newPop.Count < population.Count)
                {
                    if (temp.gameOver == true)
                    {
                        temp = new SnakeGame((SnakeGame)snake.Clone());
                    }
                    NeuralNetwork bestNN1     = new NeuralNetwork();
                    NeuralNetwork bestNN2     = new NeuralNetwork();
                    int           bestResult1 = -1;
                    int           bestResult2 = -1;
                    for (int i = 0; i < 10; i++)
                    {
                        int           nextnum = Rgen.Next(population.Count);
                        NeuralNetwork curNN   = population[i];

                        //int curResult = playGameGetScore(curNN);
                        int curResult = getResult(curNN, new SnakeGame((SnakeGame)temp.Clone()));
                        if (curResult > bestResult1)
                        {
                            bestResult1 = curResult;
                            bestNN1     = curNN;
                        }
                    }
                    for (int i = 0; i < 10; i++)
                    {
                        int           nextnum = Rgen.Next(population.Count);
                        NeuralNetwork curNN   = population[i];

                        //int curResult = playGameGetScore(curNN);
                        int curResult = getResult(curNN, new SnakeGame((SnakeGame)temp.Clone()));
                        if (curResult > bestResult2)
                        {
                            bestResult2 = curResult;
                            bestNN2     = curNN;
                        }
                    }

                    NeuralNetwork crossedNN = crossGen(bestNN1, bestNN2);
                    crossedNN = Mutate(crossedNN);
                    newPop.Add(crossedNN);
                }
                // int nextnum2 = Rgen.Next(newPop.Count);
                //temp.MakeMove(newPop[nextnum2].calculateDirection(temp.outputBox()));
                List <NeuralNetwork> newList = new List <NeuralNetwork>(newPop.Count);
                foreach (NeuralNetwork item in newPop)
                {
                    newList.Add(item);
                }
                population = newList;
                temp.moveHead(population[0].calculateDirection(temp.getInputs()));
                t++;
            }
        }