nodes_speed = {
“A”: 10.3,
“B”: 10.6,
“C”: 5.6,
“D”: 6.9,
“Y”: 0.6
}
cost = 0
def speed_tester(nodes_list):
“”"
節點測速
“”"
global cost
cost += 5
return sorted(nodes_list, key=lambda node: nodes_speed[node])
def fastest_filter():
nodes_chosen = []
# 平均分成5組
nodes_groups = []
nodes_list = list(nodes_speed.keys())
nodes_groups.append(nodes_list[0:5])
nodes_groups.append(nodes_list[5:10])
nodes_groups.append(nodes_list[10:15])
nodes_groups.append(nodes_list[15:20])
nodes_groups.append(nodes_list[20:25])
# 5組各自去測速
for i in range(0, 5):
nodes_groups[i] = speed_tester(nodes_groups[i])
# 取出各組第一名並測速
fastest_of_groups = [group[0] for group in nodes_groups]
fastest_of_groups = speed_tester(fastest_of_groups)
# 根據各組第一名的先後順序對所有組進行排序
sorted_groups = [[] for i in range(0, 5)]
for group in nodes_groups:
group_first_order = fastest_of_groups.index(group[0])
sorted_groups[group_first_order] = group
# 排除一定不在前5名的節點
for i in range(4, -1, -1):
sorted_groups[i] = sorted_groups[i][0:5-i]
# 取出最快的A1節點
nodes_chosen.append(sorted_groups[0][0])
sorted_groups[0] = sorted_groups[0][1:len(sorted_groups[0])]
# 取出各組第一名並測速
nodes_groups = sorted_groups
fastest_of_groups = [group[0] for group in nodes_groups]
fastest_of_groups = speed_tester(fastest_of_groups)
# 根據第一名的測速順序重新對5個組排序
for group in nodes_groups:
group_first_order = fastest_of_groups.index(group[0])
sorted_groups[group_first_order] = group
# 排除一定不在前4名的節點
for i in range(4, -1, -1):
sorted_groups[i] = sorted_groups[i][0:4-i]
# 取出最快的節點
nodes_chosen.append(sorted_groups[0][0])
sorted_groups[0] = sorted_groups[0][1:len(sorted_groups[0])]
#