मुझे अपने ए * एल्गोरिदम कार्यान्वयन के साथ कुछ मदद चाहिए। जब मैं एल्गोरिदम चलाता हूं तो उसे लक्ष्य मिल जाता है, लेकिन पथ निश्चित रूप से सबसे छोटा नहीं है :-Pए * एल्गोरिदम ठीक से काम नहीं कर रहा है
यहां मेरा कोड है, कृपया मुझे बग स्पॉट करने में सहायता करें! मुझे लगता है कि यह पुनर्निर्माण पथ हो सकता है जो मेरी समस्या है लेकिन मुझे यकीन नहीं है।
public class Pathfinder {
public List<Node> aStar(Node start, Node goal, WeightedGraph graph) {
Node x, y;
int tentative_g_score;
boolean tentative_is_better;
FScoreComparator comparator = new FScoreComparator();
List<Node> closedset = new ArrayList<Node>();
Queue<Node> openset = new PriorityQueue<Node>(10, comparator);
openset.add(start);
start.g_score = 0;
start.h_score = heuristic_cost_estimate(start, goal);
start.f_score = start.h_score;
while (!openset.isEmpty()) {
x = openset.peek();
if (x == goal) {
return reconstruct_path(goal);
}
x = openset.remove();
closedset.add(x);
for (Edge e : graph.adj(x)) {
if (e.v == x) {
y = e.w;
} else {
y = e.v;
}
if (closedset.contains(y) || y.illegal) {
continue;
}
tentative_g_score = x.g_score + e.weight;
if (!openset.contains(y)) {
openset.add(y);
tentative_is_better = true;
} else if (tentative_g_score < y.g_score) {
tentative_is_better = true;
} else {
tentative_is_better = false;
}
if (tentative_is_better) {
y.g_score = tentative_g_score;
y.h_score = heuristic_cost_estimate(y, goal);
y.f_score = y.g_score + y.h_score;
y.parent = x;
}
}
}
return null;
}
private int heuristic_cost_estimate(Node start, Node goal) {
return Math.abs(start.x - goal.x) + Math.abs(start.y - goal.y);
}
private List<Node> reconstruct_path(Node current_node) {
List<Node> result = new ArrayList<Node>();
while (current_node != null) {
result.add(current_node);
current_node = current_node.parent;
}
return result;
}
private class FScoreComparator implements Comparator<Node> {
public int compare(Node n1, Node n2) {
if (n1.f_score < n2.f_score) {
return 1;
} else if (n1.f_score > n2.f_score) {
return -1;
} else {
return 0;
}
}
}
} सभी महान जवाब के लिए हर किसी के लिए
धन्यवाद! मेरा ए * एल्गोरिदम अब आपके लिए पूरी तरह से धन्यवाद करता है! :-)
यह मेरी पहली पोस्ट थी और यह मंच वाकई अद्भुत है!
यह आपकी समस्या डोमेन के ज्ञान के बिना उत्तर देने के लिए असंभव नहीं है, तो यह बहुत कठिन है। क्या आप एल 1-स्पेस (उदाहरण के लिए एक ग्रिड) के माध्यम से पथ खोज रहे हैं? यदि नहीं, तो आप इसके बजाय यूक्लिडियन दूरी हेरिस्टिक पर स्विच करना चाहेंगे। –