当前位置: 首页>>代码示例>>Python>>正文


Python State.add_object方法代码示例

本文整理汇总了Python中State.State.add_object方法的典型用法代码示例。如果您正苦于以下问题:Python State.add_object方法的具体用法?Python State.add_object怎么用?Python State.add_object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在State.State的用法示例。


在下文中一共展示了State.add_object方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ALEInterface

# 需要导入模块: from State import State [as 别名]
# 或者: from State.State import add_object [as 别名]
class ALEInterface():
    """Provides an interface to the C++ code through IPC."""
    
    def __init__(self, rom, disp_screen = False):
        """
        Tries to open up a subprocess for IPC with the C++ program.
        If successful, interaction will begin with initial data transaction.
        Also sets up the state, time, and reward variables.
        """
        self.t = 0
        self.valid_actions = []
        self.game_running = False
        self.cur_state = None
        self.last_reward = None
        self.disp_screen = disp_screen
        self.screen_width = 0
        self.screen_height = 0
        cmd = EXEC + " " + rom
        try:
            self.proc = subprocess.Popen(cmd.split(),
                stdout = subprocess.PIPE, stdin = subprocess.PIPE)
            self.connect(disp_screen)
        except:
            self.proc = None
            print "Error: failed to run command '" + cmd + "'"
        print "Running", rom, "(screen = " + str(disp_screen) + ")"

    def writeline(self, msg):
        """Writes a single line out through the pipe."""
        if self.proc is not None:
            self.proc.stdin.write(msg + "\n")
            self.proc.stdin.flush()

    def readline(self):
        """Returns a line from the pipe with the trailing newline removed."""
        if self.proc is not None:
            return self.proc.stdout.readline().rstrip()
        else:
            return ""

    def get_next_message(self):
        """
        Returns the next message sent by the C++ code as a list of lines.
        Any output received before the message is ignored.
        """
        line = self.readline()
        while line != Protocol.MESSAGE_START:
            line = self.readline()
        message = []
        line = self.readline()
        while line != Protocol.MESSAGE_END:
            message.append(line)
            line = self.readline()
        return message

    def send_message(self, messages):
        """
        Sends the given list of messages to the C++ code.
        If you're only sending one message, pass it as a list anyway.
        """
        self.writeline(Protocol.MESSAGE_START)
        for msg in messages:
            self.writeline(msg)
        self.writeline(Protocol.MESSAGE_END)

    def connect(self, disp_screen):
        """Establishes connection with the C++ code and receives valid actions."""
        greeting = self.get_next_message()
        print '\n'.join(greeting)
        self.send_message(str(disp_screen))
        actions = self.get_next_message()
        self.valid_actions = map(int, actions)
        if disp_screen:
            self.screen_width = int(self.get_next_message()[0])
            self.screen_height = int(self.get_next_message()[0])

    def recv_state(self):
        """Reads a state from C++ and returns the State object."""
        obj_params = self.get_next_message()
        if Protocol.END_GAME in obj_params:
            self.game_running = False
            return False
        self.cur_state = State(self.t)
        num_objs = len(obj_params)/9 # TODO - define 9 elsewhere?
        for i in range(num_objs):
            obj_indx = i*9
            obj = ALEObject(obj_params[obj_indx:obj_indx+9])
            self.cur_state.add_object(obj)
        self.t += 1
        return True

    def send_action_get_reward(self, action):
        """Sends the selected action and gets the reward."""
        self.send_message([str(action)])
        self.last_reward = float(self.get_next_message()[0])

    def get_valid_actions(self):
        """Returns a list of valid actions."""
        return self.valid_actions

#.........这里部分代码省略.........
开发者ID:DiNAi,项目名称:nn2014-RL-atari,代码行数:103,代码来源:ALEInterface.py


注:本文中的State.State.add_object方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。