Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function for fetching gridworld env object(s) by type or name #14

Closed
matrxs opened this issue Jan 21, 2020 · 4 comments
Closed

Function for fetching gridworld env object(s) by type or name #14

matrxs opened this issue Jan 21, 2020 · 4 comments

Comments

@matrxs
Copy link
Owner

matrxs commented Jan 21, 2020

ID's of all env objects are now created as: obj_name + random_integer.
However, this makes it impossible to fetch an object when you don't know the ID of an object. It would be desirable to fetch an object by type (class) or name.

@olafvisker
Copy link

In grid_world.py change function get_env_object with the following:

    def get_env_object(self, requested_id=None, obj_type=None):
        obj = None

        if requested_id in self.__registered_agents.keys():
            if obj_type is not None:
                if isinstance(self.__registered_agents[requested_id], obj_type):
                    obj = self.__registered_agents[requested_id]
            else:
                obj = self.__registered_agents[requested_id]

        if requested_id in self.__environment_objects.keys():
            if obj_type is not None:
                if isinstance(self.__environment_objects[requested_id], obj_type):
                    obj = self.__environment_objects[requested_id]
            else:
                obj = self.__environment_objects[requested_id]

        if requested_id is None and obj_type is not None:
            for obj_id, env_obj in self.__environment_objects.items():
                if isinstance(env_obj, obj_type): 
                    obj = env_obj
                    break

        return obj

@jwaa
Copy link
Collaborator

jwaa commented Jan 22, 2020

ID's of all env objects are now created as: obj_name + random_integer.
However, this makes it impossible to fetch an object when you don't know the ID of an object. It would be desirable to fetch an object by type (class) or name.

When did it occur that you wished to fetch an object while you don't know the ID?

@thaije; please do not use the MATRXS account to post issues, as it makes it more difficult to track the origin and progress of issues.

@jwaa
Copy link
Collaborator

jwaa commented Jan 22, 2020

In grid_world.py change function get_env_object with the following:

    def get_env_object(self, requested_id=None, obj_type=None):
        obj = None

        if requested_id in self.__registered_agents.keys():
            if obj_type is not None:
                if isinstance(self.__registered_agents[requested_id], obj_type):
                    obj = self.__registered_agents[requested_id]
            else:
                obj = self.__registered_agents[requested_id]

        if requested_id in self.__environment_objects.keys():
            if obj_type is not None:
                if isinstance(self.__environment_objects[requested_id], obj_type):
                    obj = self.__environment_objects[requested_id]
            else:
                obj = self.__environment_objects[requested_id]

        if requested_id is None and obj_type is not None:
            for obj_id, env_obj in self.__environment_objects.items():
                if isinstance(env_obj, obj_type): 
                    obj = env_obj
                    break

        return obj

@olafvisker; the edit you propose returns the very first object if finds with a certain type. Why only the first and not a list of all those object types?

@thaije
Copy link
Collaborator

thaije commented Jan 24, 2020

@jwaa Ah yeah sorry about that, I was signed in by default using the MATRXS account by accident.

For how this function should preferrably work, also see issue #15. Script above was a quick fix for AIMS, in #15 a more permanent solution is proposed which returns a list as you suggest.

As for the use case: the AIMS project makes use of a Score object, simply named 'score' by the user when creating the scenario. At various locations in the code, e.g. in an action, this Score object needs to be fetched or changed. However, how to fetch this object? The current get_env_object requires the obj_id to be passed, which is not known. Only that it is an object of class Score with name 'score'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants