Beispiel #1
0
    def __wall_distances(self, snake_head: Point,
                         direction: Direction) -> np.array:
        north_wall = Point(snake_head.x, 0)
        west_wall = Point(0, snake_head.y)
        south_wall = Point(snake_head.x, self.game.dimensions()[1])
        east_wall = Point(self.game.dimensions()[0], snake_head.y)

        snake_north_wall_dist = snake_head.distance(north_wall)
        snake_west_wall_dist = snake_head.distance(west_wall)
        snake_south_wall_dist = snake_head.distance(south_wall)
        snake_east_wall_dist = snake_head.distance(east_wall)

        if direction == Direction.UP:
            return np.array([
                snake_west_wall_dist, snake_north_wall_dist,
                snake_east_wall_dist
            ])

        if direction == Direction.RIGHT:
            return np.array([
                snake_north_wall_dist, snake_east_wall_dist,
                snake_south_wall_dist
            ])

        if direction == Direction.DOWN:
            return np.array([
                snake_east_wall_dist, snake_south_wall_dist,
                snake_west_wall_dist
            ])

        if direction == Direction.LEFT:
            return np.array([
                snake_south_wall_dist, snake_west_wall_dist,
                snake_north_wall_dist
            ])
Beispiel #2
0
    def __stop_lidar(self):
        """
        Gets the snakes 8 point lidar in it's starting position.
        :return:
        """
        lidar_pulses = [
            self.lidar_north_pulse(),
            self.lidar_north_east_pulse(),
            self.lidar_east_pulse(),
            self.lidar_south_east_pulse(),
            self.lidar_south_pulse(),
            self.lidar_south_west_pulse(),
            self.lidar_west_pulse(),
            self.lidar_north_west_pulse()
        ]
        lidar = np.zeros((len(lidar_pulses) * 3), dtype=np.float32)
        snake_x, snake_y = self.snakeEnv.snake.head.pos(
        )[0], self.snakeEnv.snake.head.pos()[1]
        snake_pos = Point(snake_x, snake_y)

        for i in range(len(lidar_pulses)):
            lidar_pulse = lidar_pulses[i]
            index = i * 3
            lidar[index] = snake_pos.distance(lidar_pulse[0])
            lidar[index + 1] = lidar_pulse[1]
            lidar[index + 2] = lidar_pulse[2]

        self.lidar_end_points = [
            lidar_pulse[0] for lidar_pulse in lidar_pulses
        ]

        return lidar
Beispiel #3
0
    def __left_lidar(self):
        """
        Gets the Snake's 5 point lidar distances when it is facing left.

        Parameters:
            None

        Returns:
            lidar - A 1D numpy array with 5 entries, one for each lidar distance.
        """
        lidar_pulses = [
            self.lidar_south_pulse(),
            self.lidar_south_west_pulse(),
            self.lidar_west_pulse(),
            self.lidar_north_west_pulse(),
            self.lidar_north_pulse()
        ]
        lidar = np.zeros((len(lidar_pulses) * 3), dtype=np.float32)
        snake_x, snake_y = self.snakeEnv.snake.head.pos(
        )[0], self.snakeEnv.snake.head.pos()[1]
        snake_pos = Point(snake_x, snake_y)

        for i in range(len(lidar_pulses)):
            lidar_pulse = lidar_pulses[i]
            index = i * 3
            lidar[index] = snake_pos.distance(lidar_pulse[0])
            lidar[index + 1] = lidar_pulse[1]
            lidar[index + 2] = lidar_pulse[2]

        self.lidar_end_points = [
            lidar_pulses[0][0], lidar_pulses[1][0], lidar_pulses[2][0],
            lidar_pulses[3][0], lidar_pulses[4][0]
        ]

        return lidar
Beispiel #4
0
    def __get_food_distance_reward(self, previous_location: Point,
                                   current_location: Point):
        """

        :param previous_location: A type point that specifies previous location of the environment
        :param current_location: A type point that specifies the current location of the environment
        :return: 1 if the environment moved towards the food, 0 if the environment moved away from the food
        """
        """
        food_x = self.current_food.head.xcor()
        food_y = self.current_food.head.ycor()
        food_point = Point(food_x, food_y)
        if (previous_location.distance(food_point) > current_location.distance(food_point)):
            return 1

        return 
        """
        def normalise(x):
            return 1 - (x / self.max_distance)

        food_x = self.current_food.head.xcor()
        food_y = self.current_food.head.ycor()
        food_point = Point(food_x, food_y)
        multiplier = len(
            self.snake.segments) if len(self.snake.segments) > 2 else 1
        return normalise(current_location.distance(food_point)) * multiplier
Beispiel #5
0
def distance_to_apple_percentage_tail_length_multiplier(env: SnakeEnv):
    def normalise(x):
        return 1 - (x / env.max_distance)

    food_x = env.current_food.head.xcor()
    food_y = env.current_food.head.ycor()
    food_point = Point(food_x, food_y)
    current_location = Point(env.snake.head.xcor(), env.snake.head.ycor())
    multiplier = len(env.snake.segments) if len(env.snake.segments) > 2 else 1
    return normalise(current_location.distance(food_point)) * multiplier