Exemple #1
0
 def _predict(self, environment, model):
     predictions = []
     actions = [
         Action.left_neighbor(environment.snake_action),
         environment.snake_action,
         Action.right_neighbor(environment.snake_action)
     ]
     for action in actions:
         observation_for_prediction = environment.observation(action)
         predictions.append(
             model.model.predict(
                 np.array(observation_for_prediction).reshape(
                     -1, Constants.MODEL_FEATURE_COUNT, 1)))
     best_prediction_index = np.argmax(np.array(predictions))
     return actions[best_prediction_index]
Exemple #2
0
    def longest_path(self, start, end, environment):
        longest_path_from_transposition_table = self._path_from_transposition_table(end)
        if longest_path_from_transposition_table:
            return longest_path_from_transposition_table
        shortest_path_solver = ShortestPathBFSSolver()
        path = shortest_path_solver.shortest_path(environment, start, end)
        path.reverse()

        if not path or len(path) <= 1:
            return []
        index = 0
        while True:
            a = path[index]
            b = path[index+1]

            extended_nodes = []

            rotated_actions = [Action.left_neighbor(b.action), Action.right_neighbor(b.action)]
            for rotated_action in rotated_actions:
                inverse_a_action = (a.action[0] * -1, a.action[1] * -1)
                if rotated_action == inverse_a_action:
                    continue
                rotated_neighbor = self._neighbor(a, rotated_action, environment)
                if rotated_neighbor:
                    directed_neighbor = self._neighbor(rotated_neighbor, b.action, environment)
                    if directed_neighbor:
                        if rotated_neighbor not in path and directed_neighbor not in path:
                            extended_nodes = [rotated_neighbor, directed_neighbor]

            if len(extended_nodes) == 2:
                x = extended_nodes[0]
                y = extended_nodes[1]

                path.insert(index+1, x)
                path.insert(index+2, y)

                b = path[index+3]
                b.action = (b.point.x - y.point.x, b.point.y - y.point.y)
                path[index+3] = b
                continue

            index += 1
            if index == len(path)-1:
                break
        self.transposition_table[end] = path
        return path
Exemple #3
0
 def observation(self, new_action):
     head = self.snake[0]
     left_neighbor_action = Action.left_neighbor(self.snake_action)
     left_neighbor_point = Point(head.x + left_neighbor_action[0],
                                 head.y + left_neighbor_action[1])
     left_neighbor_accessible = self._is_point_accessible(
         left_neighbor_point)
     top_neighbor_point = Point(head.x + self.snake_action[0],
                                head.y + self.snake_action[1])
     top_neighbor_accessible = self._is_point_accessible(top_neighbor_point)
     right_neighbor_action = Action.right_neighbor(self.snake_action)
     right_neighbor_point = Point(head.x + right_neighbor_action[0],
                                  head.y + right_neighbor_action[1])
     right_point_accessible = self._is_point_accessible(
         right_neighbor_point)
     action_vector = Action.vector(self.snake_action, new_action)
     return [
         action_vector, left_neighbor_accessible, top_neighbor_accessible,
         right_point_accessible,
         self._angle_from_fruit()
     ]