Ejemplo n.º 1
0
 def test_model(self):
     steps_arr = []
     scores_arr = []
     for a in range(self.test_games):
         steps = 0
         game_memory = []
         game = Snake()
         _, score, snake, food = game.start()
         prev_observation = self.generate_observation(snake, food)
         for _ in range(self.goal_steps):
             done, score, snake, food, action, predictions = self._play(
                 game, snake, prev_observation)
             game_memory.append([prev_observation, action])
             if done:
                 result_string = steps + snake + food + prev_observation + predictions
                 print('-----')
                 print('Game Test', a)
                 print(result_string)
                 with open("testing.txt", "a") as file:
                     file.write(f'Test {a}\n {result_string}')
                 break
             else:
                 prev_observation = self.generate_observation(snake, food)
                 steps += 1
         steps_arr.append(steps)
         scores_arr.append(score)
     print('Average steps:', mean(steps_arr))
     print(Counter(steps_arr))
     print('Average score:', mean(scores_arr))
     print(Counter(scores_arr))
     step_str = str(mean(steps_arr))
     score_str = str(mean(scores_arr))
     with open("Stats.txt", "a") as file:
         file.write(step_str + "  ----------  " + score_str + "\n")
Ejemplo n.º 2
0
 def visualise_game(self):
     while True:
         game = Snake(is_gui=True)
         _, score, snake, food = game.start()
         prev_observation = self.generate_observation(snake, food)
         for _ in range(self.goal_steps):
             done, score, snake, food, action, predictions = self._play(
                 game, snake, prev_observation)
             if done:
                 break
             else:
                 prev_observation = self.generate_observation(snake, food)
Ejemplo n.º 3
0
 def initial_population(self):
     training_data = []
     for i in range(self.init_games):
         print("Training Game:", i + 1)
         game = Snake()
         _, prev_score, snake, food = game.start()
         prev_observation = self.generate_observation(snake, food)
         prev_food_distance = self.get_food_distance(snake, food)
         for _ in range(self.goal_steps):
             action, game_action = self.generate_action(snake)
             done, score, snake, food = game.move(game_action)
             if done:
                 training_data.append([
                     self.add_action_to_observation(prev_observation,
                                                    action), -1
                 ])
                 # with open("training.txt", "a") as file:
                 #     file.write('Game' + str(i + 1) + '\t' + "  ----------  " + str(training_data[-1]) + "\n")
                 break
             else:
                 food_distance = self.get_food_distance(snake, food)
                 if score > prev_score or food_distance < prev_food_distance:
                     training_data.append([
                         self.add_action_to_observation(
                             prev_observation, action), 1
                     ])
                 else:
                     training_data.append([
                         self.add_action_to_observation(
                             prev_observation, action), 0
                     ])
                 prev_observation = self.generate_observation(snake, food)
                 prev_food_distance = food_distance
             # with open("training.txt", "a") as file:
             #     file.write('Game' + str(i+1)+'\t' + "  ----------  " + str(training_data[-1]) + "\n")
     return training_data