def run_in_step_by_step_mode(self, gc_window_drawer_func = None, save_screen_func = None, event_handler_func = None, record_a_and_r_func = None):
        frame_cnt=0
        bool_drawgc = False
        clock = pygame.time.Clock()
        # Play 10 episodes
        for episode in xrange(10):
            total_reward = 0
            while not self.ale.game_over():
                # Get game image
                cur_frame_np = self.ale.getScreenRGB()
                cur_frame_Surface = pygame.surfarray.make_surface(cur_frame_np)
                cur_frame_Surface = pygame.transform.flip(cur_frame_Surface, True, False)
                cur_frame_Surface = pygame.transform.rotate(cur_frame_Surface, 90)

                frame_cnt+=1
                # Save frame to disk (160*210, i.e. not scaled; because this is faster)
                if save_screen_func != None:
                    save_screen_func(cur_frame_Surface, frame_cnt)

                key, draw_next_game_frame = None, False
                while not draw_next_game_frame:
                    clock.tick(FRAME_RATE) # control FPS

                    key = pygame.key.get_pressed()
                    if event_handler_func != None:
                        stop, eyelink_err_code, bool_drawgc = event_handler_func(key)
                        if stop:
                            return eyelink_err_code
                    a_index = aenum.action_map(key, self.gamename)
                    # Not in all cases when action_map returns "NO OP" is the real action "NO OP",
                    # Only when the human press "TAB", is the real action "NO OP".
                    if (a_index == aenum.PLAYER_A_NOOP and key[pygame.K_TAB]) \
                    or  a_index != aenum.PLAYER_A_NOOP:
                        draw_next_game_frame = True

                    # Draw the image onto screen.
                    # Perform scaling directly on screen, leaving cur_frame_Surface unscaled.
                    pygame.transform.scale(cur_frame_Surface, self.size, self.screen)

                    if gc_window_drawer_func != None and bool_drawgc:
                        gc_window_drawer_func(self.screen)

                    pygame.display.flip()
                    pygame.event.pump() # need this line to get new key pressed

                # Apply an action and get the resulting reward
                a = self.legal_actions[a_index]
                reward = self.ale.act(a);
                print("step reward: ", reward)
                total_reward += reward
                if record_a_and_r_func != None:
                    record_a_and_r_func(a, reward)

            print 'Episode', episode, 'ended with score:', total_reward
            self.ale.reset_game()

        TRIAL_OK = 0 # copied from EyeLink's constant
        return TRIAL_OK
Esempio n. 2
0
    def run_in_step_by_step_mode(self,
                                 gc_window_drawer_func=None,
                                 save_screen_func=None,
                                 event_handler_func=None,
                                 record_a_and_r_func=None):
        bool_drawgc = False
        self.run_start_time = time.time()  # used in alerecord_main.py
        while True:
            self.check_episode_end_and_if_true_reset_game()
            # Get game image
            cur_frame_np = self.ale.getScreenRGB()
            cur_frame_Surface = pygame.surfarray.make_surface(cur_frame_np)
            cur_frame_Surface = pygame.transform.flip(cur_frame_Surface, True,
                                                      False)
            cur_frame_Surface = pygame.transform.rotate(cur_frame_Surface, 90)

            self.frame_cnt += 1
            # Save frame to disk (160*210, i.e. not scaled; because this is faster)
            if save_screen_func != None:
                save_screen_func(cur_frame_Surface, self.frame_cnt)

            key, draw_next_game_frame = None, False
            while not draw_next_game_frame:
                self.clock.tick(FRAME_RATE)  # control FPS

                key = pygame.key.get_pressed()
                if event_handler_func != None:
                    stop, eyelink_err_code, bool_drawgc = event_handler_func(
                        key, self)
                    if stop:
                        return eyelink_err_code
                a_index = aenum.action_map(key, self.gamename)
                # Not in all cases when action_map returns "NO OP" is the real action "NO OP",
                # Only when the human press "TAB", is the real action "NO OP".
                if (a_index == aenum.PLAYER_A_NOOP and key[pygame.K_TAB]) \
                or  a_index != aenum.PLAYER_A_NOOP:
                    draw_next_game_frame = True

                # Draw the image onto screen.
                # Perform scaling directly on screen, leaving cur_frame_Surface unscaled.
                pygame.transform.scale(cur_frame_Surface, self.size,
                                       self.screen)

                if gc_window_drawer_func != None and bool_drawgc:
                    gc_window_drawer_func(self.screen)

                pygame.display.flip()
                pygame.event.pump()  # need this line to get new key pressed

            # Apply an action and get the resulting reward
            a = self.legal_actions[a_index]
            reward = self.ale.act(a)
            self.score += reward
            if record_a_and_r_func != None:
                record_a_and_r_func(a, reward, self.episode, self.score)
        assert False, "Returning code should only be in the while True loop"
    def run(self, gc_window_drawer_func = None, save_screen_func = None, event_handler_func = None, record_a_and_r_func = None):
        last_time=time.time()
        frame_cnt=0
        clock = pygame.time.Clock()
        # Play 10 episodes
        for episode in xrange(EPISODES):
            total_reward = 0
            while not self.ale.game_over():
                clock.tick(FRAME_RATE) # control FPS
                frame_cnt+=1

                key = pygame.key.get_pressed()
                if event_handler_func != None:
                    stop, eyelink_err_code, bool_drawgc = event_handler_func(key)
                    if stop:
                        return eyelink_err_code

                # Display FPS
                diff_time = time.time()-last_time
                if diff_time > 1.0:
                    print 'FPS: %.1f' % clock.get_fps()
                    last_time=time.time()

                # Show game image
                cur_frame_np = self.ale.getScreenRGB()
                cur_frame_Surface = pygame.surfarray.make_surface(cur_frame_np)
                cur_frame_Surface = pygame.transform.flip(cur_frame_Surface, True, False)
                cur_frame_Surface = pygame.transform.rotate(cur_frame_Surface, 90)
                # Perform scaling directly on screen, leaving cur_frame_Surface unscaled.
                # Slightly faster than scaling cur_frame_Surface and then transfer to screen.
                pygame.transform.scale(cur_frame_Surface, self.size, self.screen)

                if gc_window_drawer_func != None and bool_drawgc:
                    gc_window_drawer_func(self.screen)
                pygame.display.flip()

                # Save frame to disk (160*210, i.e. not scaled; because this is faster)
                if save_screen_func != None:
                    save_screen_func(cur_frame_Surface, frame_cnt)

                # Apply an action and get the resulting reward
                a_index = aenum.action_map(key, self.gamename)
                a = self.legal_actions[a_index]
                reward = self.ale.act(a);
                total_reward += reward
                if record_a_and_r_func != None:
                    record_a_and_r_func(a, reward)

                pygame.event.pump() # need this line to get new key pressed

            print 'Episode', episode, 'ended with score:', total_reward
            self.ale.reset_game()

        TRIAL_OK = 0 # copied from EyeLink's constant
        return TRIAL_OK
Esempio n. 4
0
    def run(self,
            gc_window_drawer_func=None,
            save_screen_func=None,
            event_handler_func=None,
            record_a_and_r_func=None):
        self.run_start_time = time.time()  # used in alerecord_main.py
        while True:
            self.check_episode_end_and_if_true_reset_game()
            self.clock.tick(FRAME_RATE)  # control FPS
            self.frame_cnt += 1

            key = pygame.key.get_pressed()
            if event_handler_func != None:
                stop, eyelink_err_code, bool_drawgc = event_handler_func(
                    key, self)
                if stop:
                    return eyelink_err_code

            # Display FPS
            diff_time = time.time() - self._last_time
            if diff_time > 1.0:
                print 'FPS: %.1f' % self.clock.get_fps()
                self._last_time = time.time()

            # Show game image
            cur_frame_np = self.ale.getScreenRGB()
            cur_frame_Surface = pygame.surfarray.make_surface(cur_frame_np)
            cur_frame_Surface = pygame.transform.flip(cur_frame_Surface, True,
                                                      False)
            cur_frame_Surface = pygame.transform.rotate(cur_frame_Surface, 90)
            # Perform scaling directly on screen, leaving cur_frame_Surface unscaled.
            # Slightly faster than scaling cur_frame_Surface and then transfer to screen.
            pygame.transform.scale(cur_frame_Surface, self.size, self.screen)

            if gc_window_drawer_func != None and bool_drawgc:
                gc_window_drawer_func(self.screen)
            pygame.display.flip()

            # Save frame to disk (160*210, i.e. not scaled; because this is faster)
            if save_screen_func != None:
                save_screen_func(cur_frame_Surface, self.frame_cnt)

            # Apply an action and get the resulting reward
            a_index = aenum.action_map(key, self.gamename)
            a = self.legal_actions[a_index]
            reward = self.ale.act(a)
            self.score += reward
            if record_a_and_r_func != None:
                record_a_and_r_func(a, reward, self.episode, self.score)

            pygame.event.pump()  # need this line to get new key pressed
        assert False, "Returning should only happen in the while True loop"
                    print "Toggle print_logits:", print_logits

                if event.key == pygame.K_ESCAPE:
                    print "K_ESCAPE pressed. Exit now."
                    sys.exit(0)

                if event.key == pygame.K_h:
                    human_take_over = not human_take_over
                    print "Toggle human play:", human_take_over

        if print_logits and pred['raw_logits'] is not None:
            logits = pred['raw_logits'][0]
            m = np.argmax(logits)
            string = aenum.nameof(a)
            for i in range(len(logits)):
                cur = " %.1f" % logits[i]
                if i == m:
                    string += MU.BMU.color(
                        cur, 'RED')  # the action that has max logit
                elif i == a:
                    string += MU.BMU.color(cur,
                                           'GREEN')  # actual action sampled
                else:
                    string += cur
            print string

        if human_take_over:
            key = pygame.key.get_pressed()
            a = aenum.action_map(key, ale.gamename)
        # --------- END keyboard event handling -------
from IPython import embed
sys.path.insert(0, '../shared') # After research, this is the best way to import a file in another dir
import action_enums as aenum
import vip_constants as V
from aleForET import aleForET

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print 'Usage:', sys.argv[0], 'rom_file', '[resume_state_file]'
        sys.exit()
    rom_file = sys.argv[1]
    resume_state_file = sys.argv[2] if len(sys.argv)>=3 else None
    rndseed = random.randint(0,65535)

    pygame.init()
    pygame.display.set_mode((160*V.xSCALE, 210*V.ySCALE), RESIZABLE | DOUBLEBUF | RLEACCEL, 32)
    pygame.mouse.set_visible(False)
    surf = pygame.display.get_surface()
    ale = aleForET(rom_file, surf, rndseed, resume_state_file)

    while True:
        key = pygame.key.get_pressed()
        pygame.event.pump() # need this line to get new key pressed
        a = ale.legal_actions[aenum.action_map(key, ale.gamename)]
        img_np, r, epEnd = ale.proceed_one_step(a, refresh_screen=True, fps_limit=30)

        if key[pygame.K_x]:
            embed()