From 129341c4600e088190cf3d1523e93ec5e9fb9abc Mon Sep 17 00:00:00 2001 From: Victor Zamanian Date: Fri, 7 Nov 2008 13:00:00 +0100 Subject: [PATCH] Have to go, leaving work to Anton the man. --- src/GraphNode.java | 2 +- src/MySearcher.java | 61 +++++++++++++++++++++++++++++------------------------ src/Road.java | 2 +- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/GraphNode.java b/src/GraphNode.java index a936609..2042948 100644 --- a/src/GraphNode.java +++ b/src/GraphNode.java @@ -18,7 +18,7 @@ public class GraphNode implements Comparable { private int y; private Double distanceToGoal; private boolean visited; - private Double distanceTraveled; + private Double distanceTraveled = Double.MAX_VALUE; private GraphNode parent; private Double evalFuncVal; diff --git a/src/MySearcher.java b/src/MySearcher.java index ea4fa5c..866b09f 100644 --- a/src/MySearcher.java +++ b/src/MySearcher.java @@ -15,13 +15,16 @@ import org.jdom.*; * @version 1.0 */ public class MySearcher extends MapSearcher { + private Graph map; + private int topSpeed; /** * Skapar en ny MySearcher-instans. */ public MySearcher () { super (); + topSpeed = 0; } /** @@ -69,11 +72,15 @@ public class MySearcher extends MapSearcher { Road road = new Road(Integer.parseInt(roadElement.getAttributeValue("speed")), distance); + if (road.getSpeed() > topSpeed) { + topSpeed = road.getSpeed(); + } map.insertEdge(cityNode.getName(), nodeAtEndOfRoad.getName(), road); } } + System.out.println("\n\n\nTOPSPEED ÄR : " + topSpeed + "\n\n\n"); } catch (IOException e) { System.err.println ("Could not read/find file."); @@ -124,7 +131,7 @@ public class MySearcher extends MapSearcher { } pathToGoal = n.getName() + ", " + pathToGoal; // Beginning System.out.println("\nAll expanded nodes:\n" + expandedNodes + "\n\n"); - return "Path to goal:\n" + pathToGoal + "\n"; + return pathToGoal; } // 5. Otherwise add the children of n to N, GraphNode neighbour; @@ -181,36 +188,36 @@ public class MySearcher extends MapSearcher { } pathToGoal = n.getName() + ", " + pathToGoal; // Beginning System.out.println("\nAll expanded nodes:\n" + expandedNodes + "\n\n"); - return "Path to goal:\n" + pathToGoal + "\n"; + return pathToGoal; } // 5. Otherwise add the children of n to N, GraphNode neighbour; for (Enumeration e = n.getNeighbours(); e.hasMoreElements();) { - neighbour = e.nextElement(); - if (fastest) { - neighbour.setDistanceTraveled(n.getDistanceTraveled() - + ((Road) n.getEdge(neighbour)).getTravelTime()); - neighbour.setDistanceToGoal(map.getNode(to)); - neighbour.setEvalFuncVal(neighbour.getDistanceTraveled() - + neighbour.getDistanceToGoal() / (110 / 3.6)); - } - else { - neighbour.setDistanceTraveled(n.getDistanceTraveled() - + ((Road) n.getEdge(neighbour)).getDistance()); - neighbour.setDistanceToGoal(map.getNode(to)); - neighbour.setEvalFuncVal(neighbour.getDistanceTraveled() + - neighbour.getDistanceToGoal()); - } + neighbour = e.nextElement(); - // sort the nodes in N according to the value on their - // evaluation function - //else if (!queue.contains(neighbour)) {//&& !neighbour.isVisited()) { - if (!neighbour.isVisited()) { - neighbour.setParent(n); - queue.add(neighbour); - // and return to step 2. (end of loop) - } + if (!neighbour.isVisited()) { + Double temp = n.getDistanceTraveled() + + ((Road) n.getEdge(neighbour)).getTravelTime(); + if (fastest && temp < neighbour.getDistanceTraveled()) { + neighbour.setDistanceTraveled(n.getDistanceTraveled() + + ((Road) n.getEdge(neighbour)).getTravelTime()); + neighbour.setDistanceToGoal(map.getNode(to)); + neighbour.setEvalFuncVal(neighbour.getDistanceTraveled() + + neighbour.getDistanceToGoal() / (topSpeed /* 3.6*/)); + queue.add(neighbour); + neighbour.setParent(n); + } + else if (temp < neighbour.getDistanceTraveled()) { + neighbour.setDistanceTraveled(n.getDistanceTraveled() + + ((Road) n.getEdge(neighbour)).getDistance()); + neighbour.setDistanceToGoal(map.getNode(to)); + neighbour.setEvalFuncVal(neighbour.getDistanceTraveled() + + neighbour.getDistanceToGoal()); + queue.add(neighbour); + neighbour.setParent(n); + } + } } } System.out.println(); @@ -255,7 +262,7 @@ public class MySearcher extends MapSearcher { } pathToGoal = n.getName() + ", " + pathToGoal; // Beginning System.out.println("\nAll expanded nodes:\n" + expandedNodes + "\n\n"); - return "Path to goal:\n" + pathToGoal + "\n"; + return pathToGoal; } // * Otherwise push all the (so-far-unexamined) successors // * (the direct child nodes) of this node into the end of @@ -305,7 +312,7 @@ public class MySearcher extends MapSearcher { } pathToGoal = n.getName() + ", " + pathToGoal; // Beginning System.out.println("\nAll expanded nodes:\n" + expandedNodes + "\n\n"); - return "Path to goal:\n" + pathToGoal + "\n"; + return pathToGoal; } // * Otherwise push all the unvisited connecting nodes of n else { diff --git a/src/Road.java b/src/Road.java index ec9352a..12eacc8 100644 --- a/src/Road.java +++ b/src/Road.java @@ -18,7 +18,7 @@ public class Road { public Road(int speed, double distance) { this.speed = speed; this.distance = distance; - this.travelTime = distance / (speed / 3.6); + this.travelTime = distance / (speed /* 3.6*/); } /** -- 2.11.4.GIT