Skip to content

Commit

Permalink
Fix vis block (#528)
Browse files Browse the repository at this point in the history
* fix bug

* remove destroy in __del__

* fix del infor

* format

* fix arrow

---------

Co-authored-by: pengzhenghao <pzh@cs.ucla.edu>
  • Loading branch information
QuanyiLi and pengzhenghao committed Oct 26, 2023
1 parent 13c934b commit 6369b70
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion metadrive/base_class/base_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
6 changes: 2 additions & 4 deletions metadrive/base_class/nameable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions metadrive/component/block/base_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions metadrive/component/lane/abs_lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions metadrive/component/nuplan_block/nuplan_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions metadrive/component/road_network/base_road_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 10 additions & 4 deletions metadrive/component/road_network/edge_road_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand Down
14 changes: 14 additions & 0 deletions metadrive/component/road_network/node_road_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions metadrive/component/scenario_block/scenario_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion metadrive/engine/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion metadrive/tests/vis_block/vis_bottleneck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions metadrive/tests/vis_block/vis_curve_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion metadrive/tests/vis_block/vis_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions metadrive/tests/vis_block/vis_yy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,6 +54,6 @@
}
)
if tm or tc:
# print("Reset")
print("Reset")
env.reset()
env.close()

0 comments on commit 6369b70

Please sign in to comment.