我在 Java 中有一个 A* 搜索算法,我希望它能够打印旅行,以便用户可以看到它尝试了哪些路线以及哪些路线是最佳路线。此刻它只打印最佳路线,这很好,我希望它这样做,但我也希望它打印路线列表,以便您可以看到每条路线的最差和相关成本。从下面的代码中,如果我打印 followRoute 打印出每个旅行,但我可以打印每个旅行的费用吗?该算法通过查找每个完整的游览和其中最低的成本来工作,理想情况下我只想打印完整的游览而不是 {0}、{0、3} 等。
以下是我认为的相关代码段,如果您需要再看,请询问:)
Cities aux = currentCities;
ArrayList followedRoute = new ArrayList();
followedRoute.add(aux.number);
while (aux.level != 0) {
aux = aux.parent;
followedRoute.add(0, aux.number);
}
if (currentCities.level == distances.getCitiesCount()) {
solution = true;
bestRoute = followedRoute;
bestCost = currentCities.g;
} else {
for (int i=0; i<distances.getCitiesCount(); i++) {
// have we visited this city in the current followed route?
boolean visited = followedRoute.contains(i);
boolean isSolution = (followedRoute.size() == distances.getCitiesCount())&&(i == firstNode);
if (!visited || isSolution) {
Cities childCities = new Cities(i, currentCities.g + distances.getCost(currentCities.number, i),
getHeuristicValue(currentCities.level + 1), currentCities.level + 1);
childCities.parent = currentCities;
opened.add(childCities);
System.out.println(followedRoute);
}
}
}
非常感谢任何帮助!提前致谢 :)