Add a way to find a path from a location

This commit is contained in:
RODRIGUEZ lucas 2024-04-29 18:20:48 +02:00
parent 29e95072f3
commit d44b8496e1
7 changed files with 49 additions and 27 deletions

View file

@ -62,7 +62,7 @@
<manifest> <manifest>
<addClasspath>true</addClasspath> <addClasspath>true</addClasspath>
<classpathPrefix>${project.build.finalName}.lib/</classpathPrefix> <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
<mainClass>fr.u_paris.gla.project.App</mainClass> <mainClass>fr.u_paris.gla.project.itinerary.Parse</mainClass>
</manifest> </manifest>
</archive> </archive>
</configuration> </configuration>

View file

@ -18,6 +18,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
* Code of an extractor for the data from IDF mobilite. * Code of an extractor for the data from IDF mobilite.
* *

View file

@ -1,23 +1,43 @@
package fr.u_paris.gla.project.itinerary; package fr.u_paris.gla.project.itinerary;
import fr.u_paris.gla.project.utils.GPS;
import java.util.*; import java.util.*;
public class Finder { public class Finder {
private Graph graph; private Graph graph;
public Finder(Graph graph) { public Finder(Graph graph) {
this.graph = graph; this.graph = graph;
} }
public List<Path> 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<Path> res = findPath(fromNode, toNode, startTime);
return res;
}
/** /**
* return a path from startNode to goalNode using A* algorithm * return a path from startNode to goalNode using A* algorithm
* @param startNode * @param startNode
* @param goalNode * @param goalNode
*/ */
public List<Path> findPath(Stop startNode, Stop goalNode, double startTime) { public List<Path> findPath(Stop startNode, Stop goalNode, double startTime) {
PriorityQueue<Stop> openSet = new PriorityQueue<>(Comparator.comparingDouble(Stop::getF));
PriorityQueue<Stop> openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF));
HashSet<Stop> closedSet = new HashSet<>(); HashSet<Stop> closedSet = new HashSet<>();
HashMap<Stop, Path> cameFrom = new HashMap<>(); HashMap<Stop, Path> cameFrom = new HashMap<>();
HashMap<Stop, Double> gScore = new HashMap<>(); HashMap<Stop, Double> gScore = new HashMap<>();
@ -111,6 +131,5 @@ public class Finder {
node.setF(newF); node.setF(newF);
openSet.add(node); openSet.add(node);
} }
} }

View file

@ -1,6 +1,6 @@
package fr.u_paris.gla.project.itinerary; package fr.u_paris.gla.project.itinerary;
import java.util.HashMap; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -25,4 +25,25 @@ public class Graph{
public Map<Stop, Set<Connection>> getConnections() { public Map<Stop, Set<Connection>> getConnections() {
return connections; return connections;
} }
public void addNode(Stop s) {
nodes.add(s);
}
public void addConnection(Stop stop, Connection con) {
Set<Connection> currentConnections = connections.get(stop);
if (currentConnections == null) {
HashSet<Connection> 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);
}
} }

View file

@ -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<Stop> getNeighbors();
double getCost(Stop neighbor);
double getF();
void setF(double value);
}

View file

@ -42,7 +42,7 @@ public class Parse {
private static final int STOP_TIME = 30; private static final int STOP_TIME = 30;
//Walking speed in m/s //Walking speed in m/s
private static final double WALK_SPEED = 1.; public static final double WALK_SPEED = 1.;
private HashSet<Stop> nodes = new HashSet<>(); private HashSet<Stop> nodes = new HashSet<>();
private HashMap<Stop, Set<Connection>> connections = new HashMap<>(); private HashMap<Stop, Set<Connection>> connections = new HashMap<>();

View file

@ -3,7 +3,7 @@ package fr.u_paris.gla.project.itinerary;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class Stop implements GraphNode { public class Stop {
// The total number of stops // The total number of stops
private static int counter = 0; private static int counter = 0;
@ -41,27 +41,22 @@ public class Stop implements GraphNode {
'}'; '}';
} }
@Override
public int getId(){ public int getId(){
return id; return id;
} }
@Override
public double getHeuristicCost(Stop goalNode) { public double getHeuristicCost(Stop goalNode) {
return 0; return 0;
} }
@Override
public Set<Stop> getNeighbors() { public Set<Stop> getNeighbors() {
return null; return null;
} }
@Override
public double getCost(Stop neighbor) { public double getCost(Stop neighbor) {
return 0; return 0;
} }
@Override
public double getF() { public double getF() {
return f; return f;
} }