172 lines
7.1 KiB
Python
172 lines
7.1 KiB
Python
|
|
|||
|
# 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 xfun(yanlinks, node):
|
|||
|
from adregionf import adregionf
|
|||
|
import numpy as np
|
|||
|
from bus_line_match import pipei
|
|||
|
import pandas as pd
|
|||
|
import networkx as nx
|
|||
|
from objective_func import objective_func
|
|||
|
from convert_to_partition import convert_to_partition
|
|||
|
adr = adregionf(yanlinks, node)
|
|||
|
# 计算 yanlinks 第 12 列的频次
|
|||
|
a = pd.value_counts(yanlinks[:, 11], sort=False).reset_index().values
|
|||
|
|
|||
|
# 按照频次对 a 排序
|
|||
|
a = a[a[:, 1].argsort()]
|
|||
|
|
|||
|
# 初始化 fm 变量
|
|||
|
fm = 0
|
|||
|
for i in a[:, 0]:
|
|||
|
i_indices = np.where(yanlinks[:, 11] == i)
|
|||
|
a_sub_rows = yanlinks[i_indices, 4].flatten().astype(int)-1
|
|||
|
a = adr[a_sub_rows, :][:, a_sub_rows]
|
|||
|
chu = 0
|
|||
|
yisou = [chu]
|
|||
|
m = 0
|
|||
|
while len(yisou) > m:
|
|||
|
m = len(yisou)
|
|||
|
b = np.where(np.sum(a[yisou, :], axis=0) > 0)[0]
|
|||
|
yisou = np.union1d(yisou, b)
|
|||
|
if len(yisou) != len(a):
|
|||
|
fm = 1
|
|||
|
break
|
|||
|
|
|||
|
if fm == 0:
|
|||
|
|
|||
|
# Assuming the indices in yanlinks[:, 10] are integers
|
|||
|
data_path = r''
|
|||
|
df2 = pd.read_csv(data_path + 'links_test1.csv')
|
|||
|
df2['L'] = yanlinks[:, 11]
|
|||
|
df2.to_csv(data_path + 'links_test1.csv', index=False)
|
|||
|
|
|||
|
pipei()
|
|||
|
bus_line_partition = pd.read_csv('bus_line_partition.csv', header=None, encoding='gbk')
|
|||
|
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.values
|
|||
|
|
|||
|
bus_line_sequence = pd.read_csv('bus_road_sequence.csv', header=None, encoding='gbk')
|
|||
|
bus_line_sequence = bus_line_sequence.drop(bus_line_sequence.columns[0], axis=1)
|
|||
|
bus_line_sequence = bus_line_sequence .fillna(0)
|
|||
|
bus_line_sequence = bus_line_sequence.astype(int)
|
|||
|
bus_line_sequence = bus_line_sequence.values
|
|||
|
|
|||
|
road_velocity =yanlinks[:, [4, 5]]
|
|||
|
|
|||
|
partition = convert_to_partition(yanlinks)
|
|||
|
G = nx.DiGraph()
|
|||
|
for row in yanlinks:
|
|||
|
G.add_edge(row[0], row[1], weight=1) # weight=row[3]
|
|||
|
G = G.to_undirected()
|
|||
|
dadr = np.load('dadr.npy')
|
|||
|
|
|||
|
# 删除全为零的行
|
|||
|
dadr = dadr[~np.all(dadr == 0, axis=1)]
|
|||
|
# 删除全为零的列
|
|||
|
dadr = dadr[:, ~np.all(dadr == 0, axis=0)]
|
|||
|
adr = dadr
|
|||
|
# 这里的dadr矩阵没有重置过的所以是有问题的
|
|||
|
|
|||
|
# 从文件中加载 adregion
|
|||
|
adregion = np.load('adregion.npy')
|
|||
|
|
|||
|
|
|||
|
# 更新 adregion
|
|||
|
for i in range(len(yanlinks)):
|
|||
|
# 将节点 yanlinks[i,5] 对应的列乘以该行染色体编码 yanlinks[i,12]
|
|||
|
adregion[:, int(yanlinks[i, 4]) - 1] = adregion[:, int(yanlinks[i, 4]) - 1] * yanlinks[i, 11]
|
|||
|
# 初始化 adr
|
|||
|
adr = np.zeros((int(np.max(yanlinks[:, 11])), int(np.max(yanlinks[:, 11]))))
|
|||
|
|
|||
|
# 更新 adr
|
|||
|
for i in range(adregion.shape[0]):
|
|||
|
a = adregion[i, :]
|
|||
|
a = np.unique(a)
|
|||
|
a = a[a != 0]
|
|||
|
|
|||
|
if len(a) > 1:
|
|||
|
for j in range(len(a)):
|
|||
|
for u in range(len(a)):
|
|||
|
if j != u:
|
|||
|
adr[int(a[j]) - 1, int(a[u]) - 1] += 1
|
|||
|
adr[int(a[u]) - 1, int(a[j]) - 1] += 1
|
|||
|
|
|||
|
# 将 adr 中大于 1 的元素设置为 1
|
|||
|
adr[adr > 1] = 1
|
|||
|
|
|||
|
|
|||
|
# 初始化
|
|||
|
y = {}
|
|||
|
z = 0
|
|||
|
for i in range(1, np.max(bus_line_partition) + 1):
|
|||
|
wu = np.where(adr[:, i - 1] == 1)[0]
|
|||
|
bus_line_partition_id = []
|
|||
|
route_sequence = []
|
|||
|
|
|||
|
for index, row in enumerate(bus_line_partition):
|
|||
|
if i in row:
|
|||
|
bus_line_partition_id.append(index)
|
|||
|
route_sequence.append(bus_line_sequence[index, :])
|
|||
|
route_sequence = np.array(route_sequence)
|
|||
|
|
|||
|
# 初始化一个与 route_velocity 形状相同的新数组
|
|||
|
bus_line_route_velocity = np.empty_like(route_sequence)
|
|||
|
|
|||
|
# 遍历 route_velocity 中的每个值
|
|||
|
for ii, row in enumerate(route_sequence):
|
|||
|
for j, value in enumerate(row):
|
|||
|
# 找到 road_velocity 第一列中与 value 相等的行索引
|
|||
|
matching_row_index = np.where(road_velocity[:, 0] == value)[0]
|
|||
|
|
|||
|
# 如果找到匹配行,将 road_velocity 第二列中对应行的值存储到新数组中
|
|||
|
if matching_row_index.size != 0:
|
|||
|
bus_line_route_velocity[ii, j] = road_velocity[matching_row_index, 1]
|
|||
|
else:
|
|||
|
bus_line_route_velocity[ii, j] = 0
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
bus_line_partition_id = bus_line_partition[bus_line_partition_id]
|
|||
|
unique_matching_rows=[]
|
|||
|
for index, row in enumerate(bus_line_partition_id):
|
|||
|
unique_row = np.array([x for i, x in enumerate(row) if x not in row[:i]])
|
|||
|
unique_matching_rows.append(unique_row)
|
|||
|
|
|||
|
bus_region_direction = [np.array([x for x in array if x in wu]) for array in unique_matching_rows]
|
|||
|
max_length = max(len(item) for item in bus_region_direction)
|
|||
|
|
|||
|
# 使用0填充长度不足的部分,并将数组转换为二维数组
|
|||
|
bus_region_direction = np.array([np.pad(item, (0, max_length - len(item)), 'constant', constant_values=0) for item in bus_region_direction])
|
|||
|
sorted_bus_region_direction = np.sort(bus_region_direction, axis=1)
|
|||
|
unique_sorted_bus_region_direction = np.unique(sorted_bus_region_direction, axis=0)
|
|||
|
same_direction_fitness= {}
|
|||
|
for aa, row in enumerate(unique_sorted_bus_region_direction):
|
|||
|
matching_row_indices = np.where(np.all(sorted_bus_region_direction == row, axis=1))[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 =same_direction_bus_line_route_velocity.shape[0]/bus_line_route_velocity.shape[0]
|
|||
|
matching_indices = np.where(same_direction_bus_line_partition_id == i)
|
|||
|
matching_values = same_direction_bus_line_route_velocity[matching_indices]
|
|||
|
# 计算方差
|
|||
|
variance = np.var(matching_values)
|
|||
|
#道路数量
|
|||
|
route_quantity = np.count_nonzero(yanlinks[:, 11] == i)
|
|||
|
same_direction_fitness[aa] = line_proportion*variance
|
|||
|
links_number = np.count_nonzero(yanlinks[:, 11] == i) - yanlinks.shape[0] / np.max(bus_line_partition)
|
|||
|
z += abs(links_number)
|
|||
|
y[i] = sum(same_direction_fitness.values())
|
|||
|
# a = objective_func(G, partition, yanlinks)
|
|||
|
y = sum(y.values())
|
|||
|
# y = np.sqrt(a**2 + y**2 + z**2)
|
|||
|
else:
|
|||
|
y = 1000000000000000
|
|||
|
return y
|
|||
|
# aa = liziqunfun(array)
|
|||
|
# print(aa)
|