I m在一条最短的道路上工作,在java使用一种 algorithm泥 d法。 I m 在该方案中执行以下Kkouery approx 300倍,以便从10 000辆公共汽车连接数据库中找到通道。 大约需要6-7秒才能执行300次询问。 关于我如何加快这项工作的建议,或关于一种可使用的不同方法的任何想法? 增 编
private HashMap<Coordinate,Node> closedNodes;
private PriorityQueue<Node> openNodes;
..
private List<Coordinate> calculatePath()
{
//While there are nodes in the open list
while (!openNodes.isEmpty())
{
//Get the node with the lowest gVal+hVal
Node node = openNodes.poll();
//Add it to the closed list
closedNodes.put(node);
//If it is not the goal node
if (!node.equals(goal))
{
//Get all the neighbours and Create neighbour node
List<Node> neighbours = helper.getNeighbours(node, goal);
//For each neighbour
for (Node neighbourNode : neighbours)
{
//Check if the neighbour is in the list of open nodes
boolean isInOpen = checkOpenNodes(neighbourNode);
//If it is not in the open nodes and not in the closed nodes
if ((!closedNodes.containsKey(neighbourNode))&& (!isInOpen))
{
//Add it to the list of open nodes
openNodes.add(neighbourNode);
}
}
}
else
{
// We found the path
path = backTrackPath(node);
break;
}
}
return path;
/**
* Gets the list of valid Nodes that are possible to travel to from <b>Node</b>
* @param stopNode Node to find neighbours for
* @param goal End Node
* @return list of neighbour Nodes
*/
public ArrayList<Node> getNeighbours(Node stopNode, Node goal)
{
ArrayList<Node> neighbours = new ArrayList<Node>();
Node neighbourNode;
//get neighbours connected to stop
try {
ResultSet rs = stmt.executeQuery("select To_Station_id, To_Station_routeID, To_Station_stopID," +
"To_Station_lat, To_Station_lng, Time from connections where Connections.From_Station_stopID ="
+stopNode.getCoord().getStopID()+" ORDER BY Connections.Time");
rs = stmt.getResultSet();
while (rs.next()) {
int id = rs.getInt("To_Station_id");
String routeID = rs.getString("To_Station_routeID");
String stopID = rs.getString("To_Station_stopID");
String stopName = rs.getString("To_Station_stopName");
Double lat = rs.getDouble("To_Station_lat");
Double lng = rs.getDouble("To_Station_lng");
int time = rs.getInt("Time");
neighbourNode = new Node(id, routeID, stopID, stopName, lat, lng);
neighbourNode.prev = stopNode;
neighbourNode.gVal = stopNode.gVal + time;
neighbourNode.hVal = heuristic.calculateHeuristic(neighbourNode, goal);
neighbours.add(neighbourNode);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return neighbours;
}