myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   A* Java problem (https://mygully.com/showthread.php?t=2523198)

PunchyDEADBEEF 18.12.11 00:24

A* Java problem
 
Hi, hat irgendwer eine Ahnung wieso dieser A* nie closest.equals(end) validiert?
PHP-Code:

  private ArrayList<Pointopen;
    private 
ArrayList<Pointclosed;
    private 
HashMap<PointPointparents;
    private 
HashMap<Point,IntegertotalCost;
    
    public 
PathFinder() {
        
this.open = new ArrayList<Point>();
        
this.closed = new ArrayList<Point>();
        
this.parents = new HashMap<PointPoint>();
        
this.totalCost = new HashMap<PointInteger>();
    }
    
    private 
int heuristicCost(Point fromPoint to) {
        return 
Math.max(Math.abs(from.to.x), Math.abs(from.to.y));
    }

    private 
int costToGetTo(Point from) {
        return 
parents.get(from) == null : (costToGetTo(parents.get(from)));
    }
    
    private 
int totalCost(Point fromPoint to) {
        if (
totalCost.containsKey(from))
            return 
totalCost.get(from);
        
        
int cost costToGetTo(from) + heuristicCost(fromto);
        
totalCost.put(fromcost);
        return 
cost;
    }

    private 
void reParent(Point childPoint parent){
        
parents.put(childparent);
        
totalCost.remove(child);
    }

    public 
ArrayList<PointfindPath(Creature creaturePoint startPoint endint maxTries) {
        
open.clear();
        
closed.clear();
        
parents.clear();
        
totalCost.clear();
        
        
open.add(start);
        
        for (
int tries 0tries maxTries && open.size() > 0tries++){
            
Point closest getClosestPoint(end);
            
            
open.remove(closest);
            
closed.add(closest);

            if (
closest.equals(end))
                return 
createPath(startclosest);
            else
                
checkNeighbors(creatureendclosest);
        }
        return 
null;
    }

    private 
Point getClosestPoint(Point end) {
        
Point closest open.get(0);
        for (
Point other open){
            if (
totalCost(otherend) < totalCost(closestend))
                
closest other;
        }
        return 
closest;
    }

    private 
void checkNeighbors(Creature creaturePoint endPoint 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(closestneighbor);
            else
                
reParentNeighbor(closestneighbor);
        }
    }

    private 
void reParentNeighbor(Point closestPoint neighbor) {
        
reParent(neighborclosest);
        
open.add(neighbor);
    }

    private 
void reParentNeighborIfNecessary(Point closestPoint neighbor) {
        
Point originalParent parents.get(neighbor);
        
double currentCost costToGetTo(neighbor);
        
reParent(neighborclosest);
        
double reparentCost costToGetTo(neighbor);
        
        if (
reparentCost currentCost)
            
open.remove(neighbor);
        else
            
reParent(neighbororiginalParent);
    }

    private 
ArrayList<PointcreatePath(Point startPoint end) {
        
ArrayList<Pointpath = new ArrayList<Point>();

        while (!
end.equals(start)) {
            
path.add(end);
            
end parents.get(end);
        }

        
Collections.reverse(path);
        return 
path;
    } 


Your_Conscience 18.12.11 16:32

Wahrscheinlich, weil er nicht zum Endpunkt kommt.
Lass dir mal während er Rechnet die Variablen anzeigen, dann kann man den Fehler besser finden.

PunchyDEADBEEF 18.12.11 17:06

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

char_ 01.01.12 16:17

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 :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:19 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.