PathmindAI / nativerl

Train reinforcement learning agents using AnyLogic or Python-based simulations
Apache License 2.0
19 stars 4 forks source link

Error in test_training.test_pathmind_env_module() #444

Closed slinlee closed 3 years ago

slinlee commented 3 years ago
`AttributeError: 'PathmindEnvironment' object has no attribute 'action_space'`

More context

   @pytest.mark.integration
    def test_pathmind_env_module():
        ray.shutdown()
        output_dir = f"testoutputs/test-pathmind-env-module-{randint(0,1000)}"
        run.main(
            environment="tests.cartpole.PathmindEnvironment",
            max_episodes=1,
            is_pathmind_simulation=True,
>           output_dir=output_dir,
        )

nativerl/python/tests/test_training.py:46: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
nativerl/python/run.py:155: in main
    env_instance = env_creator(env_config=env_config)
nativerl/python/pathmind_training/environments.py:114: in __init__
    self.action_space = self.define_action_space()
nativerl/python/pathmind_training/environments.py:131: in define_action_space
    action_space = self.nativeEnv.getActionSpace(i)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <pathmind_training.environments.get_native_env_from_simulation.<locals>.PathmindEnv object at 0x155d23750>, agent_id = 0

    def getActionSpace(self, agent_id=0):
>       space = self.simulation.action_space(agent_id=agent_id)
E       AttributeError: 'PathmindEnvironment' object has no attribute 'action_space'

nativerl/python/pathmind_training/environments.py:383: AttributeError
maxpumperla commented 3 years ago

@slinlee right, someone set the is_pathmind_simulation flag to true, which is incorrect. The PathmindSimulation interface indeed has an action_space. The above cart pole example is already a PathmindEnvironment class and should be loaded as such.

slinlee commented 3 years ago

@maxpumperla Hmm. When I remove is_pathmind_simulation it gives this error:

tests/test_training.py:48: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
run.py:155: in main
    env_instance = env_creator(env_config=env_config)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pathmind_training.environments.PathmindEnvironment object at 0x7e33f0427d90>
env_config = {'alphas': array(1.), 'num_reward_terms': None, 'reward_balance_period': 1, 'use_reward_terms': False}

    def __init__(self, env_config):
        # AnyLogic needs this to find its database
        os.chdir(jar_dir)

        # Put all JAR files found here in the class path
        jars = glob.glob(jar_dir + "/**/*.jar", recursive=True)

        # Initialize nativerl
        nativerl.init(
            [
                "-Djava.class.path=" + os.pathsep.join(jars + [jar_dir]),
                f"-Xmx{max_memory_in_mb}m",
            ]
        )

        # Instantiate the native environment, or mock it with pynativerl
        if is_pathmind_simulation:
            self.nativeEnv = get_native_env_from_simulation(
                environment_name, obs_selection, reward_function_name
            )
        else:
>           self.nativeEnv = nativerl.createEnvironment(environment_name)
E           AttributeError: module 'pathmind_training.pynativerl' has no attribute 'createEnvironment'

pathmind_training/environments.py:112: AttributeError
maxpumperla commented 3 years ago

@slinlee got it, so that's apparently missing (now?) from pynativerl, but it definitely exists in "normal" nativerl. I think that's the error message we want :D

slinlee commented 3 years ago

k. I'll have to dig more to set up the tests properly for this one.

maxpumperla commented 3 years ago

nah, I'm pretty sure this worked at some point, the setup is right. At least it's what I would expect. This is essentially the Python version of what we do in Java, so in a way it's the easiest case. Just need to (re-)introduce createEnvironment to pynativerl.

maxpumperla commented 3 years ago

yeah, found it in an old commit:

https://github.com/SkymindIO/nativerl/commit/8e4f7866e3e319ff857801f4b02432d90a1bcc09#diff-4fdbc4b4d5f81fb90b1db025a9aa122aa7cfd1efc79e118f779996893cb8d435L93-L108

I think I accidentally removed it bc I thought it was unused... we can just put this back in and it should work right away.

maxpumperla commented 3 years ago
import importlib

def get_environment_class(env_name):
    """Get environment class instance from a string, interpreted as Python module
    :param env_name:
    :return:
    """
    class_name = env_name.split('.')[-1]
    module = env_name.replace(f'.{class_name}', '')
    lib = importlib.import_module(module)
    return getattr(lib, class_name)

def createEnvironment(env_name):
    clazz = get_environment_class(env_name)
    obj = clazz()
    return obj