Example #1
0
def play(params, task, max_time):
    from reward_machines.reward_machine import RewardMachine

    # commands
    str_to_action = {
        "w": Actions.up.value,
        "d": Actions.right.value,
        "s": Actions.down.value,
        "a": Actions.left.value
    }
    # play the game!
    game = CraftWorld(params)
    rm = RewardMachine(task)
    s1 = game.get_state()
    u1 = rm.get_initial_state()
    for t in range(max_time):
        # Showing game
        game.show_map()
        print("Events:", game.get_true_propositions())
        print("Features:", game.get_features())
        print("Features.shape:", game.get_features().shape)
        print("Features.manhattan_distance:",
              game._get_features_manhattan_distance())
        acts = game.get_actions()
        # Getting action
        print("\nAction? ", end="")
        a = input()
        print()
        # Executing action
        if a in str_to_action and str_to_action[a] in acts:
            game.execute_action(str_to_action[a])

            s2 = game.get_state()
            events = game.get_true_propositions()
            u2 = rm.get_next_state(u1, events)
            reward = rm.get_reward(u1, u2, s1, a, s2)

            if game.env_game_over or rm.is_terminal_state(u2):  # Game Over
                print("Game Over")
                break

            s1, u1 = s2, u2
        else:
            print("Forbidden action")
    game.show_map()
    return reward
Example #2
0
def play():
    import pygame, time
    from reward_machines.reward_machine import RewardMachine

    from tester.tester import Tester
    from tester.tester_params import TestingParameters    
    from qrm.learning_params import LearningParameters

    # hack: moving one directory up (to keep relative references to ./src)
    import os
    os.chdir("../")

    tester = Tester(LearningParameters(), TestingParameters(), "../experiments/water/tests/water_7.txt")
    if tester is None:
        task = "../experiments/water/reward_machines/t1.txt"
        state_file = "../experiments/water/maps/world_0.pkl"
        max_x = 400
        max_y = 400
        b_num_per_color = 2
        b_radius = 15
        use_velocities = True
        ball_disappear = False

        params = WaterWorldParams(state_file, b_radius=b_radius, max_x=max_x, max_y=max_y, 
                                  b_num_per_color=b_num_per_color, use_velocities = use_velocities, 
                                  ball_disappear=ball_disappear)
    else:
        task   = tester.get_task_rms()[-2]
        params = tester.get_task_params(task).game_params

    max_x, max_y = params.max_x, params.max_y

    game = WaterWorld(params)    
    rm = RewardMachine(task) 
    s1 = game.get_state()
    u1 = rm.get_initial_state()

    print("actions", game.get_actions())

    pygame.init()
    
    black = (0,0,0)
    white = (255,255,255)
    colors = get_colors()
    
    gameDisplay = pygame.display.set_mode((max_x, max_y))
    pygame.display.set_caption('Water world :)')
    clock = pygame.time.Clock()
    crashed = False

    t_previous = time.time()
    actions = set()
    while not crashed:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True
            if event.type == pygame.KEYUP:
                if Actions.left in actions and event.key == pygame.K_LEFT:
                    actions.remove(Actions.left)
                if Actions.right in actions and event.key == pygame.K_RIGHT:
                    actions.remove(Actions.right)
                if Actions.up in actions and event.key == pygame.K_UP:
                    actions.remove(Actions.up)
                if Actions.down in actions and event.key == pygame.K_DOWN:
                    actions.remove(Actions.down)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    actions.add(Actions.left)
                if event.key == pygame.K_RIGHT:
                    actions.add(Actions.right)
                if event.key == pygame.K_UP:
                    actions.add(Actions.up)
                if event.key == pygame.K_DOWN:
                    actions.add(Actions.down)
            

        t_current = time.time()
        t_delta = (t_current - t_previous)

        # Getting the action
        if len(actions) == 0: a = Actions.none
        else: a = random.choice(list(actions))

        # Executing the action
        game.execute_action(a.value, t_delta)

        s2 = game.get_state()
        events = game.get_true_propositions()
        u2 = rm.get_next_state(u1, events)
        reward = rm.get_reward(u1,u2,s1,a,s2)

        # printing image
        gameDisplay.fill(white)
        for b in game.balls:
            draw_ball(b, colors, 0, gameDisplay, pygame, max_y)
        draw_ball(game.agent, colors, 3, gameDisplay, pygame, max_y)
        pygame.display.update()
        clock.tick(20)

        # print info related to the task
        if reward > 0: print("REWARD!! ----------------!------------!")
        if rm.is_terminal_state(u2): 
            print("Machine state:", u2, "(terminal)")
        else:
            print("Machine state:", u2)

        t_previous = t_current
        s1, u1 = s2, u2

    pygame.quit()
Example #3
0
def play():
    from tester.tester import Tester
    from tester.tester_params import TestingParameters
    from qrm.learning_params import LearningParameters
    from reward_machines.reward_machine import RewardMachine

    import os
    os.chdir("../")
    tester = Tester(LearningParameters(), TestingParameters(),
                    "../experiments/mouse/tests/mouse_0.txt")

    task = tester.get_task_rms()[1]
    params = tester.get_task_params(task).game_params
    max_x = params.max_x
    max_y = params.max_y
    game = MouseWorld(params)
    rm = RewardMachine(task)
    s1 = game.get_state()
    u1 = rm.get_initial_state()

    pygame.init()
    gameDisplay = pygame.display.set_mode((max_x, max_y))
    pygame.display.set_caption('Fake Keyboard')
    clock = pygame.time.Clock()
    crashed = False

    t_previous = time.time()
    actions = set()
    while not crashed:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True

            if event.type == pygame.KEYUP:
                if Actions.left in actions and event.key == pygame.K_LEFT:
                    actions.remove(Actions.left)
                if Actions.right in actions and event.key == pygame.K_RIGHT:
                    actions.remove(Actions.right)
                if Actions.up in actions and event.key == pygame.K_UP:
                    actions.remove(Actions.up)
                if Actions.down in actions and event.key == pygame.K_DOWN:
                    actions.remove(Actions.down)
                if Actions.jump in actions and event.key == pygame.K_SPACE:
                    actions.remove(Actions.jump)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    actions.add(Actions.left)
                if event.key == pygame.K_RIGHT:
                    actions.add(Actions.right)
                if event.key == pygame.K_UP:
                    actions.add(Actions.up)
                if event.key == pygame.K_DOWN:
                    actions.add(Actions.down)
                if event.key == pygame.K_SPACE:
                    actions.add(Actions.jump)

        t_current = time.time()
        t_delta = (t_current - t_previous)

        if len(actions) == 0:
            a = Actions.none
        else:
            a = random.choice(list(actions))

        # Executing the action
        game.execute_action(a.value, t_delta)

        s2 = game.get_state()
        events = game.get_true_propositions()
        u2 = rm.get_next_state(u1, events)
        reward = rm.get_reward(u1, u2, s1, a, s2)

        if reward > 0:
            print("REWARD ", reward)
        if rm.is_terminal_state(u2):
            print("Machine state:", u2, "(terminal)")
        else:
            print("Machine state:", u2)

        # Printing Image
        gameDisplay.fill(Colors.WHITE.value)
        for k in game.keyboard_keys:
            k.draw_on_display(gameDisplay)
        game.agent.draw_on_display(gameDisplay)
        game.draw_current_text_on_display(gameDisplay)

        pygame.display.update()
        clock.tick(20)

        t_previous = t_current
        s1, u1 = s2, u2

    pygame.quit()