Willkommen |
|
myGully |
|
Links |
|
Forum |
|
|
|
 |
18.12.11, 00:24
|
#1
|
Anfänger
Registriert seit: Sep 2011
Beiträge: 4
Bedankt: 0
|
A* Java problem
Hi, hat irgendwer eine Ahnung wieso dieser A* nie closest.equals(end) validiert?
PHP-Code:
private ArrayList<Point> open;
private ArrayList<Point> closed;
private HashMap<Point, Point> parents;
private HashMap<Point,Integer> totalCost;
public PathFinder() {
this.open = new ArrayList<Point>();
this.closed = new ArrayList<Point>();
this.parents = new HashMap<Point, Point>();
this.totalCost = new HashMap<Point, Integer>();
}
private int heuristicCost(Point from, Point to) {
return Math.max(Math.abs(from.x - to.x), Math.abs(from.y - to.y));
}
private int costToGetTo(Point from) {
return parents.get(from) == null ? 0 : (1 + costToGetTo(parents.get(from)));
}
private int totalCost(Point from, Point to) {
if (totalCost.containsKey(from))
return totalCost.get(from);
int cost = costToGetTo(from) + heuristicCost(from, to);
totalCost.put(from, cost);
return cost;
}
private void reParent(Point child, Point parent){
parents.put(child, parent);
totalCost.remove(child);
}
public ArrayList<Point> findPath(Creature creature, Point start, Point end, int maxTries) {
open.clear();
closed.clear();
parents.clear();
totalCost.clear();
open.add(start);
for (int tries = 0; tries < maxTries && open.size() > 0; tries++){
Point closest = getClosestPoint(end);
open.remove(closest);
closed.add(closest);
if (closest.equals(end))
return createPath(start, closest);
else
checkNeighbors(creature, end, closest);
}
return null;
}
private Point getClosestPoint(Point end) {
Point closest = open.get(0);
for (Point other : open){
if (totalCost(other, end) < totalCost(closest, end))
closest = other;
}
return closest;
}
private void checkNeighbors(Creature creature, Point end, Point closest) {
for (Point neighbor : closest.neighbors8()) {
if (closed.contains(neighbor)
//|| !creature.canEnter(neighbor.x, neighbor.y)
&& !neighbor.equals(end))
continue;
if (open.contains(neighbor))
reParentNeighborIfNecessary(closest, neighbor);
else
reParentNeighbor(closest, neighbor);
}
}
private void reParentNeighbor(Point closest, Point neighbor) {
reParent(neighbor, closest);
open.add(neighbor);
}
private void reParentNeighborIfNecessary(Point closest, Point neighbor) {
Point originalParent = parents.get(neighbor);
double currentCost = costToGetTo(neighbor);
reParent(neighbor, closest);
double reparentCost = costToGetTo(neighbor);
if (reparentCost < currentCost)
open.remove(neighbor);
else
reParent(neighbor, originalParent);
}
private ArrayList<Point> createPath(Point start, Point end) {
ArrayList<Point> path = new ArrayList<Point>();
while (!end.equals(start)) {
path.add(end);
end = parents.get(end);
}
Collections.reverse(path);
return path;
}
|
|
|
18.12.11, 16:32
|
#2
|
Hinter dir!
Registriert seit: Apr 2010
Beiträge: 1.125
Bedankt: 487
|
Wahrscheinlich, weil er nicht zum Endpunkt kommt.
Lass dir mal während er Rechnet die Variablen anzeigen, dann kann man den Fehler besser finden.
|
|
|
18.12.11, 17:06
|
#3
|
Anfänger
Registriert seit: Sep 2011
Beiträge: 4
Bedankt: 0
|
Das ist bei einem A* ohne aufwendige visuelle aufbereitung leider recht schwierig, naja bin jetzt auf einen Dijkstra umgestiegen, wenn jemand doch noch den Fehler findet bitte bescheid sagen ;D
|
|
|
01.01.12, 16:17
|
#4
|
Anfänger
Registriert seit: May 2010
Beiträge: 30
Bedankt: 18
|
hast du das selbst geschrieben?
Wenn ja dann dokumentiere den Code mal ordentlich(solltest du immer tun vorallem wenn andere mal schnell einen Blick drauf werfen sollen)
Dann überschreib die equals Meth so dass sie genau das macht was du möchtest.
So hast du garantiert den Fehler schnell gefunden
und vllt noch was gelernt
|
|
|
Forumregeln
|
Du kannst keine neue Themen eröffnen
Du kannst keine Antworten verfassen
Du kannst keine Anhänge posten
Du kannst nicht deine Beiträge editieren
HTML-Code ist Aus.
|
|
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:09 Uhr.
().
|