Beispiel #1
0
    def test_change_x_position_out_of_screen(self):
        """
        Test change_player_xposition (edge case - when agent tries to go out of screen from right side)
        """

        player = Player(i=10, log_dir=".", tob=10, energy=200, x=1164, y=0)
        player.change_player_xposition(5)

        self.assertEqual(player.playerX, 1168)
        self.assertEqual(player.energy, 198)
Beispiel #2
0
    def test_change_x_position_negative_x(self):
        """
        Test change_player_xposition (edge case - when x becomes <= 0 i.e agent reaches left most side)
        """

        player = Player(i=10, log_dir=".", tob=10, energy=200, x=0, y=0)
        player.change_player_xposition(-3)

        self.assertEqual(player.playerX, 0)
        self.assertEqual(player.energy, 198)
Beispiel #3
0
    def test_change_x_position(self):
        """
        Test change_player_xposition (normal case)
        """

        player = Player(i=10, log_dir=".", tob=10, energy=200, x=0, y=0)
        player.change_player_xposition(3)

        self.assertEqual(player.playerX, 3)
        self.assertEqual(player.energy, 198)
Beispiel #4
0
    def test_change_x_position_cannot_move(self):
        """
        Test change_player_xposition (when agent is not allowed to move)
        """

        player = Player(i=10, log_dir=".", tob=10, energy=200, x=0, y=0)
        player.cannot_move = True
        player.change_player_xposition(3)

        self.assertEqual(player.playerX, 0)
        self.assertEqual(player.energy, 200)
Beispiel #5
0
def visualize(file_location, speed):
    """
    Convert action from number to text and display in pygame screen along with the time at which the action was performed

    Params
    ======
    file_location  (str)
        : Full path of log file of agent containing all the actions performed by the agent throughout his/her lifetime
    speed          (int)
        : Speed (in seconds) after which the next frame should be loaded (display speed)
    """

    # Initialize pygame
    pygame.init()

    # Set caption for the pygame window
    pygame.display.set_caption("Prima Vita")

    # Set the size of pygame window
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

    # Fill the screen in green color
    screen.fill((0, 178, 0))

    # Create font in which everything will be rendered in screen
    myfont = pygame.font.SysFont("monospace", 32)

    # Extract all events in the agent's life
    life_events = np.load(file_location, allow_pickle=True)

    # Get initial position (stored at index 0 of log file)
    x, y = life_events[0][0], life_events[0][1]
    i = 0
    try:
        # Get time of birth (though it is present in the filename itself), this line helps to identify
        # if the agent died without doing anything or not
        tob = life_events[2][1] if len(
            life_events[1]) == 2 else life_events[1][1]

        # Actual time of birth
        tob = int(os.path.basename(file_location).split("-")[0])
    except:
        # Agent died without doing anything, exit the visualizer
        print(
            os.path.basename(file_location).split(".")[0].split("-")[1],
            "died without doing anything",
        )
        sys.exit()

    # Get all the actions from life events
    life_events = life_events[2:] if len(
        life_events[1]) == 2 else life_events[1:]

    # Initialize player object for the current player
    player = Player(i,
                    log_dir=".",
                    tob=tob,
                    energy=200,
                    x=x,
                    y=y,
                    mode="human")

    for life_event in life_events:
        # Extract action result, action and time at which it was done
        result = "" if life_event[2] != 0 else "Failed "
        action = life_event[0]
        timestamp = life_event[1]
        age = timestamp - tob

        # Get the text for action and timestamp
        actiontext, timetext, agetext = current_action_time(
            result, action, timestamp, age, myfont)

        # Fill the screen with green color
        screen.fill((0, 178, 0))

        # Find the food particles and players in proximity to current agent
        food_in_proximity = life_event[-1][0]
        players_in_proximity = life_event[-1][1]

        particles = []
        players = []

        # Display the food particle which are in the agent's state
        for i in range(len(food_in_proximity) // 2):
            food_info = food_in_proximity[i:i + 2]
            if len(food_info) > 0:
                particles.append(
                    Particle(
                        x=(player.playerX + food_info[0]),
                        y=(player.playerY + food_info[1]),
                        mode="human",
                    ))
                particles[-1].show_close(screen)

        # Display the agents which are in close proximity to current agent
        # All other agents appear yellow (as they are shown using show_close) while the current agent is shown in red color
        for i in range(len(players_in_proximity) // 3):
            player_info = players_in_proximity[i:i + 3]
            if len(player_info) > 0:
                players.append(
                    Player(
                        i=i,
                        tob=i,
                        log_dir=".",
                        energy=200,
                        x=(player.playerX + player_info[0]),
                        y=(player.playerY + player_info[1]),
                        mode="human",
                    ))
                players[-1].show_close(screen)

        for event in pygame.event.get():
            pass

        # Action left
        if action == 0:
            player.change_player_xposition(-3)
        # Action right
        elif action == 1:
            player.change_player_xposition(3)
        # Action up
        elif action == 2:
            player.change_player_yposition(-3)
        # Action down
        elif action == 3:
            player.change_player_yposition(3)
        # Action up then left (North-West)
        elif action == 4:
            player.change_player_yposition(-3)
            player.change_player_xposition(-3)
        # Action up then right (North-East)
        elif action == 5:
            player.change_player_yposition(-3)
            player.change_player_xposition(3)
        # Action down then left (South-West)
        elif action == 6:
            player.change_player_yposition(3)
            player.change_player_xposition(-3)
        # Action down then right (South-East)
        elif action == 7:
            player.change_player_yposition(3)
            player.change_player_xposition(3)
        # Action stay
        elif action == 8:
            player.energy -= 2

        # Show current player
        player.show_player(screen)

        # Blit the action text and timestamp text to screen
        screen.blit(actiontext, (5, 10))
        screen.blit(timetext, (5, 40))
        screen.blit(agetext, (5, 70))

        # Update the pygame display
        pygame.display.update()

        # Wait for some time (in seconds) until processing the next action (frame)
        # So that humans can properly see what is going on in an agent's life
        time.sleep(speed)
Beispiel #6
0
life_events = life_events[2:] if len(life_events[1]) == 2 else life_events[1:]

player = Player(i, tob, x, y)

for life_event in life_events:
    action = life_event[0]
    scoretext = current_action(action)

    screen.fill((0, 178, 0))

    for event in pygame.event.get():
        pass

    if action == 0:  # Left
        player.change_player_xposition(-3)

    elif action == 1:  # Right
        player.change_player_xposition(3)

    elif action == 2:  # Up
        player.change_player_yposition(-3)

    elif action == 3:  # Down
        player.change_player_yposition(3)

    elif action == 4:  # Up Left
        player.change_player_yposition(-3)
        player.change_player_xposition(-3)

    elif action == 5:  # Up Right