Junjie
2024-12-10 3de198e2ed8114f9ff17d3d0d89a108d9f0cc815
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import sys
 
 
def calculate_similarity(path1, path2):
    # 将路径转换为集合
    set1 = set(path1)
    set2 = set(path2)
 
    # 计算重叠节点
    overlap = len(set1.intersection(set2))
 
    # 计算总节点
    total_nodes = len(set1.union(set2))
 
    # 计算相似度
    similarity = overlap / total_nodes if total_nodes > 0 else 0
 
    return similarity
 
path_a_str = sys.argv[1]
path_b_str = sys.argv[2]
 
path_a = json.loads(path_a_str)
path_a = [(int(x[0]), int(x[1])) for x in path_a]
 
path_b = json.loads(path_b_str)
path_b = [(int(x[0]), int(x[1])) for x in path_b]
 
# # 示例路径
# path_a = [(1, 1), (1, 2), (1, 3), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4), (9, 4), (10, 4), (10, 5), (10, 6), (11, 6)]
# path_b = [(1, 1), (1, 2), (1, 3), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4), (9, 4), (10, 4), (10, 5), (10, 6), (11, 6)]
 
 
# 计算相似度
similarity_score = calculate_similarity(path_a, path_b)
 
calcResult = 200
 
result = {
    "firstPath": json.dumps(path_a),
    "secondPath": json.dumps(path_b),
    "similarity": similarity_score,
    "calcResult": calcResult
}
 
print(result)