车辆伴随分析算法实现:识别伴随车辆并计算伴随时间
车辆伴随分析算法实现:识别伴随车辆并计算伴随时间
问题描述:
已知一个过车记录集合 List<AnalysisJudgmentVehiceFollowVO>,其中包含车辆的号牌种类、号牌号码、过车时间等信息。需要根据过车记录,识别出可能伴随的车辆,并计算伴随时间。
算法规则:
- 对于两辆不同的车辆A和B,如果它们都经过了相同的设备列表,且经过每个相同设备的过车时间间隔小于等于阈值X,则认为A和B为伴随车辆。
- 伴随次数 (bscs) 为两辆车经过相同设备的次数。
- 伴随时间 (bssj) 为伴随车辆经过最后一个相同设备的过车时间减去伴随车辆经过第一个相同设备的过车时间,单位为分钟,字符串类型。
- 如果伴随次数大于等于阈值Y,则认为A和B为伴随车辆。
算法实现:
public List<AnalysisJudgmentFollowCarVO> getFollowCarList(List<AnalysisJudgmentVehiceFollowVO> vehicleList, int X, int Y) {
List<AnalysisJudgmentFollowCarVO> followCarList = new ArrayList<>();
Map<String, List<AnalysisJudgmentVehiceFollowVO>> vehicleMap = new HashMap<>();
// 将过车记录按照车牌号码和号牌种类进行分组
for (AnalysisJudgmentVehiceFollowVO vehicle : vehicleList) {
String key = vehicle.getHpzl() + vehicle.getHphm();
if (!vehicleMap.containsKey(key)) {
vehicleMap.put(key, new ArrayList<>());
}
vehicleMap.get(key).add(vehicle);
}
// 遍历每一组车辆过车记录
for (Map.Entry<String, List<AnalysisJudgmentVehiceFollowVO>> entry : vehicleMap.entrySet()) {
List<AnalysisJudgmentVehiceFollowVO> vehicleRecords = entry.getValue();
// 对过车记录按照设备编号和过车时间进行排序
Collections.sort(vehicleRecords, Comparator.comparing(AnalysisJudgmentVehiceFollowVO::getSbid)
.thenComparing(AnalysisJudgmentVehiceFollowVO::getGcsj));
// 遍历每两辆车之间的过车记录
for (int i = 0; i < vehicleRecords.size() - 1; i++) {
AnalysisJudgmentVehiceFollowVO vehicleA = vehicleRecords.get(i);
for (int j = i + 1; j < vehicleRecords.size(); j++) {
AnalysisJudgmentVehiceFollowVO vehicleB = vehicleRecords.get(j);
// 如果两辆车经过相同设备的数量小于等于1,则不进行计算
if (vehicleA.getSbid().equals(vehicleB.getSbid())) {
continue;
}
int bscs = 0; // 伴随次数
int bscTime = 0; // 伴随时间(单位:分钟)
List<String> sbidList = new ArrayList<>(); // 相同设备集合
int k = i + 1;
int l = j + 1;
// 计算两辆车之间的伴随情况
while (k < vehicleRecords.size() && l < vehicleRecords.size()) {
AnalysisJudgmentVehiceFollowVO recordK = vehicleRecords.get(k);
AnalysisJudgmentVehiceFollowVO recordL = vehicleRecords.get(l);
if (recordK.getSbid().equals(recordL.getSbid())
&& (DateTimeUtils.getIntervalMinutes(recordK.getGcsj(), recordA.getGcsj()) <= X)
&& (DateTimeUtils.getIntervalMinutes(recordL.getGcsj(), recordB.getGcsj()) <= X)) {
bscs++;
sbidList.add(recordK.getSbid());
if (bscs == 1) {
bscTime = DateTimeUtils.getIntervalMinutes(recordL.getGcsj(), recordK.getGcsj());
}
k++;
l++;
} else if (recordK.getSbid().compareTo(recordL.getSbid()) < 0) {
k++;
} else {
l++;
}
}
// 如果伴随次数大于等于阈值Y,则将两辆车加入新集合中
if (bscs >= Y) {
AnalysisJudgmentFollowCarVO followCar = new AnalysisJudgmentFollowCarVO();
followCar.setHpzl(vehicleA.getHpzl());
followCar.setHphm(vehicleA.getHphm());
followCar.setBscHpzl(vehicleB.getHpzl());
followCar.setBscHphm(vehicleB.getHphm());
followCar.setBssj(String.valueOf(bscTime));
followCar.setSbidList(sbidList);
followCarList.add(followCar);
}
}
}
}
return followCarList;
}
代码说明:
vehicleList: 输入的过车记录集合,类型为List<AnalysisJudgmentVehiceFollowVO>。X: 时间间隔阈值,单位为分钟。Y: 伴随次数阈值。followCarList: 返回的伴随车辆集合,类型为List<AnalysisJudgmentFollowCarVO>。vehicleMap: 用于存储过车记录,键为车辆的号牌种类和号牌号码的组合,值为该车辆的过车记录集合。DateTimeUtils.getIntervalMinutes(String time1, String time2): 用于计算两个时间之间的间隔,单位为分钟。
应用场景: 该算法可以应用于交通管理、车辆追踪等领域,用于识别可能伴随的车辆,并分析其行为模式。例如,可以利用该算法来识别可能共同行动的车辆,或分析车辆的出行轨迹。
原文地址: https://www.cveoy.top/t/topic/n1pN 著作权归作者所有。请勿转载和采集!