def test_percept_serialization(): """Test the serialization for 'percept' speech-act works.""" msg = GymMessage( message_id=2, dialogue_reference=(str(0), ""), target=1, performative=GymMessage.Performative.PERCEPT, step_id=1, observation=GymMessage.AnyObject("some_observation"), reward=10.0, done=False, info=GymMessage.AnyObject("some_info"), ) msg.to = "receiver" envelope = Envelope( to=msg.to, sender="sender", message=msg, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope.to == actual_envelope.to assert expected_envelope.sender == actual_envelope.sender assert (expected_envelope.protocol_specification_id == actual_envelope.protocol_specification_id) assert expected_envelope.message != actual_envelope.message actual_msg = GymMessage.serializer.decode(actual_envelope.message) actual_msg.to = actual_envelope.to actual_msg.sender = actual_envelope.sender expected_msg = msg assert expected_msg == actual_msg
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) gym_message = cast(GymMessage, gym_message) if gym_message.performative == GymMessage.Performative.ACT: action = gym_message.action.any step_id = gym_message.step_id observation, reward, done, info = self.gym_env.step(action) # type: ignore msg = GymMessage( performative=GymMessage.Performative.PERCEPT, observation=GymMessage.AnyObject(observation), reward=reward, done=done, info=GymMessage.AnyObject(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 gym_message.performative == GymMessage.Performative.RESET: self.gym_env.reset() # type: ignore elif gym_message.performative == GymMessage.Performative.CLOSE: self.gym_env.close() # type: ignore
async def handle_gym_message(self, envelope: Envelope) -> None: """ Forward a message to gym. :param envelope: the envelope :return: None """ assert isinstance(envelope.message, GymMessage), "Message not of type GymMessage" gym_message, dialogue = self._get_message_and_dialogue(envelope) if dialogue is None: self.logger.warning( "Could not create dialogue from message={}".format( gym_message)) return if gym_message.performative == GymMessage.Performative.ACT: action = gym_message.action.any step_id = gym_message.step_id observation, reward, done, info = await self._run_in_executor( self.gym_env.step, action) msg = GymMessage( performative=GymMessage.Performative.PERCEPT, observation=GymMessage.AnyObject(observation), reward=reward, done=done, info=GymMessage.AnyObject(info), step_id=step_id, target=gym_message.message_id, message_id=gym_message.message_id + 1, dialogue_reference=dialogue.dialogue_label.dialogue_reference, ) elif gym_message.performative == GymMessage.Performative.RESET: await self._run_in_executor(self.gym_env.reset) msg = GymMessage( performative=GymMessage.Performative.STATUS, content={"reset": "success"}, target=gym_message.message_id, message_id=gym_message.message_id + 1, dialogue_reference=dialogue.dialogue_label.dialogue_reference, ) elif gym_message.performative == GymMessage.Performative.CLOSE: await self._run_in_executor(self.gym_env.close) return msg.counterparty = gym_message.counterparty assert dialogue.update(msg), "Error during dialogue update." envelope = Envelope( to=msg.counterparty, sender=msg.sender, protocol_id=msg.protocol_id, message=msg, ) await self._send(envelope)
async def handle_gym_message(self, envelope: Envelope) -> None: """ Forward a message to gym. :param envelope: the envelope :return: None """ enforce(isinstance(envelope.message, GymMessage), "Message not of type GymMessage") gym_message, dialogue = self._get_message_and_dialogue(envelope) if dialogue is None: self.logger.warning( "Could not create dialogue from message={}".format( gym_message)) return if gym_message.performative == GymMessage.Performative.ACT: action = gym_message.action.any step_id = gym_message.step_id observation, reward, done, info = await self._run_in_executor( self.gym_env.step, action) msg = dialogue.reply( performative=GymMessage.Performative.PERCEPT, target_message=gym_message, observation=GymMessage.AnyObject(observation), reward=reward, done=done, info=GymMessage.AnyObject(info), step_id=step_id, ) elif gym_message.performative == GymMessage.Performative.RESET: await self._run_in_executor(self.gym_env.reset) msg = dialogue.reply( performative=GymMessage.Performative.STATUS, target_message=gym_message, content={"reset": "success"}, ) elif gym_message.performative == GymMessage.Performative.CLOSE: await self._run_in_executor(self.gym_env.close) return envelope = Envelope( to=msg.to, sender=msg.sender, protocol_id=msg.protocol_id, message=msg, ) await self._send(envelope)
def test_act_serialization(): """Test the serialization for 'act' speech-act works.""" msg = GymMessage( message_id=1, dialogue_reference=(str(0), ""), target=0, performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("some_action"), step_id=1, ) msg.to = "receiver" envelope = Envelope( to=msg.to, sender="sender", message=msg, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope.to == actual_envelope.to assert expected_envelope.sender == actual_envelope.sender assert (expected_envelope.protocol_specification_id == actual_envelope.protocol_specification_id) assert expected_envelope.message != actual_envelope.message actual_msg = GymMessage.serializer.decode(actual_envelope.message) actual_msg.to = actual_envelope.to actual_msg.sender = actual_envelope.sender expected_msg = msg assert expected_msg == actual_msg
def test_gym_message_instantiation(): """Test instantiation of the gym message.""" assert GymMessage( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("any_action"), step_id=1, ) assert GymMessage( performative=GymMessage.Performative.PERCEPT, observation=GymMessage.AnyObject("any_observation"), reward=0.0, info=GymMessage.AnyObject({"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"
async def test_send_act(self): """Test send act message.""" sending_dialogue = await self.send_reset() last_message = sending_dialogue.last_message assert last_message is not None msg = GymMessage( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("any_action"), step_id=1, dialogue_reference=sending_dialogue.dialogue_label. dialogue_reference, message_id=last_message.message_id + 1, target=last_message.message_id, ) msg.counterparty = self.gym_address assert sending_dialogue.update(msg) envelope = Envelope( to=msg.counterparty, sender=msg.sender, protocol_id=msg.protocol_id, message=msg, ) await self.gym_con.connect() observation = 1 reward = 1.0 done = True info = "some info" with patch.object(self.env, "step", return_value=(observation, reward, done, info)) as mock: await self.gym_con.send(envelope) mock.assert_called() response = await asyncio.wait_for(self.gym_con.receive(), timeout=3) response_msg_orig = cast(GymMessage, response.message) response_msg = copy.copy(response_msg_orig) response_msg.is_incoming = True response_msg.counterparty = response_msg_orig.sender response_dialogue = self.dialogues.update(response_msg) assert response_msg.performative == GymMessage.Performative.PERCEPT assert response_msg.step_id == msg.step_id assert response_msg.observation.any == observation assert response_msg.reward == reward assert response_msg.done == done assert response_msg.info.any == info assert sending_dialogue == response_dialogue
def test_gym_serialization(): """Test that the serialization for the 'simple' protocol works for the ERROR message.""" msg = GymMessage( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("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=GymMessage.AnyObject("any_observation"), reward=0.0, info=GymMessage.AnyObject({"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
async def handle_gym_message(self, envelope: Envelope) -> None: """ Forward a message to gym. :param envelope: the envelope :return: None """ assert isinstance(envelope.message, GymMessage), "Message not of type GymMessage" gym_message = cast(GymMessage, envelope.message) if gym_message.performative == GymMessage.Performative.ACT: action = gym_message.action.any step_id = gym_message.step_id observation, reward, done, info = await self._run_in_executor( self.gym_env.step, action) msg = GymMessage( performative=GymMessage.Performative.PERCEPT, observation=GymMessage.AnyObject(observation), reward=reward, done=done, info=GymMessage.AnyObject(info), step_id=step_id, ) envelope = Envelope( to=envelope.sender, sender=DEFAULT_GYM, protocol_id=GymMessage.protocol_id, message=msg, ) await self._send(envelope) elif gym_message.performative == GymMessage.Performative.RESET: await self._run_in_executor(self.gym_env.reset) elif gym_message.performative == GymMessage.Performative.CLOSE: await self._run_in_executor(self.gym_env.close)
def _encode_and_send_action(self, action: Action, step_id: int) -> None: """ Encode the 'action' sent to the step function and send it. :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=GymMessage.AnyObject(action), step_id=step_id, ) gym_msg.counterparty = DEFAULT_GYM # Send the message via the proxy agent and to the environment self._skill_context.outbox.put_message(message=gym_msg)
async def test_send_connection_error(self): """Test send connection error.""" msg = GymMessage( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("any_action"), step_id=1, ) msg.counterparty = "_to_key" envelope = Envelope( to="_to_key", sender="_from_key", protocol_id=GymMessage.protocol_id, message=msg, ) with pytest.raises(ConnectionError): await self.gym_con.send(envelope)
async def test_send_connection_error(self): """Test send connection error.""" msg = GymMessage( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("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)
def _encode_and_send_action(self, action: Action, step_id: int) -> None: """ Encode the 'action' sent to the step function and send it. :param action: the action that is the output of an RL algorithm. :param step_id: the step id :return: an envelope """ last_msg = self.active_gym_dialogue.last_message if last_msg is None: raise ValueError("Cannot retrieve last message.") gym_msg = self.active_gym_dialogue.reply( performative=GymMessage.Performative.ACT, target_message=last_msg, action=GymMessage.AnyObject(action), step_id=step_id, ) # Send the message via the proxy agent and to the environment self._skill_context.outbox.put_message(message=gym_msg)
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=GymMessage.AnyObject(action), step_id=step_id, ) gym_bytes = GymSerializer().encode(gym_msg) envelope = Envelope( to=DEFAULT_GYM, sender=self._skill_context.agent_address, protocol_id=GymMessage.protocol_id, message=gym_bytes, ) return envelope
async def test_send_act(self): """Test send act message.""" sending_dialogue = await self.send_reset() assert sending_dialogue.last_message is not None msg = sending_dialogue.reply( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("any_action"), step_id=1, ) envelope = Envelope( to=msg.to, sender=msg.sender, protocol_id=msg.protocol_id, message=msg, ) await self.gym_con.connect() observation = 1 reward = 1.0 done = True info = "some info" with patch.object(self.env, "step", return_value=(observation, reward, done, info)) as mock: await self.gym_con.send(envelope) mock.assert_called() response = await asyncio.wait_for(self.gym_con.receive(), timeout=3) response_msg = cast(GymMessage, response.message) response_dialogue = self.dialogues.update(response_msg) assert response_msg.performative == GymMessage.Performative.PERCEPT assert response_msg.step_id == msg.step_id assert response_msg.observation.any == observation assert response_msg.reward == reward assert response_msg.done == done assert response_msg.info.any == info assert sending_dialogue == response_dialogue
def _encode_and_send_action(self, action: Action, step_id: int) -> None: """ Encode the 'action' sent to the step function and send it. :param action: the action that is the output of an RL algorithm. :param step_id: the step id :return: an envelope """ last_msg = self.active_gym_dialogue.last_message assert last_msg is not None, "Cannot retrieve last message." gym_msg = GymMessage( dialogue_reference=self.active_gym_dialogue.dialogue_label. dialogue_reference, performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject(action), step_id=step_id, message_id=last_msg.message_id + 1, target=last_msg.message_id, ) gym_msg.counterparty = self.gym_address assert self.active_gym_dialogue.update(gym_msg) # Send the message via the proxy agent and to the environment self._skill_context.outbox.put_message(message=gym_msg)
async def test_send_act(self): """Test send act message.""" msg = GymMessage( performative=GymMessage.Performative.ACT, action=GymMessage.AnyObject("any_action"), step_id=1, ) msg.counterparty = "_to_key" envelope = Envelope( to="_to_key", sender=self.my_address, protocol_id=GymMessage.protocol_id, message=msg, ) await self.gym_con.connect() with patch.object(self.env, "step", return_value=(1, 1.0, True, "some info")) as mock: await self.gym_con.send(envelope) mock.assert_called() assert await asyncio.wait_for(self.gym_con.receive(), timeout=3) is not None