コード例 #1
0
    def handle_gym_message(self, envelope: Envelope) -> None:
        """
        Forward a message to gym.

        :param envelope: the envelope
        :return: None
        """
        gym_message = GymSerializer().decode(envelope.message)
        performative = gym_message.get("performative")
        if GymMessage.Performative(
                performative) == GymMessage.Performative.ACT:
            action = gym_message.get("action")
            step_id = gym_message.get("step_id")
            observation, reward, done, info = self.gym_env.step(
                action)  # type: ignore
            msg = GymMessage(performative=GymMessage.Performative.PERCEPT,
                             observation=observation,
                             reward=reward,
                             done=done,
                             info=info,
                             step_id=step_id)
            msg_bytes = GymSerializer().encode(msg)
            envelope = Envelope(to=envelope.sender,
                                sender=DEFAULT_GYM,
                                protocol_id=GymMessage.protocol_id,
                                message=msg_bytes)
            self._send(envelope)
        elif GymMessage.Performative(
                performative) == GymMessage.Performative.RESET:
            self.gym_env.reset()  # type: ignore
        elif GymMessage.Performative(
                performative) == GymMessage.Performative.CLOSE:
            self.gym_env.close()  # type: ignore
コード例 #2
0
ファイル: serialization.py プロジェクト: Physsix27/agents-aea
    def decode(self, obj: bytes) -> Message:
        """
        Decode the message.

        :param obj: the bytes object
        :return: the message
        """
        json_msg = json.loads(obj.decode("utf-8"))
        performative = GymMessage.Performative(json_msg["performative"])
        new_body = copy.copy(json_msg)
        new_body["type"] = performative

        if performative == GymMessage.Performative.ACT:
            action_bytes = base64.b64decode(json_msg["action"])
            action = pickle.loads(action_bytes)
            new_body["action"] = action
            new_body["step_id"] = json_msg["step_id"]
        elif performative == GymMessage.Performative.PERCEPT:
            # observation, reward and info are gym implementation specific, done is boolean
            observation_bytes = base64.b64decode(json_msg["observation"])
            observation = pickle.loads(observation_bytes)
            new_body["observation"] = observation
            reward_bytes = base64.b64decode(json_msg["reward"])
            reward = pickle.loads(reward_bytes)
            new_body["reward"] = reward
            info_bytes = base64.b64decode(json_msg["info"])
            info = pickle.loads(info_bytes)
            new_body["info"] = info
            new_body["step_id"] = json_msg["step_id"]

        gym_message = GymMessage(performative=performative, body=new_body)
        return gym_message
コード例 #3
0
ファイル: serialization.py プロジェクト: Physsix27/agents-aea
    def encode(self, msg: Message) -> bytes:
        """
        Decode the message.

        :param msg: the message object
        :return: the bytes
        """
        performative = GymMessage.Performative(msg.get("performative"))
        new_body = copy.copy(msg.body)
        new_body["performative"] = performative.value

        if performative == GymMessage.Performative.ACT:
            action = msg.body["action"]  # type: Any
            action_bytes = base64.b64encode(
                pickle.dumps(action)).decode("utf-8")
            new_body["action"] = action_bytes
            new_body["step_id"] = msg.body["step_id"]
        elif performative == GymMessage.Performative.PERCEPT:
            # observation, reward and info are gym implementation specific, done is boolean
            observation = msg.body["observation"]  # type: Any
            observation_bytes = base64.b64encode(
                pickle.dumps(observation)).decode("utf-8")
            new_body["observation"] = observation_bytes
            reward = msg.body["reward"]  # type: Any
            reward_bytes = base64.b64encode(
                pickle.dumps(reward)).decode("utf-8")
            new_body["reward"] = reward_bytes
            info = msg.body["info"]  # type: Any
            info_bytes = base64.b64encode(pickle.dumps(info)).decode("utf-8")
            new_body["info"] = info_bytes
            new_body["step_id"] = msg.body["step_id"]

        gym_message_bytes = json.dumps(new_body).encode("utf-8")
        return gym_message_bytes
コード例 #4
0
ファイル: test_gym.py プロジェクト: 8ball030/agents-aea
    async def test_send_connection_error(self):
        """Test send connection error."""
        msg = GymMessage(performative=GymMessage.Performative.ACT, action='any_action', step_id=1)
        msg_bytes = GymSerializer().encode(msg)
        envelope = Envelope(to="_to_key", sender="_from_key", protocol_id=GymMessage.protocol_id,
                            message=msg_bytes)

        self.gym_con.connection_status.is_connected = False
        with pytest.raises(ConnectionError):
            await self.gym_con.send(envelope)
コード例 #5
0
ファイル: test_gym.py プロジェクト: 8ball030/agents-aea
def test_gym_serialization():
    """Test that the serialization for the 'simple' protocol works for the ERROR message."""
    msg = GymMessage(performative=GymMessage.Performative.ACT,
                     action='any_action',
                     step_id=1)
    msg_bytes = GymSerializer().encode(msg)
    actual_msg = GymSerializer().decode(msg_bytes)
    expected_msg = msg
    assert expected_msg == actual_msg

    msg = GymMessage(performative=GymMessage.Performative.PERCEPT,
                     observation='any_observation',
                     reward=0.0,
                     info={'some_key': 'some_value'},
                     done=True,
                     step_id=1)
    msg_bytes = GymSerializer().encode(msg)
    actual_msg = GymSerializer().decode(msg_bytes)
    expected_msg = msg
    assert expected_msg == actual_msg
コード例 #6
0
ファイル: helpers.py プロジェクト: 8ball030/agents-aea
    def close(self) -> None:
        """
        Close the environment.

        :return: None
        """
        self._is_rl_agent_trained = True
        gym_msg = GymMessage(performative=GymMessage.Performative.CLOSE)
        gym_bytes = GymSerializer().encode(gym_msg)
        envelope = Envelope(to=DEFAULT_GYM,
                            sender=self._skill_context.agent_public_key,
                            protocol_id=GymMessage.protocol_id,
                            message=gym_bytes)
        self._skill_context.outbox.put(envelope)
コード例 #7
0
ファイル: helpers.py プロジェクト: 8ball030/agents-aea
    def reset(self) -> None:
        """
        Reset the environment.

        :return: None
        """
        self._step_count = 0
        self._is_rl_agent_trained = False
        gym_msg = GymMessage(performative=GymMessage.Performative.RESET)
        gym_bytes = GymSerializer().encode(gym_msg)
        envelope = Envelope(to=DEFAULT_GYM,
                            sender=self._skill_context.agent_public_key,
                            protocol_id=GymMessage.protocol_id,
                            message=gym_bytes)
        self._skill_context.outbox.put(envelope)
コード例 #8
0
ファイル: helpers.py プロジェクト: 8ball030/agents-aea
    def _encode_action(self, action: Action, step_id: int) -> Envelope:
        """
        Encode the 'action' sent to the step function as one or several envelopes.

        :param action: the action that is the output of an RL algorithm.
        :param step_id: the step id
        :return: an envelope
        """
        gym_msg = GymMessage(performative=GymMessage.Performative.ACT,
                             action=action,
                             step_id=step_id)
        gym_bytes = GymSerializer().encode(gym_msg)
        envelope = Envelope(to=DEFAULT_GYM,
                            sender=self._skill_context.agent_public_key,
                            protocol_id=GymMessage.protocol_id,
                            message=gym_bytes)
        return envelope
コード例 #9
0
    def handle_envelope(self, envelope: Envelope) -> None:
        """
        Handle envelopes.

        :param envelope: the envelope
        :return: None
        """
        gym_msg = GymSerializer().decode(envelope.message)
        gym_msg_performative = GymMessage.Performative(
            gym_msg.get("performative"))
        if gym_msg_performative == GymMessage.Performative.PERCEPT:
            assert self.context.tasks is not None, "Incorrect initialization."
            assert len(self.context.tasks) == 1, "Too many tasks loaded!"
            gym_task = cast(GymTask, self.context.tasks[0])
            gym_task.proxy_env_queue.put(gym_msg)
        else:
            raise ValueError(
                "Unexpected performative or no step_id: {}".format(
                    gym_msg_performative))
コード例 #10
0
    def handle(self, message: Message, sender: str) -> None:
        """
        Handle messages.

        :param message: the message
        :param sender: the sender
        :return: None
        """
        gym_msg = cast(GymMessage, message)
        gym_msg_performative = GymMessage.Performative(
            gym_msg.get("performative"))
        if gym_msg_performative == GymMessage.Performative.PERCEPT:
            assert self.context.tasks is not None, "Incorrect initialization."
            assert len(self.context.tasks) == 1, "Too many tasks loaded!"
            gym_task = cast(GymTask, self.context.tasks[0])
            gym_task.proxy_env_queue.put(gym_msg)
        else:
            raise ValueError(
                "Unexpected performative or no step_id: {}".format(
                    gym_msg_performative))
コード例 #11
0
ファイル: test_gym.py プロジェクト: 8ball030/agents-aea
def test_gym_message_instantiation():
    """Test instantiation of the gym message."""
    assert GymMessage(performative=GymMessage.Performative.ACT,
                      action='any_action',
                      step_id=1)
    assert GymMessage(performative=GymMessage.Performative.PERCEPT,
                      observation='any_observation',
                      reward=0.0,
                      info={'some_key': 'some_value'},
                      done=True,
                      step_id=1)
    assert GymMessage(performative=GymMessage.Performative.RESET)
    assert GymMessage(performative=GymMessage.Performative.CLOSE)
    assert str(GymMessage.Performative.CLOSE) == 'close'

    msg = GymMessage(performative=GymMessage.Performative.ACT,
                     action='any_action',
                     step_id=1)
    with mock.patch('packages.protocols.gym.message.GymMessage.Performative'
                    ) as mocked_type:
        mocked_type.ACT.value = "unknown"
        assert not msg.check_consistency(), \
            "Expect the consistency to return False"