JAVA-13615
GitHub Issue: The code for A* is in the src/test/java directory, not the src/main/java directory
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.algorithms.astar;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public class Graph<T extends GraphNode> {
|
||||
private final Set<T> nodes;
|
||||
|
||||
private final Map<String, Set<String>> connections;
|
||||
|
||||
public Graph(Set<T> nodes, Map<String, Set<String>> connections) {
|
||||
this.nodes = nodes;
|
||||
this.connections = connections;
|
||||
}
|
||||
|
||||
public T getNode(String id) {
|
||||
return nodes.stream()
|
||||
.filter(node -> node.getId().equals(id))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("No node found with ID"));
|
||||
}
|
||||
|
||||
public Set<T> getConnections(T node) {
|
||||
return connections.get(node.getId()).stream()
|
||||
.map(this::getNode)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.algorithms.astar;
|
||||
|
||||
public interface GraphNode {
|
||||
String getId();
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.algorithms.astar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class RouteFinder<T extends GraphNode> {
|
||||
private final Graph<T> graph;
|
||||
private final Scorer<T> nextNodeScorer;
|
||||
private final Scorer<T> targetScorer;
|
||||
|
||||
public RouteFinder(Graph<T> graph, Scorer<T> nextNodeScorer, Scorer<T> targetScorer) {
|
||||
this.graph = graph;
|
||||
this.nextNodeScorer = nextNodeScorer;
|
||||
this.targetScorer = targetScorer;
|
||||
}
|
||||
|
||||
public List<T> findRoute(T from, T to) {
|
||||
Map<T, RouteNode<T>> allNodes = new HashMap<>();
|
||||
Queue<RouteNode> openSet = new PriorityQueue<>();
|
||||
|
||||
RouteNode<T> start = new RouteNode<>(from, null, 0d, targetScorer.computeCost(from, to));
|
||||
allNodes.put(from, start);
|
||||
openSet.add(start);
|
||||
|
||||
while (!openSet.isEmpty()) {
|
||||
log.debug("Open Set contains: " + openSet.stream().map(RouteNode::getCurrent).collect(Collectors.toSet()));
|
||||
RouteNode<T> next = openSet.poll();
|
||||
log.debug("Looking at node: " + next);
|
||||
if (next.getCurrent().equals(to)) {
|
||||
log.debug("Found our destination!");
|
||||
|
||||
List<T> route = new ArrayList<>();
|
||||
RouteNode<T> current = next;
|
||||
do {
|
||||
route.add(0, current.getCurrent());
|
||||
current = allNodes.get(current.getPrevious());
|
||||
} while (current != null);
|
||||
|
||||
log.debug("Route: " + route);
|
||||
return route;
|
||||
}
|
||||
|
||||
graph.getConnections(next.getCurrent()).forEach(connection -> {
|
||||
double newScore = next.getRouteScore() + nextNodeScorer.computeCost(next.getCurrent(), connection);
|
||||
RouteNode<T> nextNode = allNodes.getOrDefault(connection, new RouteNode<>(connection));
|
||||
allNodes.put(connection, nextNode);
|
||||
|
||||
if (nextNode.getRouteScore() > newScore) {
|
||||
nextNode.setPrevious(next.getCurrent());
|
||||
nextNode.setRouteScore(newScore);
|
||||
nextNode.setEstimatedScore(newScore + targetScorer.computeCost(connection, to));
|
||||
openSet.add(nextNode);
|
||||
log.debug("Found a better route to node: " + nextNode);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
throw new IllegalStateException("No route found");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.baeldung.algorithms.astar;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
class RouteNode<T extends GraphNode> implements Comparable<RouteNode> {
|
||||
private final T current;
|
||||
private T previous;
|
||||
private double routeScore;
|
||||
private double estimatedScore;
|
||||
|
||||
RouteNode(T current) {
|
||||
this(current, null, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
|
||||
}
|
||||
|
||||
RouteNode(T current, T previous, double routeScore, double estimatedScore) {
|
||||
this.current = current;
|
||||
this.previous = previous;
|
||||
this.routeScore = routeScore;
|
||||
this.estimatedScore = estimatedScore;
|
||||
}
|
||||
|
||||
T getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
T getPrevious() {
|
||||
return previous;
|
||||
}
|
||||
|
||||
double getRouteScore() {
|
||||
return routeScore;
|
||||
}
|
||||
|
||||
double getEstimatedScore() {
|
||||
return estimatedScore;
|
||||
}
|
||||
|
||||
void setPrevious(T previous) {
|
||||
this.previous = previous;
|
||||
}
|
||||
|
||||
void setRouteScore(double routeScore) {
|
||||
this.routeScore = routeScore;
|
||||
}
|
||||
|
||||
void setEstimatedScore(double estimatedScore) {
|
||||
this.estimatedScore = estimatedScore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(RouteNode other) {
|
||||
if (this.estimatedScore > other.estimatedScore) {
|
||||
return 1;
|
||||
} else if (this.estimatedScore < other.estimatedScore) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", RouteNode.class.getSimpleName() + "[", "]").add("current=" + current)
|
||||
.add("previous=" + previous).add("routeScore=" + routeScore).add("estimatedScore=" + estimatedScore)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.algorithms.astar;
|
||||
|
||||
public interface Scorer<T extends GraphNode> {
|
||||
double computeCost(T from, T to);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.algorithms.astar.underground;
|
||||
|
||||
import com.baeldung.algorithms.astar.Scorer;
|
||||
|
||||
public class HaversineScorer implements Scorer<Station> {
|
||||
@Override
|
||||
public double computeCost(Station from, Station to) {
|
||||
double R = 6372.8; // In kilometers
|
||||
|
||||
double dLat = Math.toRadians(to.getLatitude() - from.getLatitude());
|
||||
double dLon = Math.toRadians(to.getLongitude() - from.getLongitude());
|
||||
double lat1 = Math.toRadians(from.getLatitude());
|
||||
double lat2 = Math.toRadians(to.getLatitude());
|
||||
|
||||
double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
|
||||
double c = 2 * Math.asin(Math.sqrt(a));
|
||||
return R * c;
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.algorithms.astar.underground;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.baeldung.algorithms.astar.GraphNode;
|
||||
|
||||
public class Station implements GraphNode {
|
||||
private final String id;
|
||||
private final String name;
|
||||
private final double latitude;
|
||||
private final double longitude;
|
||||
|
||||
public Station(String id, String name, double latitude, double longitude) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Station.class.getSimpleName() + "[", "]").add("id='" + id + "'")
|
||||
.add("name='" + name + "'").add("latitude=" + latitude).add("longitude=" + longitude).toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user