110 lines
4.2 KiB
Python
110 lines
4.2 KiB
Python
import networkx as nx
|
||
import numpy as np
|
||
|
||
def compute_cut_vol(G, partition,yanlinks,result_array):
|
||
|
||
"""
|
||
计算切割权重与内部权重的和
|
||
G: networkx 图
|
||
partition: 分区的结果,是一个列表,列表中的每个元素是一个集合,表示一个分区
|
||
返回值: 一个元组,第一个元素是切割权重之和,第二个元素是内部权重之和
|
||
"""
|
||
import numpy as np
|
||
|
||
# 假设yanlinks是你的NumPy数组
|
||
|
||
# 找到第12列的唯一值和对应的索引
|
||
unique_values_12, unique_indices_12 = np.unique(yanlinks[:, 11], return_index=True)
|
||
|
||
# 初始化一个空数组来存储结果
|
||
result_array_12 = []
|
||
|
||
# 遍历每个唯一值
|
||
for value_12 in unique_values_12:
|
||
# 找到在result_array第一列中与当前唯一值匹配的行的索引
|
||
matching_indices = np.where(result_array[:, 0] == value_12)[0]
|
||
|
||
# 如果没有匹配的行,则继续下一个唯一值的循环
|
||
if len(matching_indices) == 0:
|
||
continue
|
||
|
||
# 获取匹配行的第二列和第三列的值
|
||
column_two_values = result_array[matching_indices, 1]
|
||
column_three_values = result_array[matching_indices, 2]
|
||
|
||
# 计算第二列和第三列的平均值
|
||
column_two_mean = np.mean(column_two_values)
|
||
column_three_mean = np.mean(column_three_values)
|
||
|
||
# 将结果存储到新数组中
|
||
result_array_12.append([value_12, column_two_mean, column_three_mean])
|
||
|
||
# 将结果转换为NumPy数组
|
||
result_array_12 = np.array(result_array_12)
|
||
|
||
|
||
cut_value = 0.0
|
||
bili = 0.0
|
||
adregion = np.load('adregion.npy')
|
||
num_elements = yanlinks[:, 11].shape[0]
|
||
for i in range(len(yanlinks[:, 1])):
|
||
adregion[:, int(yanlinks[i, 4]) - 1] = adregion[:, int(yanlinks[i, 4]) - 1] * int(yanlinks[i, 11])
|
||
region_adj = np.zeros((num_elements, num_elements))
|
||
for i in range(len(adregion[:, 1])):
|
||
|
||
a = adregion[i, :]
|
||
a = np.unique(a)
|
||
a = a[a != 0]
|
||
|
||
if a.size > 0:
|
||
x = 1
|
||
y = a.shape[0]
|
||
else:
|
||
x, y = 0, 1
|
||
if y > 1:
|
||
for j in range(len(a)):
|
||
for u in range(len(a)):
|
||
if j != u:
|
||
# subregion_adj表示子区域的邻接关系,其中数值的大小表示区域之间的相关程度
|
||
region_adj[int(a[j]) - 1, int(a[u]) - 1] = 1
|
||
region_adj[int(a[u]) - 1, int(a[j]) - 1] = 1
|
||
unique_values, unique_indices = np.unique(yanlinks[:, 11], return_index=True)
|
||
cut_values = []
|
||
num_elements_list = []
|
||
for i in unique_values:
|
||
wu = np.where(region_adj[int(i) - 1, :] == 1)
|
||
wu = np.array(wu[0])
|
||
num_elements = len(wu)
|
||
num_elements_list.append(num_elements)
|
||
distances_all_region=0
|
||
for comm in partition:
|
||
distances_one_region=0
|
||
for c in comm:
|
||
rows = yanlinks[yanlinks[:, 2] == c]
|
||
# 提取result_array_12的第1列和rows的第12列
|
||
result_array_12_column1 = result_array_12[:, 0]
|
||
rows_column12 = rows[:, 11]
|
||
|
||
# 找到result_array_12第1列等于rows第12列的值的行的索引
|
||
matching_indices = int(np.where(result_array_12_column1 == rows_column12)[0])
|
||
|
||
# 提取这些行的二三列的值构成的点
|
||
points_result_array_12 = result_array_12[matching_indices, 1:3]
|
||
|
||
points_rows = rows[0, 6:8]
|
||
|
||
# 计算这两个点之间的距离
|
||
distance = np.linalg.norm(points_result_array_12 - points_rows)*111000
|
||
# 对符合条件的行进行操作,这里可以根据需求进行处理
|
||
distances_one_region+=distance
|
||
distances_one_region = distances_one_region/len(comm)
|
||
distances_all_region+=distances_one_region
|
||
# cut_value = nx.cut_size(G, comm) # 计算切割权重
|
||
# cut_values.append(cut_value)
|
||
|
||
# if len(cut_values) == len(num_elements_list):
|
||
# bili = sum([cut / num for cut, num in zip(cut_values, num_elements_list)])
|
||
# else:
|
||
# print("The lists of cut_values and num_elements are not the same length.")
|
||
|
||
return cut_value, distances_all_region |