Commit df3aa567 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Updated Breadth-First-Search

parent f6bcb0d0
...@@ -30,6 +30,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.Road ...@@ -30,6 +30,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.Road
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm { public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm {
private final int MAX_DEPTH = 10;
@Override @Override
public RoadNetworkRoute findRoute(RoadNetwork pNetwork, RoadNetworkEdge pCurrentPosition, RoadNetworkEdge pDestination, public RoadNetworkRoute findRoute(RoadNetwork pNetwork, RoadNetworkEdge pCurrentPosition, RoadNetworkEdge pDestination,
...@@ -42,13 +43,17 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm { ...@@ -42,13 +43,17 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm {
List<RoadNetworkRoute> pKnownRoutes, List<RoadNetworkEdge> pEdgesToAvoid) { List<RoadNetworkRoute> pKnownRoutes, List<RoadNetworkEdge> pEdgesToAvoid) {
List<RoadNetworkEdge> visitedEdges = new ArrayList<>(); List<RoadNetworkEdge> visitedEdges = new ArrayList<>();
Queue<PathInformation> buffer = new LinkedList<>(); Queue<PathInformation> buffer = new LinkedList<>();
PathInformation rootPathInformation = new PathInformation(pCurrentPosition, null); PathInformation rootPathInformation = new PathInformation(pCurrentPosition, null, 0);
buffer.add(rootPathInformation); buffer.add(rootPathInformation);
visitedEdges.add(pCurrentPosition); visitedEdges.add(pCurrentPosition);
outer:while (buffer.size() > 0) { outer:while (buffer.size() > 0) {
PathInformation pathInformation = buffer.poll(); PathInformation pathInformation = buffer.poll();
if (pathInformation.getLength() >= MAX_DEPTH) {
continue;
}
RoadNetworkEdge edge = pathInformation.getEdge(); RoadNetworkEdge edge = pathInformation.getEdge();
if (pEdgesToAvoid.contains(edge)) { if (pEdgesToAvoid.contains(edge)) {
...@@ -60,7 +65,8 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm { ...@@ -60,7 +65,8 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm {
for (RoadNetworkEdge roadNetworkEdge : accessibleEdges) { for (RoadNetworkEdge roadNetworkEdge : accessibleEdges) {
if (roadNetworkEdge.equals(pDestination)) { if (roadNetworkEdge.equals(pDestination)) {
List<RoadNetworkEdge> routeList = new ArrayList<>(); List<RoadNetworkEdge> routeList = new ArrayList<>();
extractRoute(routeList, new PathInformation(roadNetworkEdge, pathInformation)); extractRoute(routeList,
new PathInformation(roadNetworkEdge, pathInformation, pathInformation.getLength() + 1));
RoadNetworkRoute route = new RoadNetworkRoute(routeList); RoadNetworkRoute route = new RoadNetworkRoute(routeList);
...@@ -74,7 +80,8 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm { ...@@ -74,7 +80,8 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm {
return route; return route;
} }
} else if (!visitedEdges.contains(roadNetworkEdge)) { } else if (!visitedEdges.contains(roadNetworkEdge)) {
PathInformation childPathInformation = new PathInformation(roadNetworkEdge, pathInformation); PathInformation childPathInformation = new PathInformation(roadNetworkEdge, pathInformation,
pathInformation.getLength() + 1);
buffer.add(childPathInformation); buffer.add(childPathInformation);
visitedEdges.add(roadNetworkEdge); visitedEdges.add(roadNetworkEdge);
} }
...@@ -91,13 +98,19 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm { ...@@ -91,13 +98,19 @@ public class BreadthFirstSearchRoutingAlgorithm implements RoutingAlgorithm {
} }
private class PathInformation { private class PathInformation {
private final int _length;
private final RoadNetworkEdge _edge; private final RoadNetworkEdge _edge;
private final PathInformation _previousEdge; private final PathInformation _previousEdge;
public PathInformation(RoadNetworkEdge pEdge, public PathInformation(RoadNetworkEdge pEdge,
PathInformation pPreviousEdge) { PathInformation pPreviousEdge, int pLenght) {
_edge = pEdge; _edge = pEdge;
_previousEdge = pPreviousEdge; _previousEdge = pPreviousEdge;
_length = pLenght;
}
public int getLength() {
return _length;
} }
public RoadNetworkEdge getEdge() { public RoadNetworkEdge getEdge() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment