def test_locate_when_path_does_not_exist(self): """Test that locate function returns None when the dotted path does not exist.""" result = locate("aea.not.existing.path") assert result is None result = locate("ThisClassDoesNotExist") assert result is None
def test_locate(self): """Test the locate function to locate modules.""" cwd = os.getcwd() os.chdir(os.path.join(CUR_PATH, "..")) assert locate("packages.fetchai.connections.gym") is not None assert locate( "packages.fetchai.connections.non_existing_connection") is None os.chdir(cwd)
def test_locate(self): """Test the locate function to locate modules.""" cwd = os.getcwd() os.chdir(os.path.join(CUR_PATH, "..")) sys.modules["gym_connection"] = locate("packages.connections.gym") assert sys.modules['gym_connection'] is not None sys.modules["gym_connection"] = locate("packages.connections.weather") assert sys.modules['gym_connection'] is None os.chdir(cwd)
def test_locate_class(self): """Test the locate function to locate classes.""" cwd = os.getcwd() os.chdir(os.path.join(CUR_PATH, "..")) expected_class = OEFConnection actual_class = locate("aea.connections.oef.connection.OEFConnection") # although they are the same class, they are different instances in memory # and the build-in default "__eq__" method does not compare the attributes. # so compare the names assert actual_class is not None assert expected_class.__name__ == actual_class.__name__ os.chdir(cwd)
def from_config( cls, public_key: str, connection_configuration: ConnectionConfig) -> 'Connection': """ Get the Gym connection from the connection configuration. :param public_key: the public key of the agent. :param connection_configuration: the connection configuration object. :return: the connection object """ gym_env_package = cast(str, connection_configuration.config.get('env')) gym_env = locate(gym_env_package) return GymConnection(public_key, gym_env())
def from_config( cls, address: Address, configuration: ConnectionConfig ) -> "Connection": """ Get the Gym connection from the connection configuration. :param address: the address of the agent. :param configuration: the connection configuration object. :return: the connection object """ gym_env_package = cast(str, configuration.config.get("env")) gym_env = locate(gym_env_package) return GymConnection(gym_env(), address=address, configuration=configuration,)
def __init__(self, gym_env: Optional[gym.Env] = None, **kwargs): """ Initialize a connection to a local gym environment. :param gym_env: the gym environment (this cannot be loaded by AEA loader). :param kwargs: the keyword arguments of the parent class. """ super().__init__(**kwargs) if gym_env is None: gym_env_package = cast(str, self.configuration.config.get("env")) assert gym_env_package is not None, "env must be set!" gym_env_class = locate(gym_env_package) gym_env = gym_env_class() self.channel = GymChannel(self.address, gym_env) self._connection = None # type: Optional[asyncio.Queue]
def from_config( cls, address: Address, connection_configuration: ConnectionConfig) -> "Connection": """ Get the Gym connection from the connection configuration. :param address: the address of the agent. :param connection_configuration: the connection configuration object. :return: the connection object """ gym_env_package = cast(str, connection_configuration.config.get("env")) gym_env = locate(gym_env_package) return GymConnection( address, gym_env(), connection_id=connection_configuration.public_id, restricted_to_protocols=connection_configuration. restricted_to_protocols, excluded_protocols=connection_configuration.excluded_protocols, )
def test_locate_with_builtins(self): """Test that locate function returns the built-in.""" result = locate("int.bit_length") assert int.bit_length == result
# # ------------------------------------------------------------------------------ """This contains the proxy agent class.""" import sys from queue import Queue from typing import Optional import gym from aea.agent import Agent from aea.helpers.base import locate from aea.identity.base import Identity from aea.mail.base import Envelope sys.modules["packages.fetchai.connections.gym"] = locate( "packages.fetchai.connections.gym") from packages.fetchai.connections.gym.connection import GymConnection # noqa: E402 ADDRESS = "some_address" class ProxyAgent(Agent): """This class implements a proxy agent to be used by a proxy environment.""" 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
import sys import time from aea.helpers.base import locate import gym from queue import Queue from threading import Thread from typing import Any, Tuple, cast from aea.crypto.wallet import DEFAULT from aea.mail.base import Envelope from aea.protocols.base import Message sys.modules["gym_connection"] = locate("packages.connections.gym") sys.modules["gym_protocol"] = locate("packages.protocols.gym") from gym_protocol.message import GymMessage # noqa: E402 from gym_protocol.serialization import GymSerializer # noqa: E402 from .agent import ProxyAgent # noqa: E402 Action = Any Observation = Any Reward = float Done = bool Info = dict Feedback = Tuple[Observation, Reward, Done, Info] DEFAULT_GYM = 'gym'
# limitations under the License. # # ------------------------------------------------------------------------------ """This contains the proxy agent class.""" import sys from queue import Queue from typing import Optional import gym from aea.agent import Agent from aea.helpers.base import locate from aea.mail.base import Envelope, MailBox sys.modules["gym_connection"] = locate("packages.connections.gym") from gym_connection.connection import GymConnection # noqa: E402 class ProxyAgent(Agent): """This class implements a proxy agent to be used by a proxy environment.""" 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 """
import sys import time from queue import Queue from threading import Thread from typing import Any, Optional, Tuple, cast import gym from aea.common import Address from aea.helpers.base import locate from aea.mail.base import Envelope from aea.protocols.base import Message from aea.protocols.dialogue.base import Dialogue as BaseDialogue sys.modules["packages.fetchai.connections.gym"] = locate( # isort:skip "packages.fetchai.connections.gym") sys.modules["packages.fetchai.protocols.gym"] = locate( # isort:skip "packages.fetchai.protocols.gym") from packages.fetchai.connections.gym.connection import ( # noqa: E402 # pylint: disable=wrong-import-position PUBLIC_ID as GYM_CONNECTION_PUBLIC_ID, ) from packages.fetchai.protocols.gym.dialogues import ( # noqa: E402 # pylint: disable=wrong-import-position GymDialogue as BaseGymDialogue, ) from packages.fetchai.protocols.gym.dialogues import ( # noqa: E402 # pylint: disable=wrong-import-position GymDialogues as BaseGymDialogues, ) from packages.fetchai.protocols.gym.message import ( # noqa: E402 # pylint: disable=wrong-import-position GymMessage, ) from .agent import ProxyAgent # noqa: E402 # pylint: disable=wrong-import-position Action = Any