From d44b8496e1b76631356e0c0d64a788e153f9be13 Mon Sep 17 00:00:00 2001 From: RODRIGUEZ lucas Date: Mon, 29 Apr 2024 18:20:48 +0200 Subject: [PATCH] Add a way to find a path from a location --- pom.xml | 2 +- .../project/idfm/IDFMNetworkExtractor.java | 1 + .../u_paris/gla/project/itinerary/Finder.java | 27 ++++++++++++++++--- .../u_paris/gla/project/itinerary/Graph.java | 23 +++++++++++++++- .../gla/project/itinerary/GraphNode.java | 14 ---------- .../u_paris/gla/project/itinerary/Parse.java | 2 +- .../u_paris/gla/project/itinerary/Stop.java | 7 +---- 7 files changed, 49 insertions(+), 27 deletions(-) delete mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java diff --git a/pom.xml b/pom.xml index ffe1949..ad0ad37 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ true ${project.build.finalName}.lib/ - fr.u_paris.gla.project.App + fr.u_paris.gla.project.itinerary.Parse diff --git a/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java b/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java index c3add40..263e9fd 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java @@ -18,6 +18,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; + /** * Code of an extractor for the data from IDF mobilite. * diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java index bef9d63..31fe948 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java @@ -1,23 +1,43 @@ package fr.u_paris.gla.project.itinerary; +import fr.u_paris.gla.project.utils.GPS; + import java.util.*; public class Finder { - private Graph graph; + public Finder(Graph graph) { this.graph = graph; } + public List findPath(double from_x, double from_y, double to_x, double to_y, double startTime) { + Stop fromNode = new Stop("", "tmp_from", from_x, from_y); + Stop toNode = new Stop("", "tmp_to", to_x, to_y); + + for (Stop node : graph.getNodes()) { + double from_dst = GPS.distance(from_x, from_y, node.getLatitude(), node.getLongitude()); + double to_dst = GPS.distance(to_x, to_y, node.getLatitude(), node.getLongitude()); + Connection from_c = new Connection(node, "", from_dst, (int) ((from_dst * 1000) / Parse.WALK_SPEED)); + Connection to_c = new Connection(toNode, "", to_dst, (int) ((to_dst * 1000) / Parse.WALK_SPEED)); + graph.addConnection(fromNode, from_c); + graph.addConnection(node, to_c); + } + graph.addNode(fromNode); + graph.addNode(toNode); + + List res = findPath(fromNode, toNode, startTime); + return res; + } + /** * return a path from startNode to goalNode using A* algorithm * @param startNode * @param goalNode */ public List findPath(Stop startNode, Stop goalNode, double startTime) { - - PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF)); + PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(Stop::getF)); HashSet closedSet = new HashSet<>(); HashMap cameFrom = new HashMap<>(); HashMap gScore = new HashMap<>(); @@ -111,6 +131,5 @@ public class Finder { node.setF(newF); openSet.add(node); } - } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java index 795add9..2fdfa21 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java @@ -1,6 +1,6 @@ package fr.u_paris.gla.project.itinerary; -import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -25,4 +25,25 @@ public class Graph{ public Map> getConnections() { return connections; } + + public void addNode(Stop s) { + nodes.add(s); + } + + public void addConnection(Stop stop, Connection con) { + Set currentConnections = connections.get(stop); + if (currentConnections == null) { + HashSet set = new HashSet<>(); + set.add(con); + connections.put(stop, set); + } + else { + currentConnections.add(con); + } + } + + public void removeNode(Stop s) { + nodes.remove(s); + connections.remove(s); + } } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java deleted file mode 100644 index 175db6e..0000000 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ /dev/null @@ -1,14 +0,0 @@ -package fr.u_paris.gla.project.itinerary; - -import java.util.List; -import java.util.Set; - -public interface GraphNode { - int getId(); - double getHeuristicCost(Stop goalNode); - - Set getNeighbors(); - double getCost(Stop neighbor); - double getF(); - void setF(double value); -} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java b/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java index 5f97384..1fa083f 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java @@ -42,7 +42,7 @@ public class Parse { private static final int STOP_TIME = 30; //Walking speed in m/s - private static final double WALK_SPEED = 1.; + public static final double WALK_SPEED = 1.; private HashSet nodes = new HashSet<>(); private HashMap> connections = new HashMap<>(); diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java index 6d9169c..bae0289 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java @@ -3,7 +3,7 @@ package fr.u_paris.gla.project.itinerary; import java.util.HashSet; import java.util.Set; -public class Stop implements GraphNode { +public class Stop { // The total number of stops private static int counter = 0; @@ -41,27 +41,22 @@ public class Stop implements GraphNode { '}'; } - @Override public int getId(){ return id; } - @Override public double getHeuristicCost(Stop goalNode) { return 0; } - @Override public Set getNeighbors() { return null; } - @Override public double getCost(Stop neighbor) { return 0; } - @Override public double getF() { return f; }