#
luxiaotao1123
2024-09-24 82dadf2e9fbd30756ccd419a17cda9d41c6a032b
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.zy.acs.manager.core.service.floyd;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.entity.CodeGap;
import com.zy.acs.manager.manager.entity.Route;
import com.zy.acs.manager.manager.service.CodeGapService;
import com.zy.acs.manager.manager.service.CodeService;
import com.zy.acs.manager.manager.service.RouteService;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * Created by vincent on 2023/6/15
 */
@Slf4j
@Component
public class FloydNavigateService {
 
    public static final double INF = 999999.0;
 
    @Autowired
    private CodeService codeService;
    @Autowired
    private CodeGapService codeGapService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private ConfigService configService;
 
    private List<Long> matrixHeader;
    private Double[][] adjacencyMatrix;
    private int[][] pathMatrix;
    private Double[][] floydMatrix;
 
    public List<Integer> calculatePath(Integer origin, Integer destination) {
        List<Integer> path = new ArrayList<>();
 
        path.add(destination);
 
        Integer current = destination;
        while (!current.equals(origin)) {
            current = this.pathMatrix[origin][current];
            path.add(current);
        }
 
        Collections.reverse(path);
        return path;
    }
 
    public int codeIdx(Long codeId) {
        return this.matrixHeader.indexOf(codeId);
    }
 
    public Long getCodeId(int idx) {
        return this.matrixHeader.get(idx);
    }
 
    @PostConstruct
    @SuppressWarnings("all")
    public void generateMatrix() {
        log.info("【FLOYD】正在计算矩阵数据......");
        List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1).eq(Code::getDeleted, false));
 
        int size = codeList.size();
 
        matrixHeader = new ArrayList<>(size);
        adjacencyMatrix = new Double[size][size];
        pathMatrix = new int[size][size];
 
        for (int i = 0; i < size; i++) {
            Code startCode = codeList.get(i);
            matrixHeader.add(startCode.getId());
            for (int j = 0; j < size; j++) {
                Code endCode = codeList.get(j);
                Route route = routeService.findByCodeOfBoth(startCode.getId(), endCode.getId());
                CodeGap codeGap = codeGapService.findByCodeOfBoth(startCode.getId(), endCode.getId());
 
                double distance = INF;
                int path = -1;
 
                if (null != route && null != codeGap && codeGap.getDistance() > 0) {
                    if (i != j) {
                        distance = codeGap.getDistance();
                    }
                    path = i;
                }
 
 
                adjacencyMatrix[i][j] = distance;
 
                pathMatrix[i][j] = path;
            }
        }
 
        floydMatrix = this.floydAlgorithm(adjacencyMatrix);
 
    }
 
    /**
     * Floyd
     */
    @SuppressWarnings("all")
    private Double[][] floydAlgorithm(Double[][] graph) {
        int n = graph.length;
        Double[][] shortestPaths = new Double[n][n];
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                shortestPaths[i][j] = graph[i][j];
            }
        }
 
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (shortestPaths[i][k] + shortestPaths[k][j] < shortestPaths[i][j]) {
                        shortestPaths[i][j] = shortestPaths[i][k] + shortestPaths[k][j];
                        pathMatrix[i][j] = pathMatrix[k][j];
                    }
                }
            }
        }
 
        return shortestPaths;
    }
 
    public Double[][] getAdjacencyMatrix() {
        if (null == adjacencyMatrix) {
            throw new BusinessException("核心算法还在进行中");
        }
        return adjacencyMatrix;
    }
 
    public int[][] getPathMatrix() {
        if (null == pathMatrix) {
            throw new BusinessException("核心算法还在进行中");
        }
        return pathMatrix;
    }
 
    public List<Long> getMatrixHeader() {
        if (null == matrixHeader) {
            throw new BusinessException("核心算法还在进行中");
        }
        return matrixHeader;
    }
 
    public Double[][] getFloydMatrix() {
        if (null == floydMatrix) {
            throw new BusinessException("核心算法还在进行中");
        }
        return floydMatrix;
    }
}