多线程并行 Dijkstra与A*算法结合实践

2023-11-02

多线程并行 Dijkstra与A*算法融合

1、Dijkstra总能找到最优解 但是时间消耗大 ,本文实现了多线程并行的搜索算法,使得路径搜素时间缩短约1/3

2、H作为传入参数可以使得本算法可以切换为A*也可以变为Dijkstra

3、本算法使得空间复杂度增加

4、程序构造了一个路网测试程序、后续准备进行路网随机生成测试

 

算法主程序


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
//并行计算主程序
public class CalculateThread extends Thread  {

    private Graph g;
    private Vertex from;
    private Vertex to;

    private Vertex result;
    private Double coefficientH;
    private Double coefficientG;
    CountDownLatch latch;
    private List<Vertex> openList = Collections.synchronizedList(new ArrayList<Vertex>());
    private List<Vertex> closeList = Collections.synchronizedList(new ArrayList<Vertex>());
    double sum = 0.0;

    public Vertex getResult() {
        return result;
    }

    public void setResult(Vertex result) {
        this.result = result;
    }

    public CalculateThread(Graph g, Vertex from, Vertex to, Double coefficientH, Double coefficientG,CountDownLatch latch){
        this.from = from;
        this.to = to;
        this.g = g;
        this.coefficientH = coefficientH;
        this.coefficientG = coefficientG;

        this.latch = latch;

    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();    //获取开始时间

        int flag =0;
        //while(flag<100) {

            Vertex parent = AstarCalculate(g, from, to, coefficientH, coefficientG);
            List<Vertex> arrayList = Collections.synchronizedList(new ArrayList<Vertex>());


            arrayList = parent != from ? getPaths(parent, from) : null;
            //System.out.println(arrayList.toString());
            //System.out.println("arrayList.size()"+arrayList.size());
            // System.out.println("计算结束");
            if (arrayList.size() > 0) {
                for (int i = 0; i < arrayList.size()-1; i++) {
                    for (Path path : g.PathHashSet) {
                        if (path.getFrom().nodeId.equals(arrayList.get(i).nodeId) && path.getTo().nodeId.equals(arrayList.get(i + 1).nodeId)
                                || path.getFrom().nodeId.equals(arrayList.get(i + 1).nodeId) && path.getTo().nodeId.equals(arrayList.get(i).nodeId)) {
                            sum += path.getWeight();

                        }
                    }
                }
            }
        Collections.reverse(arrayList);
            System.out.println("sum" + sum + "  arrayList"+arrayList.toString());

           // System.out.println(Thread.currentThread().getName() + "coefficientH: " + coefficientH);
           // System.out.println(arrayList.toString());
            flag++;
        //}
        long endTime = System.currentTimeMillis();    //获取结束时间

        System.out.println("计算结束运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间

        System.out.println("latch.countDown()");
        latch.countDown();
    }


    public Vertex AstarCalculate(Graph g, Vertex from, Vertex to, Double coefficientH, Double coefficientG) {
        List<Vertex> neighbour = from.getNeighbours();
        this.coefficientH = coefficientH;
        this.coefficientG = coefficientG;
        from.setErgodic(true);
        openList.add(from);

        openList.remove("2");
        closeList.add(g.vertexHashMap.get("2"));
        int flag =0;
        System.out.println("from: "+from);
        while (openList.size() > 0) {
            Vertex currentNode = findMinFVertexInOpneList();
            openList.remove(currentNode);
            closeList.add(currentNode);
            List<Vertex> neighborNodes = findNeighborNodes(currentNode);
            for(int k=0;k<closeList.size();k++){
                neighborNodes.remove(closeList.get(k));
            }
            for (Vertex node : neighborNodes) {
                flag++;
                if (exists(openList, node)) {
                    foundPoint(currentNode, node,g);
                } else {
                    notFoundPoint(currentNode, to, node,g);
                }
            }
            if (find(openList, to) != null) {
                System.out.println("遍历节点: "+flag);
                return find(openList, to);
            }
        }
        System.out.println("遍历节点: "+flag);
        return find(openList, to);

    }
    private synchronized  void foundPoint(Vertex tempStart, Vertex node,Graph g) {
        Double G = calcG2(tempStart, node,g);
        if (G < node.G) {
            node.parent = tempStart;
            node.G = G;
            node.calcF();
        }
    }
    private void notFoundPoint(Vertex tempStart, Vertex end, Vertex node,Graph g) {
        node.parent = tempStart;
        node.G = calcG(tempStart, node,g);
        //System.out.println("node.G: "+node.G);
        node.H = calcH(end, node);
        node.calcF();
        // System.out.println("node.getF(): "+node.getF());
        openList.add(node);
    }
    public Vertex findMinFVertexInOpneList() {
        Vertex bestNode = null;
        double bestF = Double.MAX_VALUE;
        for(Vertex node: openList){
            if(node.getF() < bestF){
                bestF = node.getF();
                bestNode = node;
            }
        }
        return bestNode;

    }

    private static ArrayList<Vertex> getPaths(Vertex endNode,Vertex startNode) {
        ArrayList<Vertex> arrayList = new ArrayList<Vertex>();
        Vertex pre = endNode;
        int flag =0;
        while (pre.nodeId != startNode.nodeId && flag<200) {
            arrayList.add(new Vertex(pre.nodeId, pre.x, pre.y, pre.isErgodic, pre.totalDistance));
            pre = pre.parent;
            flag++;
        }
        arrayList.add(new Vertex(startNode.nodeId, startNode.x, startNode.y, startNode.isErgodic, startNode.totalDistance));
        return arrayList;
    }

    public List<Vertex> findNeighborNodes(Vertex currentNode) {
        return currentNode.getNeighbours();
    }

    public static boolean exists(List<Vertex> maps, Vertex node) {
        for (Vertex n : maps) {
            if ((n.x == node.x ) && (n.y == node.y)) {
                return true;
            }
        }
        return false;
    }

    public static Vertex find(List<Vertex> maps, Vertex point) {
        for (Vertex n : maps)
            if ((n.x == point.x ) && (n.y == point.y)) {
                return n;
            }
        return null;
    }




    private Double calcG(Vertex start, Vertex node,Graph g) {

       // System.out.println("calcG: ");
       // System.out.println("start: "+ start);
       // System.out.println("node: "+ node);
       // System.out.println("PathHashSet: "+ g.PathHashSet);
        Double G = 0.0;
        for (Path path : g.PathHashSet) {
            if(path.getFrom().nodeId.equals(node.nodeId) && path.getTo().nodeId.equals(start.nodeId)){
                G = coefficientG*path.getWeight();
            }else if(path.getFrom().nodeId.equals(start.nodeId) && path.getTo().nodeId.equals(node.nodeId)){
                G = coefficientG*path.getWeight();
            }
        }
        //System.out.println("G: "+G);
        Double parentG = node.parent != null ? node.parent.G : 0;

        return G + parentG;
    }

    private Double calcG2(Vertex start, Vertex node,Graph g) {
        Double G = 0.0;
        for (Path path : g.PathHashSet) {
            if(path.getFrom().nodeId.equals(node.nodeId) && path.getTo().nodeId.equals(start.nodeId)){
                G = coefficientG*path.getWeight();
            }else if(path.getFrom().nodeId.equals(start.nodeId) && path.getTo().nodeId.equals(node.nodeId)){
                G = coefficientG*path.getWeight();
            }
        }
        //Double parentG = start.parent != null ? start.parent.G : 0;
        Double parentG = start.getG();
        return G + parentG;
    }

    private Double calcH(Vertex end, Vertex node) {
        Double step = coefficientH*((node.x - end.x)*(node.x - end.x) + (node.y - end.y)*(node.y - end.y));
        return Math.sqrt(step);
        // return 0.0;
    }
}

 

路径实体类

package com.graph;

import com.sun.javafx.scene.traversal.WeightedClosestCorner;

public class Path {

    Double Weight;
    private Vertex from;
    private Vertex to;


    public Path(Double weight, Vertex from, Vertex to) {
        Weight = weight;
        this.from = from;
        this.to = to;
    }

    @Override
    public String toString() {
        return "Path{" +
                "Weight=" + Weight +
                ", from=" + from +
                ", to=" + to +
                '}';
    }

    public Double getWeight() {
        return Weight;
    }

    public void setWeight(Double weight) {
        Weight = weight;
    }

    public Vertex getFrom() {
        return from;
    }

    public void setFrom(Vertex from) {
        this.from = from;
    }

    public Vertex getTo() {
        return to;
    }

    public void setTo(Vertex to) {
        this.to = to;
    }
}

顶点实体类

package com.graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Vertex {

    String nodeId;
    Double x;
    Double y;
    Boolean isErgodic;
    float totalDistance;
    private LinkedList<Vertex> neighbours;

    public Double F;
    public Double G;
    public Double H;
    public Vertex parent;



    public Vertex(String nodeId, Double x, Double y, Boolean isErgodic, float totalDistance, LinkedList<Vertex> neighbours) {
        this.nodeId = nodeId;
        this.x = x;
        this.y = y;
        this.isErgodic = isErgodic;
        this.totalDistance = totalDistance;
        this.neighbours = neighbours;
        this.G = 0.0;
        this.F = 0.0;
        this.H = 0.0;
    }
    public Vertex(Vertex v) {
        this.nodeId = v.nodeId;
        this.x = v.x;
        this.y = v.y;
        this.isErgodic = v.isErgodic;
        this.totalDistance = v.totalDistance;
        this.neighbours = v.neighbours;
        this.G =  v.G;
        this.F =  v.F;
        this.H =  v.H;
    }

    public Vertex(String nodeId, Double x, Double y, Boolean isErgodic, float totalDistance) {
        this.nodeId = nodeId;
        this.x = x;
        this.y = y;
        this.isErgodic = isErgodic;
        this.totalDistance = totalDistance;
        this.neighbours = new LinkedList<Vertex>();

    }

    public Vertex() {

    }

    @Override
    public String toString() {
        return "Vertex{" +
                "nodeId='" + nodeId + '\'' +

                '}';
    }

    public void calcF() {
        //this.F = this.G + this.H;
        this.F = this.G;
    }

    public Double getF() {
        return F;
    }

    public void setF(Double f) {
        F = f;
    }

    public Double getG() {
        return G;
    }

    public void setG(Double g) {
        G = g;
    }

    public Double getH() {
        return H;
    }

    public void setH(Double h) {
        H = h;
    }

    public Vertex getParent() {
        return parent;
    }

    public void setParent(Vertex parent) {
        this.parent = parent;
    }


    public float getTotalDistance() {
        return totalDistance;
    }

    public void setTotalDistance(float totalDistance) {
        this.totalDistance = totalDistance;
    }

    public LinkedList<Vertex> getNeighbours() {
        return neighbours;
    }

    public void setNeighbours(LinkedList<Vertex> neighbours) {
        this.neighbours = neighbours;
    }

    public String getNodeId() {
        return nodeId;
    }

    public Double getX() {
        return x;
    }

    public Double getY() {
        return y;
    }

    public Boolean getErgodic() {
        return isErgodic;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public void setY(Double y) {
        this.y = y;
    }

    public void setErgodic(Boolean ergodic) {
        isErgodic = ergodic;
    }


}

 

图的构造初始化:

package com.graph;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Graph {

    public ConcurrentHashMap<String, Vertex> vertexHashMap;
    public ConcurrentHashSet<Path> PathHashSet;

    @Override
    public String toString() {
        return "Graph{" +
                "vertexHashMap=" + vertexHashMap +
                ", PathHashSet=" + PathHashSet +
                '}';
    }

    public Vertex findVertex(String id){
       return (Vertex)vertexHashMap.get(id);
    }

    public Graph (ConcurrentHashMap<String,Vertex> vertexHashMap, ConcurrentHashSet<Path> PathHashSet) {
        this.vertexHashMap = vertexHashMap;
        this.PathHashSet = PathHashSet;

        for (String key : vertexHashMap.keySet()) {
            for (Path path : PathHashSet) {
                if(path.getTo().nodeId.equals(vertexHashMap.get(key).nodeId)){
                    Vertex v2 = new Vertex();
                    v2 = vertexHashMap.get(path.getFrom().nodeId);
                   // LinkedList<Vertex>  list = v.getNeighbours();
                  //  list.add(v2);
                    vertexHashMap.get(key).getNeighbours().add(v2);
                }else if(path.getFrom().nodeId.equals(vertexHashMap.get(key).nodeId)){
                    Vertex v2 = new Vertex();
                    v2 = vertexHashMap.get(path.getTo().nodeId);
                    vertexHashMap.get(key).getNeighbours().add(v2);
                }
            }

        }
    }
}

计算方法构造

package com.graph;

import com.Dijkstra.ParallelAstarRunnable;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.lang.Thread.sleep;

public  class  Calculate  {

    private List<Vertex> openList = Collections.synchronizedList(new ArrayList<Vertex>());
    private List<Vertex> closeList = Collections.synchronizedList(new ArrayList<Vertex>());



    public List<Vertex> getOpenList() {
        return openList;
    }

    public void setOpenList(List<Vertex> openList) {
        this.openList = openList;
    }

    public List<Vertex> getCloseList() {
        return closeList;
    }

    public void setCloseList(List<Vertex> closeList) {
        this.closeList = closeList;
    }

    public double getSum() {
        return sum;
    }

    public void setSum(double sum) {
        this.sum = sum;
    }

    public Double getCoefficientH() {
        return coefficientH;
    }

    public void setCoefficientH(Double coefficientH) {
        this.coefficientH = coefficientH;
    }

    public Double getCoefficientG() {
        return coefficientG;
    }

    public void setCoefficientG(Double coefficientG) {
        this.coefficientG = coefficientG;
    }

    double sum = 0.0;

    Double coefficientH =0.0;
    Double coefficientG = 0.0;



    public void AstarPath(ConcurrentHashMap<String, Vertex> vertexHashMap, ConcurrentHashSet<Path> PathHashSet, Vertex v2, Vertex v5,Double coefficientH,Double coefficientG){
             System.out.println("开始计算");
        long startTime = System.currentTimeMillis();    //获取开始时间

            Graph g = new Graph(vertexHashMap,PathHashSet);

           // Vertex parent =  AstarCalculate(g,v2,v5,coefficientH,coefficientG);
            //System.out.println(parent);

           // List<Vertex> arrayList = Collections.synchronizedList(new ArrayList<Vertex>());
           // arrayList = parent != v2 ? getPaths(parent,v2) : null;
            //System.out.println("arrayList.size()"+arrayList.size());
             //System.out.println("计算结束");

            // if(arrayList.size()>0){
        /*for (int i = 0; i < arrayList.size()-1; i++) {
            for (Path path : g.PathHashSet) {
                if(path.getFrom().nodeId.equals(arrayList.get(i).nodeId) && path.getTo().nodeId.equals(arrayList.get(i+1).nodeId)
                        || path.getFrom().nodeId.equals(arrayList.get(i+1).nodeId) && path.getTo().nodeId.equals(arrayList.get(i).nodeId)){
                    sum += path.getWeight();

                }
            }
        }
             }
        System.out.println("sum"+ sum);

        Collections.reverse(arrayList);
        System.out.println(Thread.currentThread().getName()+"coefficientH: "+coefficientH);
        System.out.println(arrayList.toString());*/
        }


    public Vertex AstarCalculate(ArrayList<Graph> graphs, Vertex from, Vertex to,Double coefficientH,Double coefficientG) {
        ExecutorService pool = Executors.newFixedThreadPool(16);
        List<Vertex> neighbour = from.getNeighbours();
        this.coefficientH = coefficientH;
        this.coefficientG = coefficientG;
        from.setErgodic(true);
        openList.add(from);
        Vector<Thread> threadVector =  new Vector<Thread>();
        Vertex currentNode = findMinFVertexInOpneList();
        List<Vertex> neighborNodes2 = new ArrayList<Vertex>();
        neighborNodes2 = findNeighborNodes(currentNode);
        final CountDownLatch latch = new CountDownLatch(neighborNodes2.size());
        // for (Vertex node : neighborNodes2) {
        // Graph g2 = new Graph(g.vertexHashMap,g.PathHashSet);
       // Vertex from2 = g.vertexHashMap.get("3");
        //pool.submit(new CalculateThread(g, g.vertexHashMap.get("5"), to, coefficientH,coefficientG,latch));
        CalculateThread v1 = new CalculateThread(graphs.get(0), graphs.get(0).vertexHashMap.get("3"), to, coefficientH,coefficientG,latch);
        CalculateThread v2 = new CalculateThread(graphs.get(1), graphs.get(1).vertexHashMap.get("4"), to, coefficientH,coefficientG,latch);
        CalculateThread v3 = new CalculateThread(graphs.get(2), graphs.get(2).vertexHashMap.get("5"), to, coefficientH,coefficientG,latch);
        CalculateThread v4 = new CalculateThread(graphs.get(3), graphs.get(3).vertexHashMap.get("11"), to, coefficientH,coefficientG,latch);
        CalculateThread v5 = new CalculateThread(graphs.get(4), graphs.get(4).vertexHashMap.get("1"), to, coefficientH,coefficientG,latch);
        pool.submit(v1);
        pool.submit(v2);
        pool.submit(v3);
        pool.submit(v4);
        pool.submit(v5);
        //}
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main Thread go on");
        pool.shutdown();
        return find(openList, to);
    }

}

 

图形节点与path初始化构造:

package com.graph;

import javax.management.StringValueExp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class ParallelAstarRunnable3 {


    Double coefficientH =0.0;
    Double coefficientG =0.0;

    ParallelAstarRunnable3(Double coefficientH,Double coefficientG){
        this.coefficientH = coefficientH;
        this.coefficientG = coefficientG;
    }


    public void cal() {



        Calculate a = new Calculate();
        // LinkedList<Vertex> list = new LinkedList<Vertex>();
        List<Vertex> list = new LinkedList<Vertex>();


        Vertex v1 = new Vertex("1", 0.0, 3.0, false,0,new LinkedList<Vertex>());
        Vertex v2 = new Vertex("2", 2.0, 0.0, false,0,new LinkedList<Vertex>());
        Vertex v3 = new Vertex("3", 1.5, 3.0, false,0,new LinkedList<Vertex>());
        Vertex v4 = new Vertex("4", 4.0, 2.0, false,0,new LinkedList<Vertex>());
        Vertex v5 = new Vertex("5", 8.0, 0.0, false,0,new LinkedList<Vertex>());
        Vertex v6 = new Vertex("6", 6.0, 6.0, false,0,new LinkedList<Vertex>());
        Vertex v7 = new Vertex("7", 4.0, 7.0, false,0,new LinkedList<Vertex>());
        Vertex v8 = new Vertex("8", 3.0, 9.0, false,0,new LinkedList<Vertex>());
        Vertex v9 = new Vertex("9", 4.0, 11.0, false,0,new LinkedList<Vertex>());
        Vertex v10 = new Vertex("10", 8.0, 8.0, false,0,new LinkedList<Vertex>());
        Vertex v11 = new Vertex("11", 8.0, 3.0, false,0,new LinkedList<Vertex>());
        Vertex v12 = new Vertex("12", 10.0, 5.0, false,0,new LinkedList<Vertex>());

        list.add(v1);
        list.add(v2);
        list.add(v3);
        list.add(v4);
        list.add(v5);
        list.add(v6);
        list.add(v7);
        list.add(v8);
        list.add(v9);
        list.add(v10);
        list.add(v11);
        list.add(v12);


        ConcurrentHashMap conMap = new ConcurrentHashMap<String, Vertex>();
        ConcurrentHashMap conMap2 = new ConcurrentHashMap<String, Vertex>();
        ConcurrentHashMap conMap3 = new ConcurrentHashMap<String, Vertex>();
        ConcurrentHashMap conMap4 = new ConcurrentHashMap<String, Vertex>();
        ConcurrentHashMap conMap5 = new ConcurrentHashMap<String, Vertex>();
        //Vertex(String nodeId, Double x, Double y, Boolean isErgodic, float totalDistance, LinkedList<Vertex> neighbours)
        for(int i = 0; i< 12 ;i++){
            conMap.put(String.valueOf(i+1),new Vertex(list.get(i).getNodeId(),list.get(i).getX(),list.get(i).getY(),list.get(i).isErgodic,list.get(i).getTotalDistance(),new LinkedList<Vertex>()));
            conMap2.put(String.valueOf(i+1),new Vertex(list.get(i).getNodeId(),list.get(i).getX(),list.get(i).getY(),list.get(i).isErgodic,list.get(i).getTotalDistance(),new LinkedList<Vertex>()));
            conMap3.put(String.valueOf(i+1),new Vertex(list.get(i).getNodeId(),list.get(i).getX(),list.get(i).getY(),list.get(i).isErgodic,list.get(i).getTotalDistance(),new LinkedList<Vertex>()));
            conMap4.put(String.valueOf(i+1),new Vertex(list.get(i).getNodeId(),list.get(i).getX(),list.get(i).getY(),list.get(i).isErgodic,list.get(i).getTotalDistance(),new LinkedList<Vertex>()));
            conMap5.put(String.valueOf(i+1),new Vertex(list.get(i).getNodeId(),list.get(i).getX(),list.get(i).getY(),list.get(i).isErgodic,list.get(i).getTotalDistance(),new LinkedList<Vertex>()));

        }
        List<ConcurrentHashMap> ConcurrentHashMaplist = new LinkedList<ConcurrentHashMap>();
        ConcurrentHashMaplist.add(conMap);
        ConcurrentHashMaplist.add(conMap2);
        ConcurrentHashMaplist.add(conMap3);
        ConcurrentHashMaplist.add(conMap4);
        ConcurrentHashMaplist.add(conMap5);

        ConcurrentHashSet<Path> pathSet = new ConcurrentHashSet<Path>();
        ConcurrentHashSet<Path> pathSet2 = new ConcurrentHashSet<Path>();
        ConcurrentHashSet<Path> pathSet3 = new ConcurrentHashSet<Path>();
        ConcurrentHashSet<Path> pathSet4 = new ConcurrentHashSet<Path>();
        ConcurrentHashSet<Path> pathSet5 = new ConcurrentHashSet<Path>();
        List<Path> listpath = new LinkedList<Path>();
        List<ConcurrentHashSet<Path>> listPathset =new LinkedList<ConcurrentHashSet<Path>>();
        for(int i =0;i<ConcurrentHashMaplist.size();i++){
            listpath.add(new Path(1.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("1")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("2"))));
            listpath.add(new Path(2.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("1")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("8"))));
            listpath.add(new Path(1.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("8")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("9"))));
            listpath.add(new Path(1.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("7")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("8"))));
            listpath.add(new Path(4.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("7")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("4"))));

            listpath.add(new Path(2.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("11")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("5"))));
            listpath.add(new Path(2.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("3")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("4"))));
            listpath.add(new Path(2.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("3")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("2"))));
            listpath.add(new Path(5.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("2")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("4"))));
            listpath.add(new Path(3.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("2")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("5"))));

            listpath.add(new Path(8.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("2")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("11"))));
            listpath.add(new Path(1.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("4")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("11"))));
            listpath.add(new Path(3.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("10")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("11"))));
            listpath.add(new Path(1.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("11")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("12"))));
            listpath.add(new Path(3.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("7")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("6"))));

            listpath.add(new Path(8.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("4")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("6"))));
            listpath.add(new Path(2.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("6")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("10"))));
            listpath.add(new Path(4.0, new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("8")), new Vertex((Vertex)ConcurrentHashMaplist.get(i).get("10"))));

        }


        for(int i =0;i<18;i++){
            pathSet.add(listpath.get(i));
        }
        for(int i =18;i<36;i++){
            pathSet2.add(listpath.get(i));
        }
        for(int i =36;i<54;i++){
            pathSet3.add(listpath.get(i));
        }
        for(int i =54;i<72;i++){
            pathSet4.add(listpath.get(i));
        }
        for(int i =72;i<90;i++){
            pathSet5.add(listpath.get(i));
        }

        int flag =0;
        Graph graph1 = new Graph(conMap, pathSet);
        Graph graph2 = new Graph(conMap2, pathSet2);
        Graph graph3 = new Graph(conMap3, pathSet3);
        Graph graph4 = new Graph(conMap4, pathSet4);
        Graph graph5 = new Graph(conMap5, pathSet5);


        ArrayList<Graph> graphs = new ArrayList<>();

        graphs.add(graph1);
        graphs.add(graph2);
        graphs.add(graph3);
        graphs.add(graph4);
        graphs.add(graph5);

        a.AstarCalculate(graphs,v2,v10,coefficientH, coefficientG);


    }
}

 

计算启动

package com.graph;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyExecutorService {

    public static void main(String[] args) {
        // public static ExecutorService newFixedThreadPool(int nThreads)
        ExecutorService pool = Executors.newFixedThreadPool(16);
       // pool.submit(new MyRunnable());
        //pool.submit(new MyRunnable());
        int flag =0;
       // while(flag<20000){
            //pool.submit(new ParallelAstarRunnable(1.0,1.0));
          //  pool.submit(new ParallelAstarRunnable(0.7,0.1));
           // pool.submit(new ParallelAstarRunnable(0.6,0.4));
           // pool.submit(new ParallelAstarRunnable(0.4,0.5));
            //pool.submit(new ParallelAstarRunnable(0.2,0.7));
           // pool.submit(new ParallelAstarRunnable(0.1,1.1));
            flag++;
       // }

       // ParallelAstarRunnable p = new ParallelAstarRunnable(1.0,1.0);
        ParallelAstarRunnable3 p3 = new ParallelAstarRunnable3(1.0,1.0);
       // p.cal();
        p3.cal();
        //结束线程池
       // pool.shutdown();
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

多线程并行 Dijkstra与A*算法结合实践 的相关文章

  • Java import 详解

    Java import 详解 1 package 机制 Java 的 package 机制类似于 C 的 namespace 机制 在编写 Java 程序时 随着程序架构越来越大 类的个数也越来越多 这时就会发现管理程序中维护类名称也是一件
  • el-form-item rules validator validate函数传参

    validator只能传3个参数 rule value cb 如果想传入额外的参数来做校验 那么需要通过在rules上嵌套一层 传入参数 如row 之后在函数中定义validator 就可以直接用到自己需要的参数了 我这边需要的是row 校
  • Android Studio apk 文件路径

    out production
  • java实现图片与base64转换

    如果你是一个软件开发 不论前端后端工程师 图片的处理你是肯定要会的 关于图片的Base64编码 你可能有点陌生 但是这是一个软件工程师应该要掌握的知识点 现在很多网友把图片与base64转换都做成了小工具如 http www yzcopen
  • Android 使用OpenCV的三种方式(Android Studio)

    其实最早接触OpenCV是很久很久之前的事了 大概在2013年的5 6月份 当时还是个菜逼 虽然现在也是个菜逼 在那一段时间 学了一段时间的android 并不算学 一个月都不到 之后再也没接触android 而是一直在接触java web
  • linux常见报错种类

    说起来日常的故障 其实 首先应该相到的就是 备份 毕竟再怎么牢固的系统或硬件都会有故障的时候 所以 备份放第一位 作为linux运维 多多少少会碰见这样那样的问题或故障 从中总结经验 查找问题 汇总并分析故障的原因 这是一个Linux运维工
  • [春秋云镜]CVE-2022-22733

    声明 中所涉及的技术 思路和 具仅供以安全为 的的学习交流使 任何 不得将其 于 法 途以及盈利等 的 否则后果 承担 所有渗透都需获取授权 靶场介绍 Apache ShardingSphere ElasticJob UI由于返回 toke
  • Kali 2020.1版本 更新不能解析域名问题解决

    Kali 2020 1版本 更新不能解析域名问题解决 问题阐述 解决方法 1 添加DNS解析服务器的ip地址 2 重启 3 kali联网即可更新
  • IC工程师入门必学《Verilog超详细教程》(附下载)

    Verilog HDL 简称 Verilog 是一种硬件描述语言 用于数字电路的系统设计 可对算法级 门级 开关级等多种抽象设计层次进行建模 Verilog 继承了 C 语言的多种操作符和结构 与另一种硬件描述语言 VHDL 相比 语法不是
  • 用U盘给虚拟机装系统——U深度

    下载一个u深度 将要安装的系统镜像放进 重装系统 创建虚拟机 按shift 修改位如下所示 按fn f10确认 选择第二个 进行磁盘分区 开始装机 完成后关机并把启动时间修改回去 如果拔出U盘后出现以下情况 把新添加的硬盘移除即可
  • 北京的三甲医院都是定点医院吗?不列入医保卡范围不能报销?

    北京有19家三甲医院看病 用医保卡实时报销 其他的三甲医院需要在蓝本上定点后 才能报销 1 中国医学科学院北京协和医院 2 首都医科大学附属北京同仁医院 3 首都医科大学宣武医院 4 首都医科大学附属北京友谊医院 5 北京大学第一医院 6
  • 不错的在线视频下载软件

    发现了一款非常好的下载在线视频软件 而且可以跨浏览器使用 它几乎支持所有的web浏览器 如IE Chrome Firefox Opera Safari等浏览器 支持Youtube Youku Ku6 6间房 凤凰卫视视频网等在线视频网站的视
  • 破解世界数学难题!GPT-4 得出P≠NP

    Datawhale干货 编辑 陈萍 来源 机器之心 这是对 LLM for Science 一次有希望的探索 对于身处科研领域的人来说 或多或少的都听到过 P NP 问题 该问题被克雷数学研究所收录在千禧年大奖难题中 里面有七大难题 大家熟

随机推荐

  • 一些关于CV和deeplearning的干货链接(长期更新)

    目录 yolo系列汇总 关于batch normalization的理解 各类归一化方法的总结及代码 YJango的卷积神经网络介绍 目标检测SSD讲解 关于AP PR曲线计算的理解 内附代码 生肉 英文 解释了yolov3的forward
  • vue2与vue3有什么区别?

    今天要说的vue3基本兼容我们所熟悉的vue2代码 一 两者基本的不同点 1 vue3固然是优点多多的 其3个主要的优点有 1 按需引用 2 组合式api 更加接近原生js 更加直观 3 vue3新增的set up中没有this 也就是说v
  • Android之OpenGL学习

    1 前言 本来一直就想做音视频开发这方面 包括我的毕业论文也是 可惜却太久没有接触有些陌生 遂写文章来复习 在这里有几个目标需要订下 第一个就是需要实现相机使用OpenGL ES进行渲染 第二个就是搞定实现一些初步的滤镜 第三个就是了解视频
  • 软件测试 中静态测试与动态测试的区别

    1 测试部分的不同 静态测试是指测试不运行的部分 只是检查和审阅 如规范测试 软件模型测试 文档测试等 动态测试是通常意义上的测试 也就是运行和使用软件 2 测试方式不同 静态测试 通过评审文档 阅读代码等方式测试软件称为静态测试 通过运行
  • python数字类型分为三类_Python | 数据类型

    Python让Python成为语言研究的利器Xu YangPhoneticSan学习参考 Python for Linguists Natural Language Processing with Python Introducing Py
  • 小白学加速乐的理解

    本来是想学下树美的 感觉太难了 就开始了学习加速乐的进程 网上文章挺多的 到了自己就整不起走 对大佬来说可能就是一些小知识点 无需挂齿 今天我把自己的理解做个小记录 1 2 打开抓包后 打开浏览器开发者工具 在debugger就断着了 大胆
  • 硬件电路点点滴滴“女屌逆袭”2---晶体三极管(1)

    一 晶体管基础知识 晶体管分2种 NPN PNP 晶体管通常封装为TO 92 下面是元件实物图 和 元件符合 NPN 当电压和电流被加到基极上时 NPN晶体管 其工作原理 就像水龙头 给控制开关一点压力 它就放出水来 同样给基极一定电压和电
  • PCL (再探)点云配准精度评价指标——均方根误差

    目录 一 算法原理 二 代码实现 三 代码解析 四 备注 本文由CSDN点云侠原创 原文链接 如果你不是在点云侠的博客中看到该文章 那么此处便是不要脸的爬虫 一 算法原理 见 点云配准精度评价指标 均方根误差 PCL 点云配准精度评价 点到
  • 【技术分享】搭建java项目引入外部依赖教程

    文章目录 引言 如何在linux中编译运行java程序 IDEA中新建一个简单的java工程项目并运行 IDEA中如何引入外部依赖并运行 maven引入log4j jar包 手工引入log4j jar包 如何使用命令行的方式添加外部依赖 如
  • 2021-01-07 库存锁定问题

    前言 今天同事突然问我 要是一个商品我直接下单所有库存 那么是不是要等到订单取消后另一个人才可以下单 我思考了下 确实是需要限制一下 下面是我参考的方案 方案 下单锁库存 支付锁库存 通过淘宝测试 n件以内下单是下单锁库存 n件以上是支付锁
  • 2021年华数杯数学建模A题电动汽车无线充电优化匹配研究求解全过程文档及程序

    2021年华数杯数学建模 A 题 电动汽车无线充电优化匹配研究 原题再现 电动汽车以环境污染小 噪音低 能源利用效率高 维修方便等优势深受消费者青睐 但现有电动汽车的有线充电方式操作复杂 且存在安全隐患 因此采用无线充电方式对电动汽车进行快
  • 算法——最小生成树与最短路径相关算法

    最小生成树算法 普利姆算法代码参考 https blog csdn net tingting256 article details 50471033 具体如何判断是否构成回路 举例说明 克鲁斯卡尔算法代码参考 https blog csdn
  • ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() ora.l

    小白随手记录改bug过程 if G nodes node source print type G nodes node 开始的代码 报错如标题 分析应该是将一个值与多个值或一个列表中的值相比较 匹配的原因 source是一个列表有多个值 遂
  • 华为OD题目: 预订酒店

    预订酒店 预订酒店 题目 放暑假了 小明决定到某旅游景点游玩 他在网上搜索到了各种价位的酒店 长度为 的数组 A 他的心理价位是X元 请都他篇先出k 个最接近x 元的酒店 n gt k gt 0 并由低到高打印酒店的价格 输入 第一行 n
  • 关于R实现多重插补及其可视化

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 进行多重插补 二 多重插补结果可视化 三 结果评估与结果选择 前言 接着对前文数据集进行多重插补来填补缺失值 利用的是mice包中的airquality数
  • VC++ 程序启动即隐藏

    所谓的隐藏是程序启动后不显示主窗体 网上介绍了很多方法 是否达到效果 众说纷纭 这里只介绍一种在项目中实际应用到的切实可行的方法 这里假设主窗体为CMainDialog 1 变量声明 BOOL m bShowWindow 2 给变量赋初始值
  • 爬虫python能做什么-Python除了能做爬虫之外还能做什么?

    原标题 Python除了能做爬虫之外还能做什么 1 web开发python拥有非常完善的与web服务器进行交互的库 以及大量的免费的前端网页模板 更具优势的是 有非常优秀且成熟的Django Web框架 功能一应俱全 请输入图片描述 2 l
  • Linux-交叉编译-linuxptp

    参考文档 https blog csdn net BUPTOctopus article details 86246335 Linux PTP官网介绍 http linuxptp sourceforge net 1 LinuxPTP源码下载
  • 这30个CSS选择器,你必须熟记(上)

    关注前端达人 与你共同进步 CSS的魅力就是让我们前端工程师像设计师一样进行网页的设计 我们能轻而易举的改变颜色 布局 制作出漂亮的影音效果等等 我们只需要改几行代码 不需要借助任何软件 就能轻而易举的实现 感觉就像魔法师一般 几秒钟就能得
  • 多线程并行 Dijkstra与A*算法结合实践

    多线程并行 Dijkstra与A 算法融合 1 Dijkstra总能找到最优解 但是时间消耗大 本文实现了多线程并行的搜索算法 使得路径搜素时间缩短约1 3 2 H作为传入参数可以使得本算法可以切换为A 也可以变为Dijkstra 3 本算