import numpy as np import pandas as pd import networkx as nx from objective_func import objective_func from convert_to_partition import convert_to_partition import timeit # x = np.array([8, 3, 6, 1, 3, 7, 2, 2, 2, 4, 6, 5, 8, 5, 5, 3, 1, 3, 2, 2, 7, 7,4, 8, 1, 7, 1, 1, 3, 7, 4, 7, 2, 6, 2, 8, 1, 3, 1, 6]) def liziqunfun(x,yanlinks,road_velocity,dadr,bus_line_sequence,result_array): # Assuming x is a list of integers. if isinstance(x, list): x = x[0] # Use numpy advanced (integer) indexing to avoid for loop yanlinks[:, 11] = x[yanlinks[:, 10].astype(int) - 1] data_path = r'' df2 = pd.read_csv(data_path + 'links_test1.csv') df2['L'] = yanlinks[:, 11] df1 = pd.read_csv(data_path + 'bus_road_sequence_processed.csv', header=None, encoding='gbk') replace_dict = df2.set_index(df2.columns[5])[df2.columns[11]].to_dict() df1.iloc[:, 1:] = df1.iloc[:, 1:].applymap(replace_dict.get) bus_line_partition = df1.copy() bus_line_partition = bus_line_partition.drop(bus_line_partition.columns[0], axis=1) bus_line_partition = bus_line_partition.fillna(0) bus_line_partition = bus_line_partition.astype(int) bus_line_partition = bus_line_partition.to_numpy() partition = convert_to_partition(yanlinks) G = nx.Graph() G.add_edges_from([(row[0], row[1], {'weight': 1}) for row in yanlinks]) # 删除全为零的行和列 rows_non_zero = np.any(dadr != 0, axis=1) cols_non_zero = np.any(dadr != 0, axis=0) adr = dadr[rows_non_zero][:, cols_non_zero] # 初始化 badr badr = np.zeros((len(np.unique(x)), len(np.unique(x)))) # 为了避免在循环中多次计算,提前计算好非零元素的索引 non_zero_indices = np.argwhere(adr) for i, j in non_zero_indices: if x[i] != x[j]: badr[x[i] - 1, x[j] - 1] = 1 badr[x[j] - 1, x[i] - 1] = 1 y = {} z = 0 max_bus_line_partition = np.max(bus_line_partition) # 遍历 route_velocity 中的每个值 road_velocity_dict = {row[0]: row[1] for row in road_velocity} def get_velocity(value): return road_velocity_dict.get(value, 0) vectorized_get_velocity = np.vectorize(get_velocity) for i in range(1, max_bus_line_partition + 1): wu = np.where(badr[:, i - 1] == 1)[0] bus_line_partition_id = [index for index, row in zip(range(len(bus_line_partition)), bus_line_partition) if i in row] route_sequence = bus_line_sequence[bus_line_partition_id, :] bus_line_route_velocity = vectorized_get_velocity(route_sequence) # 1. 从 bus_line_partition 提取 rows bus_line_partition_id = bus_line_partition[bus_line_partition_id] # 2. 对每一行使用 numpy 的 unique 函数,进行去重操作 unique_matching_rows = [np.unique(row) for row in bus_line_partition_id] # 3. 筛选与 wu 交集的元素 bus_region_direction = [np.intersect1d(row, wu) for row in unique_matching_rows] # 4. 计算最大长度以进行填充操作 max_length = max(len(item) for item in bus_region_direction) # 5. 使用0填充长度不足的部分,并将数组转换为二维数组 bus_region_direction = np.array( [np.pad(item, (0, max_length - len(item)), 'constant', constant_values=0) for item in bus_region_direction]) # 6. 对 bus_region_direction 进行排序 sorted_bus_region_direction = np.sort(bus_region_direction, axis=1) # 7. 获取唯一的 bus_region_direction unique_sorted_bus_region_direction = np.unique(sorted_bus_region_direction, axis=0) # 8. 初始化 same_direction_fitness 字典 same_direction_fitness = {} # 计算整个数组的行匹配 matching_rows = np.all(sorted_bus_region_direction[:, None] == unique_sorted_bus_region_direction, axis=2) # 计算总线路数 total_bus_lines = bus_line_route_velocity.shape[0] total_bus_lines = len(matching_rows) # 这个应该在循环外部计算一次 for aa, row_match in enumerate(matching_rows.T): matching_row_indices = np.where(row_match)[0] same_direction_bus_line_route_velocity = bus_line_route_velocity[matching_row_indices] same_direction_bus_line_partition_id = bus_line_partition_id[matching_row_indices] line_proportion = len(matching_row_indices) / total_bus_lines # 使用NumPy的向量化操作 matching_values = same_direction_bus_line_route_velocity[ np.where(same_direction_bus_line_partition_id == i)] matching_values = matching_values[matching_values != 0] # 使用numpy来决定是否计算方差 variance = np.var(matching_values) if len(matching_values) > 1 else 0 # 使用方差和线路比例更新 same_direction_fitness same_direction_fitness[aa] = line_proportion * variance links_number = np.count_nonzero(yanlinks[:,11]==i)-yanlinks.shape[0]/max_bus_line_partition z +=abs(links_number) y[i] = sum(same_direction_fitness.values()) # unique_values, unique_indices = np.unique(yanlinks[:, 11], return_index=True) # Tvb = 0 # for i in unique_values: # selected_values = yanlinks[yanlinks[:, 11] == i][:, 5] # variance = np.var(selected_values) # Tvb += variance a = objective_func(G, partition,yanlinks,result_array) y = sum(y.values()) # y = np.sqrt(y**2 + a**2 + z**2) # y = np.concatenate((y,a,z),axis=1) # print(f'适应度值为:{y}') return y,a,z # aa = liziqunfun(x) # print(aa)