import numpy as np
def farthest_point_sampling(points, num_samples):
farthest_points = []
selected_points = []
idx = np.random.randint(len(points))
farthest_points.append(points[idx])
selected_points.append(idx)
for _ in range(num_samples - 1):
distances = np.linalg.norm(points - farthest_points[-1], axis=1)
idx = np.argmax(distances)
while idx in selected_points:
distances[idx] = 0
idx = np.argmax(distances)
farthest_points.append(points[idx])
selected_points.append(idx)
return np.array(farthest_points)
def assign_points_to_nodes(points, nodes):
assigned_nodes = [[] for _ in range(len(nodes))]
for point in points:
distances = np.linalg.norm(nodes - point, axis=1)
closest_node_idx = np.argmin(distances)
assigned_nodes[closest_node_idx].append(point)
return assigned_nodes
points = np.random.rand(100, 2)
sampled_points = farthest_point_sampling(points, 10)
nodes = np.random.rand(5, 2)
assigned_nodes = assign_points_to_nodes(points, nodes)
print("采样得到的点:", sampled_points)
print("每个节点分配的点:", assigned_nodes)
distances = torch.cdist(points, nodes)
closest_nodes = torch.argmin(distances, dim=1)
assigned_nodes = [[] for _ in range(len(nodes))]
for i, node_idx in enumerate(closest_nodes):
assigned_nodes[node_idx.item()].append(points[i])