|
@@ -0,0 +1,149 @@
|
|
|
|
+package com.steerinfo.inPlantNavigation.service.impl;
|
|
|
|
+
|
|
|
|
+import com.steerinfo.inPlantNavigation.model.IPMMSVertex;
|
|
|
|
+import com.steerinfo.inPlantNavigation.model.IPMMSVertexEdge;
|
|
|
|
+import com.steerinfo.inPlantNavigation.service.IIPMMSVertexService;
|
|
|
|
+import org.apache.commons.lang.SerializationUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+@Service("ipmmsVertexService")
|
|
|
|
+public class IPMMSVertexServiceImpl implements IIPMMSVertexService {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ArrayList<IPMMSVertex> getObtainTheOptimalPath(String startpoint,String endPoint,Map<String, List<IPMMSVertexEdge>> vertexEdgeList, Map<String, IPMMSVertex> vertexList) {
|
|
|
|
+ //初始化数据
|
|
|
|
+ int[] startPoin=new int[]{0,-1};
|
|
|
|
+ //创建S战队(放入的是已经遍历过边的顶点)
|
|
|
|
+ List<String> obtainTheOptimalPath=new ArrayList();
|
|
|
|
+ //obtainTheOptimalPath.add(statpoint);
|
|
|
|
+ //创建 dist[] (起点到集合点中的最短路径) key:指向的点 new int[]{0,-1}; {权重、中间点}
|
|
|
|
+ HashMap<String,int[]> currentBestDinstance=new HashMap<>();
|
|
|
|
+ currentBestDinstance.put(startpoint,startPoin);
|
|
|
|
+ while (obtainTheOptimalPath.size()!=vertexList.size()){
|
|
|
|
+ // dist[]最小值
|
|
|
|
+ String stringMinimumValue = getStringMinimumValue(currentBestDinstance, obtainTheOptimalPath);
|
|
|
|
+ //加入S战队
|
|
|
|
+
|
|
|
|
+ obtainTheOptimalPath.add(stringMinimumValue);
|
|
|
|
+
|
|
|
|
+ //借东风
|
|
|
|
+ List<IPMMSVertexEdge> ipmmsVertexEdges = vertexEdgeList.get(stringMinimumValue);
|
|
|
|
+ //遍历所有的边
|
|
|
|
+ for(IPMMSVertexEdge ipmmsVertexEdge :ipmmsVertexEdges){
|
|
|
|
+ //箭头出发点
|
|
|
|
+ BigDecimal outVertexID = ipmmsVertexEdge.getOutVertexID();
|
|
|
|
+ //到该点的最短路径
|
|
|
|
+ int[] starBestPath = currentBestDinstance.get(outVertexID.toString());
|
|
|
|
+ //被指向的顶点
|
|
|
|
+ BigDecimal inVertexID= ipmmsVertexEdge.getInVertexID();
|
|
|
|
+ int[] historyBestPaht = currentBestDinstance.get(inVertexID.toString());
|
|
|
|
+ //判断是否存在其路线到这个点的距离。如果不存在则将这条线路作为起点到该点最短路径、如果本条路线是最短路径需要替换最短路径
|
|
|
|
+ if(historyBestPaht==null||(starBestPath[0]+ipmmsVertexEdge.getWeigh())<historyBestPaht[0]){
|
|
|
|
+ int distance=starBestPath[0]+ipmmsVertexEdge.getWeigh();
|
|
|
|
+ int outVertex=outVertexID.intValue();
|
|
|
|
+ int[] bestPath=new int[]{distance,outVertex};
|
|
|
|
+ currentBestDinstance.put(inVertexID.toString(),bestPath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return startPointToEndPointPaht(endPoint,currentBestDinstance,vertexList);
|
|
|
|
+ }
|
|
|
|
+ //通过所有最优解集合选择我们要的
|
|
|
|
+ public ArrayList<IPMMSVertex> startPointToEndPointPaht(String point,HashMap<String,int[]> currentBestDinstance,Map<String, IPMMSVertex> vertexList){
|
|
|
|
+ ArrayList<IPMMSVertex> obtainOptimalPath=new ArrayList();
|
|
|
|
+ int beforeDistance=100;
|
|
|
|
+ do {
|
|
|
|
+ int[] ints = currentBestDinstance.get(point);
|
|
|
|
+ beforeDistance=ints[1];
|
|
|
|
+ obtainOptimalPath.add(vertexList.get(point));
|
|
|
|
+ point=String.valueOf(ints[1]);
|
|
|
|
+ }
|
|
|
|
+ while (beforeDistance!=-1);
|
|
|
|
+ return obtainOptimalPath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getStringMinimumValue(HashMap<String,int[]> currentBestDinstance,List<String> obtainTheOptimalPath){
|
|
|
|
+
|
|
|
|
+ HashMap<String,int[]> bestDinstance = (HashMap<String, int[]>) SerializationUtils.clone(currentBestDinstance);
|
|
|
|
+
|
|
|
|
+ for (String item:obtainTheOptimalPath){
|
|
|
|
+ if (bestDinstance.containsKey(item)){
|
|
|
|
+ bestDinstance.remove(item);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Set<String> keys = bestDinstance.keySet();
|
|
|
|
+ String vertex="";
|
|
|
|
+ int minimumValue=100;
|
|
|
|
+ for (String key :keys){
|
|
|
|
+ int[] value = currentBestDinstance.get(key);
|
|
|
|
+ if (minimumValue>value[1]){
|
|
|
|
+ minimumValue=value[1];
|
|
|
|
+ vertex=key;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return vertex;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Map<String,IPMMSVertex> initIPMMSVertex(){
|
|
|
|
+
|
|
|
|
+ Map<String,IPMMSVertex> IPMMSVertexList=new HashMap<>();
|
|
|
|
+
|
|
|
|
+ IPMMSVertex ipmmsVertex0=new IPMMSVertex();
|
|
|
|
+ ipmmsVertex0.setVertexID(new BigDecimal(0));
|
|
|
|
+ ipmmsVertex0.setAddressName("小东门");
|
|
|
|
+ ipmmsVertex0.setLongitude(new BigDecimal(23.555521));
|
|
|
|
+ ipmmsVertex0.setLatitude(new BigDecimal(23.555521));
|
|
|
|
+
|
|
|
|
+ IPMMSVertexList.put("0",ipmmsVertex0);
|
|
|
|
+
|
|
|
|
+ IPMMSVertex ipmmsVertex1=new IPMMSVertex();
|
|
|
|
+ ipmmsVertex1.setVertexID(new BigDecimal(1));
|
|
|
|
+ ipmmsVertex1.setAddressName("一棒库");
|
|
|
|
+ ipmmsVertex1.setLongitude(new BigDecimal(23.555521));
|
|
|
|
+ ipmmsVertex1.setLatitude(new BigDecimal(23.555521));
|
|
|
|
+
|
|
|
|
+ IPMMSVertexList.put("1",ipmmsVertex1);
|
|
|
|
+
|
|
|
|
+ IPMMSVertex ipmmsVertex2=new IPMMSVertex();
|
|
|
|
+ ipmmsVertex2.setVertexID(new BigDecimal(2));
|
|
|
|
+ ipmmsVertex2.setAddressName("二棒库");
|
|
|
|
+ ipmmsVertex2.setLongitude(new BigDecimal(23.555521));
|
|
|
|
+ ipmmsVertex2.setLatitude(new BigDecimal(23.555521));
|
|
|
|
+
|
|
|
|
+ IPMMSVertexList.put("2",ipmmsVertex2);
|
|
|
|
+
|
|
|
|
+ IPMMSVertex ipmmsVertex3=new IPMMSVertex();
|
|
|
|
+ ipmmsVertex3.setVertexID(new BigDecimal(3));
|
|
|
|
+ ipmmsVertex3.setAddressName("高线库");
|
|
|
|
+ ipmmsVertex3.setLongitude(new BigDecimal(23.555521));
|
|
|
|
+ ipmmsVertex3.setLatitude(new BigDecimal(23.555521));
|
|
|
|
+
|
|
|
|
+ IPMMSVertexList.put("3",ipmmsVertex3);
|
|
|
|
+
|
|
|
|
+// IPMMSVertex ipmmsVertex4=new IPMMSVertex();
|
|
|
|
+// ipmmsVertex4.setVertexID(new BigDecimal(4));
|
|
|
|
+// ipmmsVertex4.setAddressName("拐角1");
|
|
|
|
+// ipmmsVertex4.setLongitude(new BigDecimal(23.555521));
|
|
|
|
+// ipmmsVertex4.setLatitude(new BigDecimal(23.555521));
|
|
|
|
+//
|
|
|
|
+// IPMMSVertexList.put("4",ipmmsVertex4);
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// IPMMSVertex ipmmsVertex5=new IPMMSVertex();
|
|
|
|
+// ipmmsVertex5.setVertexID(new BigDecimal(5));
|
|
|
|
+// ipmmsVertex5.setAddressName("拐角2");
|
|
|
|
+// ipmmsVertex5.setLongitude(new BigDecimal(23.555521));
|
|
|
|
+// ipmmsVertex5.setLatitude(new BigDecimal(23.555521));
|
|
|
|
+//
|
|
|
|
+// IPMMSVertexList.put("4",ipmmsVertex5);
|
|
|
|
+ return IPMMSVertexList;
|
|
|
|
+ }
|
|
|
|
+}
|