Ejemplo n.º 1
0
 def setup(self):
     """Initialise the class."""
     self.env = gym.GoalEnv()
     configuration = ConnectionConfig(
         connection_id=GymConnection.connection_id)
     self.my_address = "my_key"
     identity = Identity("name", address=self.my_address)
     self.gym_con = GymConnection(gym_env=self.env,
                                  identity=identity,
                                  configuration=configuration)
     self.loop = asyncio.get_event_loop()
Ejemplo n.º 2
0
 def setup(self):
     """Initialise the class."""
     self.env = gym.GoalEnv()
     configuration = ConnectionConfig(
         connection_id=GymConnection.connection_id)
     self.agent_address = "my_address"
     identity = Identity("name", address=self.agent_address)
     self.gym_con = GymConnection(
         gym_env=self.env,
         identity=identity,
         configuration=configuration,
         data_dir=MagicMock(),
     )
     self.loop = asyncio.get_event_loop()
     self.gym_address = str(GymConnection.connection_id)
     self.dialogues = GymDialogues(self.agent_address)
Ejemplo n.º 3
0
def test_gym_from_config():
    """Test the Connection from config File."""
    conf = ConnectionConfig()
    conf.config["env"] = "tests.conftest.DUMMY_ENV"
    con = GymConnection.from_config(address="pk",
                                    connection_configuration=conf)
    assert con is not None
Ejemplo n.º 4
0
    def __init__(self, name: str, gym_env: gym.Env,
                 proxy_env_queue: Queue) -> None:
        """
        Instantiate the agent.

        :param name: the name of the agent
        :param gym_env: gym environment
        :param proxy_env_queue: the queue of the proxy environment
        :return: None
        """
        identity = Identity(name, ADDRESS)
        configuration = ConnectionConfig(
            connection_id=GymConnection.connection_id)
        super().__init__(
            identity,
            [
                GymConnection(
                    gym_env,
                    identity=identity,
                    configuration=configuration,
                    data_dir=os.getcwd(),
                )
            ],
            period=0.01,
        )
        self.proxy_env_queue = proxy_env_queue
Ejemplo n.º 5
0
 def setup_class(cls):
     """Initialise the class."""
     cls.env = gym.GoalEnv()
     cls.gym_con = GymConnection(
         address="my_key",
         gym_env=cls.env,
         connection_id=PublicId("fetchai", "gym", "0.1.0"),
     )
Ejemplo n.º 6
0
 def test_gym_env_load(self):
     """Load gym env from file."""
     curdir = os.getcwd()
     os.chdir(os.path.join(ROOT_DIR, "examples", "gym_ex"))
     gym_env_path = "gyms.env.BanditNArmedRandom"
     configuration = ConnectionConfig(
         connection_id=GymConnection.connection_id, env=gym_env_path)
     identity = Identity("name", address=self.my_address)
     gym_con = GymConnection(gym_env=None,
                             identity=identity,
                             configuration=configuration)
     assert gym_con.channel.gym_env is not None
     os.chdir(curdir)
Ejemplo n.º 7
0
    def __init__(self, name: str, gym_env: gym.Env, proxy_env_queue: Queue) -> None:
        """
        Instantiate the agent.

        :param name: the name of the agent
        :param gym_env: gym environment
        :param proxy_env_queue: the queue of the proxy environment
        :return: None
        """
        identity = Identity(name, ADDRESS)
        super().__init__(
            identity, [GymConnection(identity.address, gym_env)], timeout=0,
        )
        self.proxy_env_queue = proxy_env_queue
Ejemplo n.º 8
0
class TestGymConnection:
    """Test the packages/connection/gym/connection.py."""
    def setup(self):
        """Initialise the class."""
        self.env = gym.GoalEnv()
        configuration = ConnectionConfig(
            connection_id=GymConnection.connection_id)
        self.agent_address = "my_address"
        identity = Identity("name", address=self.agent_address)
        self.gym_con = GymConnection(
            gym_env=self.env,
            identity=identity,
            configuration=configuration,
            data_dir=MagicMock(),
        )
        self.loop = asyncio.get_event_loop()
        self.gym_address = str(GymConnection.connection_id)
        self.dialogues = GymDialogues(self.agent_address)

    def teardown(self):
        """Clean up after tests."""
        self.loop.run_until_complete(self.gym_con.disconnect())

    @pytest.mark.asyncio
    async def test_gym_connection_connect(self):
        """Test the connection None return value after connect()."""
        assert self.gym_con.channel._queue is None
        await self.gym_con.channel.connect()
        assert self.gym_con.channel._queue is not None

    @pytest.mark.asyncio
    async def test_decode_envelope_error(self):
        """Test the decoding error for the envelopes."""
        await self.gym_con.connect()
        envelope = Envelope(
            to=self.gym_address,
            sender=self.agent_address,
            protocol_specification_id=UNKNOWN_PROTOCOL_PUBLIC_ID,
            message=b"hello",
        )

        with pytest.raises(ValueError):
            await self.gym_con.send(envelope)

    @pytest.mark.asyncio
    async def test_send_connection_error(self):
        """Test send connection error."""
        msg, sending_dialogue = self.dialogues.create(
            counterparty=self.gym_address,
            performative=GymMessage.Performative.RESET,
        )
        envelope = Envelope(
            to=msg.to,
            sender=msg.sender,
            message=msg,
        )

        with pytest.raises(ConnectionError):
            await self.gym_con.send(envelope)

    @pytest.mark.asyncio
    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,
            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

    @pytest.mark.asyncio
    async def test_send_reset(self):
        """Test send reset message."""
        _ = await self.send_reset()

    @pytest.mark.asyncio
    async def test_send_close(self):
        """Test send close message."""
        sending_dialogue = await self.send_reset()
        assert sending_dialogue.last_message is not None
        msg = sending_dialogue.reply(
            performative=GymMessage.Performative.CLOSE, )
        envelope = Envelope(
            to=msg.to,
            sender=msg.sender,
            message=msg,
        )
        await self.gym_con.connect()

        with patch.object(self.env, "close") as mock:
            await self.gym_con.send(envelope)
            mock.assert_called()

    @pytest.mark.asyncio
    async def test_send_close_negative(self):
        """Test send close message with invalid reference and message id and target."""
        incorrect_msg = GymMessage(
            performative=GymMessage.Performative.CLOSE,
            dialogue_reference=self.dialogues.
            new_self_initiated_dialogue_reference(),
        )
        incorrect_msg.to = self.gym_address
        incorrect_msg.sender = self.agent_address

        # the incorrect message cannot be sent into a dialogue, so this is omitted.

        envelope = Envelope(
            to=incorrect_msg.to,
            sender=incorrect_msg.sender,
            protocol_specification_id=incorrect_msg.protocol_specification_id,
            message=incorrect_msg,
        )
        await self.gym_con.connect()

        with patch.object(self.gym_con.channel.logger,
                          "warning") as mock_logger:
            await self.gym_con.send(envelope)
            mock_logger.assert_any_call(
                f"Could not create dialogue from message={incorrect_msg}")

    async def send_reset(self) -> GymDialogue:
        """Send a reset."""
        msg, sending_dialogue = self.dialogues.create(
            counterparty=self.gym_address,
            performative=GymMessage.Performative.RESET,
        )
        assert sending_dialogue is not None
        envelope = Envelope(
            to=msg.to,
            sender=msg.sender,
            message=msg,
        )
        await self.gym_con.connect()

        with patch.object(self.env, "reset") 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.STATUS
        assert response_msg.content == {"reset": "success"}
        assert sending_dialogue == response_dialogue
        return sending_dialogue

    @pytest.mark.asyncio
    async def test_receive_connection_error(self):
        """Test receive connection error and Cancel Error."""
        with pytest.raises(ConnectionError):
            await self.gym_con.receive()

    def test_gym_env_load(self):
        """Load gym env from file."""
        curdir = os.getcwd()
        os.chdir(os.path.join(ROOT_DIR, "examples", "gym_ex"))
        gym_env_path = "gyms.env.BanditNArmedRandom"
        configuration = ConnectionConfig(
            connection_id=GymConnection.connection_id, env=gym_env_path)
        identity = Identity("name", address=self.agent_address)
        gym_con = GymConnection(
            gym_env=None,
            identity=identity,
            configuration=configuration,
            data_dir=MagicMock(),
        )
        assert gym_con.channel.gym_env is not None
        os.chdir(curdir)
Ejemplo n.º 9
0
class TestGymConnection:
    """Test the packages/connection/gym/connection.py."""
    def setup(self):
        """Initialise the class."""
        self.env = gym.GoalEnv()
        configuration = ConnectionConfig(
            connection_id=GymConnection.connection_id)
        self.my_address = "my_key"
        identity = Identity("name", address=self.my_address)
        self.gym_con = GymConnection(gym_env=self.env,
                                     identity=identity,
                                     configuration=configuration)
        self.loop = asyncio.get_event_loop()

    def teardown(self):
        """Clean up after tests."""
        self.loop.run_until_complete(self.gym_con.disconnect())

    @pytest.mark.asyncio
    async def test_gym_connection_connect(self):
        """Test the connection None return value after connect()."""
        assert self.gym_con.channel._queue is None
        await self.gym_con.channel.connect()
        assert self.gym_con.channel._queue is not None

    @pytest.mark.asyncio
    async def test_decode_envelope_error(self):
        """Test the decoding error for the envelopes."""
        await self.gym_con.connect()
        envelope = Envelope(
            to="_to_key",
            sender=self.my_address,
            protocol_id=UNKNOWN_PROTOCOL_PUBLIC_ID,
            message=b"hello",
        )

        with pytest.raises(ValueError):
            await self.gym_con.send(envelope)

    @pytest.mark.asyncio
    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)

    @pytest.mark.asyncio
    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

    @pytest.mark.asyncio
    async def test_send_reset(self):
        """Test send reset message."""
        msg = GymMessage(performative=GymMessage.Performative.RESET, )
        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 pytest.raises(gym.error.Error):
            await self.gym_con.send(envelope)

        with pytest.raises(asyncio.TimeoutError):
            await asyncio.wait_for(self.gym_con.receive(), timeout=0.5)

    @pytest.mark.asyncio
    async def test_send_close(self):
        """Test send close message."""
        msg = GymMessage(performative=GymMessage.Performative.CLOSE, )
        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()

        await self.gym_con.send(envelope)

        with pytest.raises(asyncio.TimeoutError):
            await asyncio.wait_for(self.gym_con.receive(), timeout=0.5)

    @pytest.mark.asyncio
    async def test_receive_connection_error(self):
        """Test receive connection error and Cancel Error."""
        with pytest.raises(ConnectionError):
            await self.gym_con.receive()

    def test_gym_env_load(self):
        """Load gym env from file."""
        curdir = os.getcwd()
        os.chdir(os.path.join(ROOT_DIR, "examples", "gym_ex"))
        gym_env_path = "gyms.env.BanditNArmedRandom"
        configuration = ConnectionConfig(
            connection_id=GymConnection.connection_id, env=gym_env_path)
        identity = Identity("name", address=self.my_address)
        gym_con = GymConnection(gym_env=None,
                                identity=identity,
                                configuration=configuration)
        assert gym_con.channel.gym_env is not None
        os.chdir(curdir)
Ejemplo n.º 10
0
 def setup_class(cls):
     """Initialise the class."""
     cls.env = gym.GoalEnv()
     cls.gym_con = GymConnection(gym_env=cls.env, address="my_key")
     cls.gym_con.channel = GymChannel("my_key", gym.GoalEnv())
     cls.gym_con._connection = None