Results 1 to 4 of 4
Thread: Need Help with A star
- 10-09-2009, 09:18 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
Need Help with A star
Hello!
I would like some help with a program I am developing at the moment. The objective is to implement an A star algorithm. I got this parcialy done on the web and I tried to do the missing parts.
The problem is that is not working as it should. What I have done was done in the AStarState class and Location class.
Help please :confused:
In the Location class done this:
And in the AStarState:Java Code:public boolean equals(Location loc) { return (this.xCoord==loc.xCoord && this.yCoord==loc.yCoord); } public int hashcode() { return (this.xCoord*2+this.yCoord*2); }
You can get all the other classes in here:Java Code:package astar_demo1; import java.util.HashMap; import java.util.Iterator; /** * This class stores the basic state necessary for the A* algorithm to compute a * path across a map. This state includes a collection of "open waypoints" and * another collection of "closed waypoints." In addition, this class provides * the basic operations that the A* pathfinding algorithm needs to perform its * processing. **/ public class AStarState { /** This is a reference to the map that the A* algorithm is navigating. **/ private Map2D map; public HashMap open = new HashMap(); public HashMap close = new HashMap(); /** * Initialize a new state object for the A* pathfinding algorithm to use. **/ public AStarState(Map2D map) { if (map == null) throw new NullPointerException("map cannot be null"); this.map = map; } /** Returns the map that the A* pathfinder is navigating. **/ public Map2D getMap() { return map; } /** * This method scans through all open waypoints, and returns the waypoint * with the minimum total cost. If there are no open waypoints, this method * returns <code>null</code>. **/ public Waypoint getMinOpenWaypoint() { if(open.isEmpty()) return null; else{ // TODO: Implement. Iterator i = open.keySet().iterator(); //int x=1; //float min= 0; Waypoint aux,aux2=(Waypoint) open.get(i.next()); while(i.hasNext()) { aux = (Waypoint) open.get(i.next()); //if (x==1) //aux2=aux; //else if(aux.getTotalCost()<aux2.getTotalCost()) aux2=aux; } return aux2; } } /** * This method adds a waypoint to (or potentially updates a waypoint already * in) the "open waypoints" collection. If there is not already an open * waypoint at the new waypoint's location then the new waypoint is simply * added to the collection. However, if there is already a waypoint at the * new waypoint's location, the new waypoint replaces the old one <em>only * if</em> the new waypoint's "previous cost" value is less than the current * waypoint's "previous cost" value. **/ public boolean addOpenWaypoint(Waypoint newWP) { // TODO: Implement. Location local = newWP.getLocation(); if(!open.containsKey(local)) { open.put(local,newWP);} else{ Waypoint aux = (Waypoint) open.get(local); if(newWP.getPreviousCost()< aux.getPreviousCost()){ open.put(local, newWP); return true; } return false; } return false; } /** Returns the current number of open waypoints. **/ public int numOpenWaypoints() { // TODO: Implement. return open.size(); } /** * This method moves the waypoint at the specified location from the * open list to the closed list. **/ public void closeWaypoint(Location loc) { // TODO: Implement. if (open.containsKey(loc)){ close.put(loc,open.get(loc)); open.remove(loc); } } /** * Returns true if the collection of closed waypoints contains a waypoint * for the specified location. **/ public boolean isLocationClosed(Location loc) { // TODO: Implement. if(close.containsKey(loc)) return true; else return false; } }XML Code:http://feupload.fe.up.pt/get/GyAu6ItNASi5Mno
- 10-10-2009, 10:36 AM #2
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
The problem is in these last functions...the functions that work with the waypoints...
Is it ok to do cast of an iterator??
- 10-11-2009, 02:04 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
The problem was at the Location class in the equals() and hashcode() functions.
- 10-11-2009, 02:50 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 54
- Rep Power
- 0
Similar Threads
-
Star pattern help?
By GeeKunMow in forum New To JavaReplies: 13Last Post: 07-21-2011, 05:59 AM -
star icon
By benkyma in forum EclipseReplies: 1Last Post: 03-18-2009, 05:24 PM -
star array
By nanna in forum New To JavaReplies: 4Last Post: 11-23-2008, 07:58 AM -
creating a Star
By jhen in forum New To JavaReplies: 2Last Post: 11-19-2008, 03:41 AM -
Replace number to the star
By linux0kernel in forum New To JavaReplies: 5Last Post: 10-15-2008, 09:39 AM


LinkBack URL
About LinkBacks

Bookmarks