| | |
| | | |
| | | public Double similarityPath(List<NavigateNode> firstPath, List<NavigateNode> secondPath) { |
| | | try { |
| | | ProcessBuilder processBuilder = new ProcessBuilder("python", pythonCalcSimilarity, JSON.toJSONString(firstPath), JSON.toJSONString(secondPath)); |
| | | List<int[]> first = new ArrayList<>(); |
| | | for (NavigateNode node : firstPath) { |
| | | first.add(new int[]{node.getX(), node.getY()}); |
| | | } |
| | | |
| | | List<int[]> second = new ArrayList<>(); |
| | | for (NavigateNode node : secondPath) { |
| | | second.add(new int[]{node.getX(), node.getY()}); |
| | | } |
| | | |
| | | ProcessBuilder processBuilder = new ProcessBuilder("python", pythonCalcSimilarity, JSON.toJSONString(first), JSON.toJSONString(second)); |
| | | processBuilder.redirectErrorStream(true); |
| | | |
| | | Process process = processBuilder.start(); |
New file |
| | |
| | | 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) |