Java で A* 検索アルゴリズムを使用しています。ツアーを印刷して、ユーザーがどのルートを試し、どのルートが最適かを確認できるようにしたいと考えています。現時点では、最適なルートを出力するだけですが、それは問題ありません。それを実行したいのですが、ルートのリストも出力して、それぞれの最悪のコストと関連するコストを確認できるようにしたいと考えています。以下のコードから、すべてのツアーを出力する followedRoute を出力すると、それぞれのコストを出力できますか? アルゴリズムは、完全な各ツアーとそれらの最低コストを見つけることで機能します。理想的には、{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);
}
}
}
どんな助けでも大歓迎です!前もって感謝します :)