53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
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):
|
|
|
|
# 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]
|
|
unique_values, unique_indices = np.unique(yanlinks[:, 11], return_index=True)
|
|
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])
|
|
z = 0
|
|
max_bus_line_partition = np.max(bus_line_partition)
|
|
for i in range(1, max_bus_line_partition + 1):
|
|
links_number = np.count_nonzero(yanlinks[:,11]==i)-yanlinks.shape[0]/max_bus_line_partition
|
|
z +=abs(links_number)
|
|
Tvb = 0
|
|
for i in unique_values:
|
|
selected_values = yanlinks[yanlinks[:, 10] == i][:, 5]
|
|
variance = np.var(selected_values)
|
|
Tvb += variance
|
|
a = objective_func(G, partition,yanlinks)
|
|
# y = np.sqrt(y**2 + a**2 + z**2)
|
|
# y = np.concatenate((y,a,z),axis=1)
|
|
|
|
# print(f'适应度值为:{y}')
|
|
return Tvb,a,z
|
|
# aa = liziqunfun(x)
|
|
# print(aa) |