"""优化路由文件:只保留在控制区域内行驶的车辆""" import xml.etree.ElementTree as ET # 控制区域的边(来自config) control_edges = { "G1523_AM3_4.1", "G1523_AM3_4.2", "G1523_AM3_4.3", "G1523_AM3_4.4", "G1523_AM3_4.5", "G1523_AM3_4.6", "G1523_AM4", "G1523_AM5", "G1523_AM6", "G1523_AM6.1", "G1523_AM6.2", "G1523_AM6.3", "G1523_AM6.4", "G1523_AM6.5", "G1523_AM6.6", "G1523_AM7", "G1523_AM7.1", "G1523_AM7.2", "G1523_AM8", "G1523_AM8.1", "G1523_AM8.2" } print(f"控制区域边数: {len(control_edges)}") # 解析路由文件 route_file = "sumo_resource/routes_filtered.xml" tree = ET.parse(route_file) root = tree.getroot() # 分析每条路由 routes_info = {} for route in root.findall('route'): route_id = route.get('id') edges = route.get('edges').split() # 计算在控制区域内的边数量 control_edges_in_route = [e for e in edges if e in control_edges] non_control_edges = [e for e in edges if e not in control_edges] routes_info[route_id] = { 'total': len(edges), 'control': len(control_edges_in_route), 'non_control': len(non_control_edges), 'edges': edges } print(f"\n路由分析:") for rid, info in routes_info.items(): print(f" {rid}: 总边数={info['total']}, 控制区={info['control']}, 非控制区={info['non_control']}") # 只保留主要在控制区域内的路由(控制区边数 >= 总边数的50%) valid_routes = {rid for rid, info in routes_info.items() if info['control'] >= info['total'] * 0.5} print(f"\n保留的路由: {valid_routes}") print(f"删除的路由: {set(routes_info.keys()) - valid_routes}")