问题
I'm developing an algorithm to find the best route between two airports. I came across the concept of All Pairs Shortest Path (APSP) and the GraphStream library.
The content is read from a text file. Each line represents an airline stopover:
PEK,LAX,5
ATL,HND,75
ATL,LAX,20
DXB,HND,5
ATL,PEK,10
PEK,HND,40
LAX,DXB,20
ATL,DXB,55
Because it's a problem involving airline tickets, I called the 3rd column "price", but when I tried putting real price values in it, some routes stopped working. Also, one of the features is adding new lines to the file. I'm having problems when adding any values I want for new stopovers.
Reading a StackExchange's Math forum thread, I tried dividing all values by the biggest weight, before creating the graph string, but had no success:
String graphString;
String graphStringHeader = "DGS004\nmy 0 0\n";
String graphStringNodes = "";
String graphStringEdges = "";
BigDecimal biggestPrice = BigDecimal.ZERO;
while (stLines.hasMoreTokens()) {
line = stLines.nextToken(System.getProperty("line.separator"));
final StringTokenizer stColumns = new StringTokenizer(line);
Airport originAux = null;
Airport destinationAux = null;
BigDecimal priceAux = null;
String column;
Integer columnIndex = 1;
while (stColumns.hasMoreTokens()) {
column = stColumns.nextToken(",");
if (columnIndex == 1) {
originAux = new Airport(column);
if (!nodes.contains(column)) {
// an = add node
graphStringNodes += "an " + column + " \n";
nodes.add(column);
}
} else if (columnIndex == 2) {
destinationAux = new Airport(column);
if (!nodes.contains(column)) {
// an = add node
graphStringNodes += "an " + column + " \n";
nodes.add(column);
}
} else if (columnIndex == 3) {
double parsedPreco = Double.parseDouble(column);
priceAux = BigDecimal.valueOf(parsedPreco);
/**
* Normalizing values.
*/
if (priceAux.intValue() > biggestPrice.intValue()) {
biggestPrice = priceAux;
} else {
priceAux = priceAux.divide(biggestPrice, 2, RoundingMode.HALF_UP);
}
edges.add(originAux.getName() + "," + destinationAux.getName()+ "," + priceAux.intValue());
stopovers.add(new Stopover(originAux, destinationAux, priceAux));
// ae = add edge
graphStringEdges += "ae " + originAux.getName() + "_" + destinationAux.getName() + " "
+ originAux.getName() + " > " + destinationAux.getName()
+ " price:" + priceAux.intValue()
+ " \n";
}
columnIndex++;
}
lineIndex++;
}
graphString = graphStringHeader + graphStringNodes + graphStringEdges;
That's the exception thrown:
java.lang.NullPointerException: null
at org.graphstream.graph.Path.add(Path.java:230)
at org.graphstream.algorithm.APSP$APSPInfo.getShortestPathTo(APSP.java:594)
at service.RouteFinderService.findRoute(RouteFinderService.java:183)
Service's 230th line is: Path shortestPath = edge.getShortestPathTo(destination.getName())
. The parameter is the destination airport code.
That how I instantiate the Graph
and APSP
object:
Graph graph = new DefaultGraph("BestRouteGraph");
ByteArrayInputStream bs = new ByteArrayInputStream(graphString.getBytes());
FileSourceDGS source = new FileSourceDGS();
source.addSink(graph);
source.readAll(bs);
APSP apsp = new APSP();
apsp.init(graph);
apsp.setDirected(false);
apsp.setWeightAttributeName("price");
apsp.compute();
回答1:
at org.graphstream.graph.Path.add(Path.java:230)
refers to the use of the method getOpposite in the APSPInfo class
nodePath.push(edge.getOpposite(from));
Make sure to check that there is a path to the node :
for(Node n : graph.getNodeSet()) {
for(Node n2 : graph.getNodeSet()) {
if (n != n2) {
System.out.println("Shortest Path between "+n+" and "+n2+" :");
try {
APSPInfo nodeInfo = n.getAttribute("APSPInfo");
System.out.println(nodeInfo.getShortestPathTo(n2.getId()));
} catch (Exception e) {
System.out.println("no path");
}
}
}
}
For example, in your dataset, there is no path to ATL as you can see in the graph.
Here the result you should have:
Shortest Path between PEK and LAX :
[PEK, LAX]
Shortest Path between PEK and ATL :
no path
Shortest Path between PEK and HND :
[PEK, LAX, DXB, HND]
Shortest Path between PEK and DXB :
[PEK, LAX, DXB]
Shortest Path between LAX and PEK :
no path
Shortest Path between LAX and ATL :
no path
Shortest Path between LAX and HND :
[LAX, DXB, HND]
Shortest Path between LAX and DXB :
[LAX, DXB]
Shortest Path between ATL and PEK :
[ATL, PEK]
Shortest Path between ATL and LAX :
[ATL, PEK, LAX]
Shortest Path between ATL and HND :
[ATL, PEK, LAX, DXB, HND]
Shortest Path between ATL and DXB :
[ATL, PEK, LAX, DXB]
Shortest Path between HND and PEK :
no path
Shortest Path between HND and LAX :
no path
Shortest Path between HND and ATL :
no path
Shortest Path between HND and DXB :
no path
Shortest Path between DXB and PEK :
no path
Shortest Path between DXB and LAX :
no path
Shortest Path between DXB and ATL :
no path
Shortest Path between DXB and HND :
[DXB, HND]
来源:https://stackoverflow.com/questions/60963398/nullpointerexception-at-org-graphstream-algorithm-apspapspinfo-getshortestpatht