Exemple #1
0
    def _make_ml_execute(self, scene_info):
        """Send the scene_info to the ml process and wait for the instruction
        """
        comm.send_to_ml(scene_info, self._ml_name)
        time.sleep(self._ml_execute_time)
        instruction = self._instruct_receiver.recv(self._ml_name)

        if instruction.frame != -1 and \
           scene_info.frame - instruction.frame > self._frame_delayed:
            self._frame_delayed = scene_info.frame - instruction.frame
            print("Delayed {} frame(s)".format(self._frame_delayed))

        return instruction
Exemple #2
0
    def _make_ml_execute(self, scene_info):
        """
        Send the scene_info to the ml process and wait for the instruction
        """
        comm.send_to_ml(scene_info, self._ml_name)
        time.sleep(self._ml_execute_time)
        game_cmd = self._process_cmd(comm.recv_from_ml(self._ml_name))

        if (game_cmd["frame"] != -1 and
                scene_info["frame"] - game_cmd["frame"] > self._frame_delayed):
            self._frame_delayed = scene_info["frame"] - game_cmd["frame"]
            print("Delayed {} frame(s)".format(self._frame_delayed))

        return game_cmd["command"]
Exemple #3
0
    def game_loop(self):
        """
        The main loop of the game in machine learning mode

        The execution order in the loop:
        1. Send the scene information to the machine learning process.
        2. Wait for the key frame (During this period, machine learning process can
           process the information and generate the game instruction.)
        3. Check if there has a command to receive. If it has, receive the
           command. Otherwise, generate a dummy one.
        4. Pass the command received to the game and update the scene.
        5. If the game is over or passed, send the scene information containing the
           game status to the machine learning process, and reset the game.
        6. Back to 1.
        """
        comm.wait_ml_ready(self._ml_name)

        while not quit_or_esc():
            scene_info = self._scene.get_scene_info()
            command = self._make_ml_execute(scene_info)

            scene_info["command"] = command.value
            self._record_handler(scene_info)

            game_status = self._scene.update(command)

            self._screen.update(self._scene.catch_ball_times)

            if (game_status == GameStatus.GAME_OVER
                    or game_status == GameStatus.GAME_PASS):
                scene_info = self._scene.get_scene_info()
                comm.send_to_ml(scene_info, self._ml_name)

                scene_info["command"] = None
                self._record_handler(scene_info)

                print(game_status.value)

                if self._one_shot_mode:
                    return

                self._scene.reset()
                self._frame_delayed = 0
                # Wait for ml process doing resetting jobs
                comm.wait_ml_ready(self._ml_name)
Exemple #4
0
    def game_loop(self):
        """
        The game execution loop
        """
        # Wait for the ml process
        comm.wait_ml_ready(self._ml_name)

        while not quit_or_esc():
            # Generate the scene information
            scene_info = self._scene.get_scene_info()

            # Send the scene information to the ml process
            # and wait the command sent from it
            command = self._make_ml_execute(scene_info)

            # Record the scene information
            scene_info["command"] = command.value
            self._record_handler(scene_info)

            # Update the scene
            game_status = self._scene.update(command)

            # If the game is over, reset the scene or
            # quit the game loop if one shot mode is set.
            if game_status == GameStatus.GAME_OVER:
                # Send the scene info with the game over status
                # and record that scene info
                scene_info = self._scene.get_scene_info()
                comm.send_to_ml(scene_info, self._ml_name)

                scene_info["command"] = None
                self._record_handler(scene_info)

                if self._one_shot_mode:
                    return

                self._scene.reset()
                self._frame_delayed = 0

                # Wait for the ml process for the next round
                comm.wait_ml_ready(self._ml_name)

            # Draw the scene to the display
            self._draw_scene()
Exemple #5
0
    def _make_ml_execute(self, scene_info):
        """
        Send the scene info to the ml process and receive the command from it

        The method will wait the ml process to execute the command for a period
        which is decided by the `_ml_execution_time`.
        If the ml process can't send the command in time,
        the method will return a default command.
        """
        comm.send_to_ml(scene_info, self._ml_name)
        time.sleep(self._ml_execution_time)
        game_cmd = self._cmd_receiver.recv(self._ml_name)

        # Check and update the frame delayed
        if (game_cmd.frame != -1 and
            scene_info.frame - game_cmd.frame > self._frame_delayed):
            self._frame_delayed = scene_info.frame - game_cmd.frame

        return game_cmd.command
Exemple #6
0
    def game_loop(self):
        """The main loop of the game in machine learning mode

        The execution order in the loop:
        1. Send the SceneInfo to the machine learning process.
        2. Wait for the key frame (During this period, machine learning process can
           process the SceneInfo and generate the GameInstruction.)
        3. Check if there has a GameInstruction to receive. If it has, receive the
           instruction. Otherwise, generate a dummy one.
        4. Pass the GameInstruction to the game and update the scene (and frame no.)
        5. If the game is over or passed, send the SceneInfo containing the
           game status to the machine learning process, and reset the game.
        6. Back to 1.
        """

        keep_going = lambda: not quit_or_esc()

        comm.wait_ml_ready(self._ml_name)

        while keep_going():
            scene_info = self._scene.fill_scene_info_obj(SceneInfo())
            self._record_handler(scene_info)

            instruction = self._make_ml_execute(scene_info)
            game_status = self._scene.update(instruction.command)

            self._draw_scene()

            if game_status == GameStatus.GAME_OVER or \
               game_status == GameStatus.GAME_PASS:
                scene_info = self._scene.fill_scene_info_obj(SceneInfo())
                self._record_handler(scene_info)
                comm.send_to_ml(scene_info, self._ml_name)

                if self._one_shot_mode:
                    return

                self._scene.reset()
                self._frame_delayed = 0
                # Wait for ml process doing resetting jobs
                comm.wait_ml_ready(self._ml_name)

        pygame.quit()