From c258b7f8b34a7aef5135e811d9265c1ae8b22a27 Mon Sep 17 00:00:00 2001 From: Tjalling Haije Date: Mon, 25 Oct 2021 18:47:54 +0200 Subject: [PATCH 1/4] Closes #308 - fixes get_room_objects() bug --- matrx/agents/agent_utils/state.py | 59 +++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/matrx/agents/agent_utils/state.py b/matrx/agents/agent_utils/state.py index ddb8b72..fd9c339 100644 --- a/matrx/agents/agent_utils/state.py +++ b/matrx/agents/agent_utils/state.py @@ -279,28 +279,59 @@ def get_all_room_names(self): return list({obj['room_name'] for obj in rooms}) def get_room_objects(self, room_name): - # Locate method to identify room content - def is_content(obj): - if 'class_inheritance' in obj.keys(): - chain = obj['class_inheritance'] - if not (Wall.__name__ in chain or Door.__name__ in chain or AreaTile in chain) \ - and obj['room_name'] == room_name: - return obj - else: # the object is a Wall, Door or AreaTile - return None - - # Get all room objects + """ This function finds all objects in a rectengular room. + Only works for rectengular rooms. + This is done by finding all objects with the `"room_name":room_name` + property (such as walls, doors, and areatiles), getting their locations, + and finding any other objects on those locations. + All objects are returned, including walls, doors, and areatiles. + """ + # Get all room objects with the {"room_name":room_name} property room_objs = self.get_room(room_name) if room_objs is None: # No room with room_name was found return None - # Filter out all area's, walls and doors - content = map(is_content, room_objs) - content = [c for c in content if c is not None] + # get locations of all room objects + room_locations = [obj['location'] for obj in room_objs] + + top_left = room_locations[0] + bottom_right = room_locations[0] + for loc in room_locations: + if loc[0] < top_left[0] or loc[1] < top_left[1]: + top_left = loc + elif loc[0] > bottom_right[0] or loc[1] > bottom_right[1]: + bottom_right = loc + content = self.get_objects_in_area(top_left=top_left, width=5, height=6)#bottom_right=bottom_right) + return content + def get_objects_in_area(self, top_left, width=None, height=None, bottom_right=None): + """ Find all objects within a designated area """ + # convert width and height to bottom_right coordinate if passed + if bottom_right is None: + if not width or not height: + raise Exception("Either a bottom_right coordinate, or width and height are required.") + else: + bottom_right = (top_left[0] + width, top_left[1] + height) + + # Check if object within area + def within_area(obj): + if "location" not in obj.keys(): + return None + # object is within the room + elif obj['location'][0] >= top_left[0] and obj['location'][1] >= top_left[1] and obj['location'][0] <= bottom_right[0] and obj['location'][1] <= bottom_right[1]: + return obj + else: + return None + + # Filter out all objects not within the room + objs_in_area = map(within_area, self.__state_dict.values()) + objs_in_area = [c for c in objs_in_area if c is not None] + + return objs_in_area + def get_room_doors(self, room_name): # Locate method to identify doors of the right room def is_content(obj): From 8749d1b26a29c29753412ec0d126706f68d9d81f Mon Sep 17 00:00:00 2001 From: Tjalling Haije Date: Mon, 25 Oct 2021 18:49:58 +0200 Subject: [PATCH 2/4] Closed #306 - fixed team name being ignored by agent --- matrx/objects/agent_body.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/matrx/objects/agent_body.py b/matrx/objects/agent_body.py index d523ba5..205aa0d 100644 --- a/matrx/objects/agent_body.py +++ b/matrx/objects/agent_body.py @@ -162,7 +162,10 @@ def __init__(self, location, possible_actions, sense_capability, class_callable, "action_result": None} # We set a placeholder for the 'team' property so that it can be found in self.properties - self.team = "" + if team is not None: + self.team = "" + else: + self.team = team # Call the super constructor (we do this here because then we have access to all of EnvObject, including a # unique id From 1085b16e386637fd98e0c9ae2c29580a6b94c360 Mon Sep 17 00:00:00 2001 From: Tjalling Haije Date: Mon, 25 Oct 2021 19:07:18 +0200 Subject: [PATCH 3/4] Passing custom properties to door for worldbuilder.add_room() --- matrx/cases/simple_case.py | 2 +- matrx/world_builder.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/matrx/cases/simple_case.py b/matrx/cases/simple_case.py index 14ffd04..feba90d 100644 --- a/matrx/cases/simple_case.py +++ b/matrx/cases/simple_case.py @@ -12,7 +12,7 @@ def create_builder(): factory.add_logger(logger_class=LogActions, save_path="log_data/") - factory.add_room(top_left_location=[0, 0], width=15, height=15, name="world_bounds") + factory.add_room(top_left_location=[0, 0], width=15, height=15, name="world_bounds", door_locations=[[11,0]], door_custom_properties={"img_name": "fire.gif"}) n_agent = 1 diff --git a/matrx/world_builder.py b/matrx/world_builder.py index 5e16391..ee92c0c 100644 --- a/matrx/world_builder.py +++ b/matrx/world_builder.py @@ -1903,6 +1903,8 @@ def add_room(self, top_left_location, width, height, name, door_open_colour=None, door_closed_colour=None, door_visualization_opacity=None, + door_custom_properties=None, + door_customizable_properties=None, wall_visualize_colour=None, wall_visualize_opacity=None, wall_custom_properties=None, wall_customizable_properties=None, @@ -1952,6 +1954,14 @@ def add_room(self, top_left_location, width, height, name, door_visualization_opacity: str (optional, 1.0) Opacity of the object, as a percentge from 0.0 (fully opaque) to 1.0 (no opacity). Defaults to 1.0. + door_custom_properties : dict (optional, default None) + A dictionary of custom properties and their values passed to every + door object. + + door_customizable_properties : list (optional, default None) + A list of property names that other objects and agents are allowed + to adjust. + wall_visualize_colour : string (optional, default None) The colour of the walls. @@ -2057,6 +2067,7 @@ def add_room(self, top_left_location, width, height, name, # those wall locations door_locations = [] if door_locations is None else door_locations for door_loc in door_locations: + door_loc = tuple(door_loc) if door_loc in all_: all_.remove(door_loc) else: @@ -2078,10 +2089,15 @@ def add_room(self, top_left_location, width, height, name, # Add all doors for door_loc in door_locations: + if door_custom_properties is None: + door_custom_properties = {"room_name": name} + else: + door_custom_properties = {**door_custom_properties, "room_name": name} self.add_object(location=door_loc, name=f"{name} - door@{door_loc}", callable_class=Door, open_colour=door_open_colour, closed_colour=door_closed_colour, visualize_opacity=door_visualization_opacity, - is_open=doors_open, **{"room_name": name}) + is_open=doors_open, custom_properties=door_custom_properties, + customizable_properties=door_customizable_properties) # Add all area tiles if required if with_area_tiles: From 5c161bdb26155b5028d9de6451e3efd3139a536b Mon Sep 17 00:00:00 2001 From: Tjalling Haije Date: Mon, 25 Oct 2021 19:12:22 +0200 Subject: [PATCH 4/4] updated version --- matrx/__version__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrx/__version__.py b/matrx/__version__.py index ca457ec..65e97c3 100644 --- a/matrx/__version__.py +++ b/matrx/__version__.py @@ -12,7 +12,7 @@ __url__ = 'https://matrx-software.com' __doc_url__ = 'http://docs.matrx-software.com/en/latest/' __source_url__ = 'https://github.com/matrx-software/matrx' -__version__ = '2.1.0' +__version__ = '2.1.1' __author__ = 'MATRX Team at TNO.nl' __author_email__ = 'info@matrx.com' __license__ = 'MIT License'