From 6369b7057543ab0e71c3cd51f3d0a41200fa4e16 Mon Sep 17 00:00:00 2001 From: Quanyi Li Date: Thu, 26 Oct 2023 13:27:03 +0100 Subject: [PATCH] Fix vis block (#528) * fix bug * remove destroy in __del__ * fix del infor * format * fix arrow --------- Co-authored-by: pengzhenghao --- metadrive/base_class/base_object.py | 2 +- metadrive/base_class/nameable.py | 6 ++---- metadrive/component/block/base_block.py | 6 +++--- metadrive/component/lane/abs_lane.py | 3 --- metadrive/component/nuplan_block/nuplan_block.py | 5 ----- .../component/road_network/base_road_network.py | 5 +++++ .../component/road_network/edge_road_network.py | 14 ++++++++++---- .../component/road_network/node_road_network.py | 14 ++++++++++++++ .../component/scenario_block/scenario_block.py | 4 ---- .../vehicle_navigation_module/base_navigation.py | 1 - metadrive/engine/interface.py | 2 +- metadrive/tests/vis_block/vis_bottleneck.py | 2 +- metadrive/tests/vis_block/vis_curve_block.py | 4 ++-- metadrive/tests/vis_block/vis_intersection.py | 3 ++- metadrive/tests/vis_block/vis_yy.py | 4 ++-- 15 files changed, 43 insertions(+), 32 deletions(-) diff --git a/metadrive/base_class/base_object.py b/metadrive/base_class/base_object.py index d13f85fdf..1711b55ab 100644 --- a/metadrive/base_class/base_object.py +++ b/metadrive/base_class/base_object.py @@ -245,13 +245,13 @@ def destroy(self): """ Fully delete this element and release the memory """ + super(BaseObject, self).destroy() try: from metadrive.engine.engine_utils import get_engine except ImportError: pass else: engine = get_engine() - if engine is not None: self.detach_from_world(engine.physics_world) if self._body is not None and hasattr(self.body, "object"): diff --git a/metadrive/base_class/nameable.py b/metadrive/base_class/nameable.py index 1cd036636..bdf36d3b1 100644 --- a/metadrive/base_class/nameable.py +++ b/metadrive/base_class/nameable.py @@ -18,11 +18,9 @@ def class_name(self): def __del__(self): try: - str(self) - except AttributeError: - pass - else: logging.debug("{} is destroyed".format(str(self))) + except: + pass def __repr__(self): return "{}".format(str(self)) diff --git a/metadrive/component/block/base_block.py b/metadrive/component/block/base_block.py index 0dd1d0a3d..628b700c1 100644 --- a/metadrive/component/block/base_block.py +++ b/metadrive/component/block/base_block.py @@ -338,9 +338,9 @@ def destroy(self): self._global_network = None super(BaseBlock, self).destroy() - def __del__(self): - self.destroy() - logger.debug("{} is being deleted.".format(type(self))) + # def __del__(self): + # self.destroy() + # logger.debug("{} is being deleted.".format(type(self))) @property def bounding_box(self): diff --git a/metadrive/component/lane/abs_lane.py b/metadrive/component/lane/abs_lane.py index 67b023ea7..190c7f1d5 100644 --- a/metadrive/component/lane/abs_lane.py +++ b/metadrive/component/lane/abs_lane.py @@ -412,9 +412,6 @@ def destroy(self): else: clear_node_list(self._node_path_list) - def __del__(self): - self.destroy() - def get_polyline(self, interval=2, lateral=0): """ This method will return the center line of this Lane in a discrete vector representation diff --git a/metadrive/component/nuplan_block/nuplan_block.py b/metadrive/component/nuplan_block/nuplan_block.py index 6b6db92d6..08b4fae27 100644 --- a/metadrive/component/nuplan_block/nuplan_block.py +++ b/metadrive/component/nuplan_block/nuplan_block.py @@ -187,11 +187,6 @@ def destroy(self): self.nuplan_map_data = None super(NuPlanBlock, self).destroy() - def __del__(self): - self.destroy() - super(NuPlanBlock, self).__del__() - # print("NuPlan Block is being deleted.") - @staticmethod def _get_points_from_boundary(boundary, center): path = boundary.discrete_path diff --git a/metadrive/component/road_network/base_road_network.py b/metadrive/component/road_network/base_road_network.py index 2408b0a3d..8fb7c799f 100644 --- a/metadrive/component/road_network/base_road_network.py +++ b/metadrive/component/road_network/base_road_network.py @@ -86,6 +86,11 @@ def remove_bounding_box(self): np.removeNode() def destroy(self): + """ + Destroy all lanes in this network + Returns: None + + """ self.bounding_box = None def has_connection(self, lane_index_1, lane_index_2): diff --git a/metadrive/component/road_network/edge_road_network.py b/metadrive/component/road_network/edge_road_network.py index dd8e46a4d..172c27380 100644 --- a/metadrive/component/road_network/edge_road_network.py +++ b/metadrive/component/road_network/edge_road_network.py @@ -96,11 +96,17 @@ def get_peer_lanes_from_index(self, lane_index): return ret def destroy(self): + """ + Destroy all lanes in this road network + Returns: None + + """ super(EdgeRoadNetwork, self).destroy() - for k, v in self.graph.items(): - v.lane.destroy() - self.graph[k]: lane_info = None - self.graph = None + if self.graph is not None: + for k, v in self.graph.items(): + v.lane.destroy() + self.graph[k]: lane_info = None + self.graph = None def __del__(self): logging.debug("{} is released".format(self.__class__.__name__)) diff --git a/metadrive/component/road_network/node_road_network.py b/metadrive/component/road_network/node_road_network.py index f9697023e..077b54065 100644 --- a/metadrive/component/road_network/node_road_network.py +++ b/metadrive/component/road_network/node_road_network.py @@ -319,3 +319,17 @@ def get_all_lanes(self): for _to, lanes in _to_dict.items(): ret += lanes return ret + + def destroy(self): + """ + Destroy all lanes in this network + Returns: None + + """ + super(NodeRoadNetwork, self).destroy() + if self.graph is not None: + for from_, _to_dict in self.graph.items(): + for _to, lanes in _to_dict.items(): + for lane in lanes: + lane.destroy() + self.graph = None diff --git a/metadrive/component/scenario_block/scenario_block.py b/metadrive/component/scenario_block/scenario_block.py index 307742285..483d9ca4c 100644 --- a/metadrive/component/scenario_block/scenario_block.py +++ b/metadrive/component/scenario_block/scenario_block.py @@ -169,7 +169,3 @@ def destroy(self): self.map_index = None # self.map_data = None super(ScenarioBlock, self).destroy() - - def __del__(self): - self.destroy() - super(ScenarioBlock, self).__del__() diff --git a/metadrive/component/vehicle_navigation_module/base_navigation.py b/metadrive/component/vehicle_navigation_module/base_navigation.py index 77228e32e..42b9849c4 100644 --- a/metadrive/component/vehicle_navigation_module/base_navigation.py +++ b/metadrive/component/vehicle_navigation_module/base_navigation.py @@ -187,7 +187,6 @@ def set_force_calculate_lane_index(self, force: bool): self.FORCE_CALCULATE = force def __del__(self): - self.destroy() logging.debug("{} is destroyed".format(self.__class__.__name__)) def get_current_lateral_range(self, current_position, engine) -> float: diff --git a/metadrive/engine/interface.py b/metadrive/engine/interface.py index 26e7fed49..af3b93b35 100644 --- a/metadrive/engine/interface.py +++ b/metadrive/engine/interface.py @@ -196,7 +196,7 @@ def _update_navi_arrow(self, lanes_heading): dir_0 = np.array([math.cos(lane_0_heading), math.sin(lane_0_heading), 0]) dir_1 = np.array([math.cos(lane_1_heading), math.sin(lane_1_heading), 0]) cross_product = np.cross(dir_1, dir_0) - left = False if cross_product[-1] < 0 else True + left = True if cross_product[-1] < 0 else False if not self._is_showing_arrow: self._is_showing_arrow = True if left: diff --git a/metadrive/tests/vis_block/vis_bottleneck.py b/metadrive/tests/vis_block/vis_bottleneck.py index 2dba49c9b..ea31e70aa 100644 --- a/metadrive/tests/vis_block/vis_bottleneck.py +++ b/metadrive/tests/vis_block/vis_bottleneck.py @@ -11,7 +11,7 @@ initialize_asset_loader(test) global_network = NodeRoadNetwork() - b = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1) + b = FirstPGBlock(global_network, 3.0, 2, test.render, test.world, 1) for i in range(1, 13): tp = Merge if i % 3 == 0 else Split b = tp(i, b.get_socket(0), global_network, i) diff --git a/metadrive/tests/vis_block/vis_curve_block.py b/metadrive/tests/vis_block/vis_curve_block.py index 9bf02c799..5161df12e 100644 --- a/metadrive/tests/vis_block/vis_curve_block.py +++ b/metadrive/tests/vis_block/vis_curve_block.py @@ -14,10 +14,10 @@ curve = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1) for i in range(1, 13): curve = Curve(i, curve.get_socket(0), global_network, i) - # print(i) + print(i) while True: success = curve.construct_block(test.render, test.world) - # print(success) + print(success) if success: break curve.destruct_block(test.world) diff --git a/metadrive/tests/vis_block/vis_intersection.py b/metadrive/tests/vis_block/vis_intersection.py index 6977df743..3203e601a 100644 --- a/metadrive/tests/vis_block/vis_intersection.py +++ b/metadrive/tests/vis_block/vis_intersection.py @@ -12,7 +12,8 @@ global_network = NodeRoadNetwork() first = FirstPGBlock(global_network, 3.0, 2, test.render, test.world, 20) intersection = InterSection(3, first.get_socket(0), global_network, 1) - intersection.construct_block(test.render, test.world) + # print(intersection.construct_block(test.render, test.world)) + id = 4 for socket_idx in range(intersection.SOCKET_NUM): block = Curve(id, intersection.get_socket(socket_idx), global_network, id) diff --git a/metadrive/tests/vis_block/vis_yy.py b/metadrive/tests/vis_block/vis_yy.py index 2c8166207..7d107a0d1 100644 --- a/metadrive/tests/vis_block/vis_yy.py +++ b/metadrive/tests/vis_block/vis_yy.py @@ -42,7 +42,7 @@ ) o, _ = env.reset() - # print("vehicle num", len(env.engine.traffic_manager.vehicles)) + print("vehicle num", len(env.engine.traffic_manager.vehicles)) for i in range(1, 100000): o, r, tm, tc, info = env.step([0, 1]) info["fuel"] = env.vehicle.energy_consumption @@ -54,6 +54,6 @@ } ) if tm or tc: - # print("Reset") + print("Reset") env.reset() env.close()