class ActorWorker(object):
    def __init__(self, local_actor: AbsActor, proxy_params):
        self._local_actor = local_actor
        self._proxy = Proxy(component_type="actor_worker", **proxy_params)
        self._registry_table = RegisterTable(self._proxy.get_peers)
        self._registry_table.register_event_handler("actor:rollout:1", self.on_rollout_request)

    def on_rollout_request(self, message):
        data = message.payload
        if data.get(PayloadKey.DONE, False):
            sys.exit(0)

        performance, experiences = self._local_actor.roll_out(model_dict=data[PayloadKey.MODEL],
                                                              epsilon_dict=data[PayloadKey.EPSILON],
                                                              return_details=data[PayloadKey.RETURN_DETAILS])

        self._proxy.reply(received_message=message,
                          tag=MessageTag.UPDATE,
                          payload={PayloadKey.PERFORMANCE: performance,
                                   PayloadKey.EXPERIENCE: experiences}
                          )

    def launch(self):
        """
        This launches an ActorWorker instance.
        """
        for msg in self._proxy.receive():
            self._registry_table.push(msg)
            triggered_events = self._registry_table.get()
            for handler_fn, cached_messages in triggered_events:
                handler_fn(cached_messages)
class ActorWorker(object):
    """A ``AbsActor`` wrapper that accepts roll-out requests and performs roll-out tasks.

    Args:
        local_actor: An ``AbsActor`` instance.
        proxy_params: Parameters for instantiating a ``Proxy`` instance.
    """
    def __init__(self, local_actor: AbsActor, proxy_params):
        self._local_actor = local_actor
        self._proxy = Proxy(component_type="actor_worker", **proxy_params)
        self._registry_table = RegisterTable(self._proxy.get_peers)
        self._registry_table.register_event_handler("actor:rollout:1",
                                                    self.on_rollout_request)

    def on_rollout_request(self, message):
        """Perform local roll-out and send the results back to the request sender.

        Args:
            message: Message containing roll-out parameters and options.
        """
        data = message.payload
        if data.get(PayloadKey.DONE, False):
            sys.exit(0)

        performance, experiences = self._local_actor.roll_out(
            model_dict=data[PayloadKey.MODEL],
            epsilon_dict=data[PayloadKey.EPSILON],
            return_details=data[PayloadKey.RETURN_DETAILS])

        self._proxy.reply(received_message=message,
                          tag=MessageTag.UPDATE,
                          payload={
                              PayloadKey.PERFORMANCE: performance,
                              PayloadKey.EXPERIENCE: experiences
                          })

    def launch(self):
        """Entry point method.

        This enters the actor into an infinite loop of listening to requests and handling them according to the
        register table. In this case, the only type of requests the actor needs to handle is roll-out requests.
        """
        for msg in self._proxy.receive():
            self._registry_table.push(msg)
            triggered_events = self._registry_table.get()
            for handler_fn, cached_messages in triggered_events:
                handler_fn(cached_messages)
Пример #3
0
def worker(group_name):
    """
    The main worker logic includes initialize proxy and handle jobs from the master.

    Args:
        group_name (str): Identifier for the group of all communication components
    """
    proxy = Proxy(group_name=group_name,
                  component_type="worker",
                  expected_peers={"master": 1})

    # Nonrecurring receive the message from the proxy.
    for msg in proxy.receive(is_continuous=False):
        print(f"{proxy.component_name} receive message from {msg.source}. the payload is {msg.payload}.")

        if msg.tag == "sum":
            replied_payload = sum(msg.payload)
            proxy.reply(received_message=msg, tag="sum", payload=replied_payload)
Пример #4
0
def actor_init(queue, redis_port):
    proxy = Proxy(component_type="actor",
                  expected_peers={"master": 1},
                  redis_address=("localhost", redis_port),
                  **PROXY_PARAMETER)

    # Continuously receive messages from proxy.
    for msg in proxy.receive(is_continuous=True):
        print(f"receive message from master. {msg.tag}")
        if msg.tag == "cont":
            proxy.reply(message=msg, tag="recv", payload="successful receive!")
        elif msg.tag == "stop":
            proxy.reply(message=msg,
                        tag="recv",
                        payload=f"{proxy.name} exited!")
            queue.put(proxy.name)
            break
        elif msg.tag == "finish":
            proxy.reply(message=msg,
                        tag="recv",
                        payload=f"{proxy.name} finish!")
            sys.exit(0)

    proxy.close()
    sys.exit(1)
Пример #5
0
def worker(group_name):
    """
    The main worker logic includes initialize proxy and handle jobs from the master.

    Args:
        group_name (str): Identifier for the group of all communication components.
    """
    proxy = Proxy(group_name=group_name,
                  component_type="worker",
                  expected_peers={"master": 1})
    counter = 0
    print(f"{proxy.component_name}'s counter is {counter}.")

    # nonrecurring receive the message from the proxy.
    for msg in proxy.receive(is_continuous=False):
        print(f"{proxy.component_name} receive message from {msg.source}.")

        if msg.tag == "INC":
            counter += 1
            print(
                f"{proxy.component_name} receive INC request, {proxy.component_name}'s count is {counter}."
            )
            proxy.reply(received_message=msg, tag="done")
class ActorWorker(object):
    def __init__(self, local_actor, proxy_params):
        self._local_actor = local_actor
        self._proxy = Proxy(component_type="actor_worker", **proxy_params)
        self._registry_table = RegisterTable(self._proxy.get_peers)
        self._registry_table.register_event_handler("actor_proxy:rollout:1",
                                                    self.on_rollout_request)

    def on_rollout_request(self, message):
        if message.payload[PayloadKey.RolloutMode] == RolloutMode.EXIT:
            sys.exit(0)

        data = message.payload
        performance, exp_by_agent = self._local_actor.roll_out(
            mode=data[PayloadKey.RolloutMode],
            models=data[PayloadKey.MODEL],
            epsilon_dict=data[PayloadKey.EPSILON],
            seed=data[PayloadKey.SEED])

        self._proxy.reply(received_message=message,
                          tag=MessageType.UPDATE,
                          payload={
                              PayloadKey.PERFORMANCE: performance["local"],
                              PayloadKey.EXPERIENCE: exp_by_agent
                          })

    def launch(self):
        # TODO
        """
        Launches the an ActorWorker instance
        """
        for msg in self._proxy.receive():
            self._registry_table.push(msg)
            triggered_events = self._registry_table.get()
            for handler_fn, cached_messages in triggered_events:
                handler_fn(cached_messages)