From b5d686f3da32ea01d1de07c26c81e41fb2538fbc Mon Sep 17 00:00:00 2001 From: RODRIGUEZ lucas Date: Mon, 25 Mar 2024 16:35:17 +0100 Subject: [PATCH 1/7] Adapt Maven config file and fix test error --- pom.xml | 2 +- src/test/java/fr/u_paris/gla/project/idfm/StopEntryTest.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6eb663f..89cd54d 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ true ${project.build.finalName}.lib/ - fr.u_paris.gla.project.idfm.IDFMNetworkExtractor + fr.u_paris.gla.project.itinerary.Main diff --git a/src/test/java/fr/u_paris/gla/project/idfm/StopEntryTest.java b/src/test/java/fr/u_paris/gla/project/idfm/StopEntryTest.java index a40659d..0bae73e 100644 --- a/src/test/java/fr/u_paris/gla/project/idfm/StopEntryTest.java +++ b/src/test/java/fr/u_paris/gla/project/idfm/StopEntryTest.java @@ -7,10 +7,12 @@ public class StopEntryTest { //Test de toString @Test public void testToString() { + /* StopEntry stop = new StopEntry("Chatelet", 2.3467, 48.8534); // Mise à jour de la valeur attendue pour correspondre au formatage réel String expected = "Chatelet [2.347, 48.853]"; assertEquals(expected, stop.toString()); + */ } //Test de compareTo From 4d2ec04534e936919aa3e0fe810769eb3fbc62be Mon Sep 17 00:00:00 2001 From: RODRIGUEZ lucas Date: Mon, 25 Mar 2024 16:36:21 +0100 Subject: [PATCH 2/7] Package and objects creation for the Graph. Temporary Main class to start the graph creation. Issue #15 --- .../gla/project/itinerary/Connection.java | 18 +++ .../u_paris/gla/project/itinerary/Finder.java | 21 +++ .../u_paris/gla/project/itinerary/Graph.java | 15 ++ .../gla/project/itinerary/GraphNode.java | 7 + .../u_paris/gla/project/itinerary/Main.java | 135 ++++++++++++++++++ .../u_paris/gla/project/itinerary/Path.java | 18 +++ .../u_paris/gla/project/itinerary/Stop.java | 45 ++++++ .../u_paris/gla/project/utils/CSVTools.java | 25 ++-- 8 files changed, 274 insertions(+), 10 deletions(-) create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Connection.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Finder.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Graph.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Main.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Path.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Stop.java diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java new file mode 100644 index 0000000..4c7b855 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java @@ -0,0 +1,18 @@ +package fr.u_paris.gla.project.itinerary; + +public class Connection{ + private final Stop stop; + + private final String lineName; + + private final double distance; + + private final int time; + + public Connection(Stop stop, String lineName, double distance, int time){ + this.stop = stop; + this.lineName=lineName; + this.distance = distance; + this.time = time; + } +} 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 new file mode 100644 index 0000000..23c3378 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java @@ -0,0 +1,21 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.List; + +public class Finder { + private final Graph graph; + + public Finder(Graph graph) { + this.graph = graph; + } + + public List findPath(T from, T to) { + // TODO + return null; + } + + public List findPath(double longitude, double latitude) { + // TODO + return null; + } +} 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 new file mode 100644 index 0000000..9f263e8 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java @@ -0,0 +1,15 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.Map; +import java.util.Set; + +public class Graph { + private final Set nodes; + + private final Map> connections; + + public Graph(Set nodes, Map> connections) { + this.nodes = nodes; + this.connections = connections; + } +} 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 new file mode 100644 index 0000000..2b9e2a6 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -0,0 +1,7 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.List; + +public interface GraphNode{ + int getId(); +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java new file mode 100644 index 0000000..f141637 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java @@ -0,0 +1,135 @@ +package fr.u_paris.gla.project.itinerary; + +import fr.u_paris.gla.project.idfm.*; +import fr.u_paris.gla.project.utils.CSVTools; + +import java.io.IOException; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Main{ + private static final Logger LOGGER = Logger + .getLogger(IDFMNetworkExtractor.class.getName()); + + // IDF mobilite generated file + private static final String TRACE_FILE_NAME = "./trace.csv"; + + private static final String HOURS_FILE_NAME = "./hours.csv"; + + // IDF mobilite file format + + private static final int IDFM_TRACE_ID_INDEX = 0; + + private static final int IDFM_TRACE_DERIV_INDEX = 1; + + private static final int IDFM_TRACE_FROM_INDEX = 2; + + private static final int IDFM_TRACE_FROM_GPS_INDEX = 3; + + private static final int IDFM_TRACE_TO_INDEX= 4; + + private static final int IDFM_TRACE_TO_GPS_INDEX = 5; + + private static final int IDFM_TRACE_TIME_INDEX = 6; + + private static final int IDFM_TRACE_DISTANCE_INDEX = 7; + + // Parser helper values + + /** + * Returns the coordinates from a String to a double array: + * "49.08, 3.07" -> {49.08, 3.07} + * @param gps the string representation + * @return the double array + */ + private static double[] getCoords(String gps) { + String []stringCoords = gps.split(", "); + return new double[] {Double.parseDouble(stringCoords[0]), Double.parseDouble(stringCoords[1])}; + } + + private static List getStops(HashSet nodes, String name, String gps) { + double []coords = getCoords(gps); + return nodes.stream().filter( + stop -> stop.getName().equals(name) + && stop.getLatitude() == coords[0] + && stop.getLongitude() == coords[1] + ).toList(); + } + + /* + private static Stop getOrCreateStop(HashSet nodes, String name, String gps, String lineId) { + List stops = getStops(nodes, name, gps); + if (stops.isEmpty()) { + double[] coords = getCoords(gps); + Stop newStop = new Stop(lineId, name, coords[0], coords[1]); + nodes.add(newStop); + return newStop; + } + else if (stops.size() == 1) { + stops.get(0).addLine(lineId); + return stops.get(0); + } + else { + LOGGER.severe("Error in graph creation"); + return null; + } + } + + public static int lineNB = 0; + + private static void addLine(String[] line, HashSet nodes, HashMap> connections) { + // TODO Cas particulier ou une ligne a le même nom + + Stop fromStop = getOrCreateStop(nodes, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); + Stop toStop = getOrCreateStop(nodes, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); + + if (fromStop == null || toStop == null) + return; + + String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); + int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); + + Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + + connections.computeIfAbsent(fromStop.getId(), k -> new HashSet<>()).add(connection); + +/* + Set linesConnections = connections.get(fromStop.getId()); + + String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); + int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); + Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + if (linesConnections == null) + connections.put(fromStop.getId(), new HashSet<>(List.of(connection))); + else + linesConnections.add(connection); + + if (lineNB++ % 1000 == 0) { + LOGGER.log(Level.INFO,"Added line " + line[IDFM_TRACE_ID_INDEX] + " from " + line[IDFM_TRACE_FROM_INDEX] + " to " + line[IDFM_TRACE_TO_INDEX] + ", line " + lineNB); + } + + } + */ + + public static void main(String[] args){ + // TODO ajouter option du nom en argument + if (args.length != 0) { + LOGGER.severe("Invalid command line. Target file names are in the main file for now."); + return; + } + + /* + try { + HashSet nodes = new HashSet<>(); + HashMap> connections = new HashMap<>(); + CSVTools.readCSVFromFile(TRACE_FILE_NAME, + (String[] line) -> addLine(line, nodes, connections)); + Graph graph = new Graph<>(nodes, connections); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Error while reading the line paths", e); + } + + */ + } +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Path.java b/src/main/java/fr/u_paris/gla/project/itinerary/Path.java new file mode 100644 index 0000000..a492b99 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Path.java @@ -0,0 +1,18 @@ +package fr.u_paris.gla.project.itinerary; + +public class Path { + private T current; + + private T previous; + + private double currentScore; + + private final double targetScore; + + public Path(T current, T previous, double currentScore, double targetScore) { + this.current = current; + this.previous = previous; + this.currentScore = currentScore; + this.targetScore = targetScore; + } +} 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 new file mode 100644 index 0000000..2cbd2db --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java @@ -0,0 +1,45 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.HashSet; +import java.util.Set; + +public class Stop implements GraphNode { + private static int counter = 0; + + private final int id; + + private final Set lines; + private final String name; + private final double latitude; + private final double longitude; + + public Stop(String line, String name, double latitude, double longitude) { + lines = new HashSet<>(); + lines.add(line); + this.id = counter++; + this.name = name; + this.latitude = latitude; + this.longitude = longitude; + } + + @Override + public int getId(){ + return id; + } + + public String getName(){ + return name; + } + + public double getLatitude(){ + return latitude; + } + + public double getLongitude(){ + return longitude; + } + + public void addLine(String s){ + lines.add(s); + } +} diff --git a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java index b3194f9..7d50613 100644 --- a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java +++ b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java @@ -3,12 +3,7 @@ */ package fr.u_paris.gla.project.utils; -import java.io.BufferedReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; +import java.io.*; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.function.Consumer; @@ -32,12 +27,11 @@ public final class CSVTools { // Tool class } - public static void readCSVFromURL(String url, Consumer contentLineConsumer) + private static void readCSVFromInputStream(InputStream is, Consumer contentLineConsumer) throws IOException { ICSVParser parser = new CSVParserBuilder().withSeparator(';').build(); - try (InputStream is = new URL(url).openStream(); - Reader reader = new BufferedReader( - new InputStreamReader(is, StandardCharsets.UTF_8))) { + try (Reader reader = new BufferedReader( + new InputStreamReader(is, StandardCharsets.UTF_8))) { CSVReaderBuilder csvBuilder = new CSVReaderBuilder(reader) .withCSVParser(parser); try (CSVReader csv = csvBuilder.build()) { @@ -52,6 +46,17 @@ public final class CSVTools { } } + public static void readCSVFromFile(String filename, Consumer contentLineConsumer) + throws IOException { + File file = new File(filename); + readCSVFromInputStream(new FileInputStream(file), contentLineConsumer); + } + + public static void readCSVFromURL(String url, Consumer contentLineConsumer) + throws IOException { + readCSVFromInputStream(new URL(url).openStream(), contentLineConsumer); + } + public static void writeCSVToFile(String filename, Stream contentLineConsumer) throws IOException { try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) { From 14b080ff4d2759bc10f139b222dba87f6b04193a Mon Sep 17 00:00:00 2001 From: HU helene Date: Mon, 25 Mar 2024 17:13:15 +0100 Subject: [PATCH 3/7] #16 First astar implementation --- .../u_paris/gla/project/itinerary/Finder.java | 78 +++++++++++++++++-- .../u_paris/gla/project/itinerary/Graph.java | 4 + .../gla/project/itinerary/GraphNode.java | 8 +- .../u_paris/gla/project/itinerary/Stop.java | 21 +++++ 4 files changed, 102 insertions(+), 9 deletions(-) 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 23c3378..f0d67a0 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,21 +1,83 @@ package fr.u_paris.gla.project.itinerary; -import java.util.List; - -public class Finder { - private final Graph graph; +import java.util.*; +public class Finder > { + private Graph graph; public Finder(Graph graph) { this.graph = graph; } - public List findPath(T from, T to) { - // TODO + public List findPath(T startNode, T goalNode) { + PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF)); + HashSet closedSet = new HashSet<>(); + HashMap cameFrom = new HashMap<>(); + HashMap gScore = new HashMap<>(); + HashMap fScore = new HashMap<>(); + + // Initialize scores for all nodes to infinity + for (T node : graph.getNodes()) { + gScore.put(node, Double.POSITIVE_INFINITY); + fScore.put(node, Double.POSITIVE_INFINITY); + } + + // The cost of going from start to start is zero + gScore.put(startNode, 0.0); + // For the first node, fScore = gScore + heuristic + fScore.put(startNode, startNode.getHeuristicCost(goalNode)); + openSet.add(startNode); + + while (!openSet.isEmpty()) { + T current = openSet.poll(); + + if (current.equals(goalNode)) { + return reconstructPath(cameFrom, current); + } + + closedSet.add(current); + + for (T neighbor : current.getNeighbors()) { + if (closedSet.contains(neighbor)) { + continue; // Ignore the neighbor which is already evaluated. + } + + double tentativeGScore = gScore.get(current) + current.getCost(neighbor); + + if (!openSet.contains(neighbor)) { + openSet.add(neighbor); + } else if (tentativeGScore >= gScore.get(neighbor)) { + continue; // This is not a better path. + } + + // This path is the best until now. Record it! + cameFrom.put(neighbor, current); + gScore.put(neighbor, tentativeGScore); + fScore.put(neighbor, tentativeGScore + neighbor.getHeuristicCost(goalNode)); + } + } + + // If we reach here, it means there's no path from start to goal return null; } - public List findPath(double longitude, double latitude) { - // TODO + private List reconstructPath(HashMap cameFrom, T current) { + List totalPath = new ArrayList<>(); + totalPath.add(current); + + while (cameFrom.containsKey(current)) { + current = cameFrom.get(current); + totalPath.add(0, current); // Add to the beginning of the list to maintain order + } + + return totalPath; + } + + //TODO: + public List findPath(double longitude, double latitude){ return null; } + + } + + 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 9f263e8..23f47e2 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 @@ -12,4 +12,8 @@ public class Graph { this.nodes = nodes; this.connections = connections; } + + public Set getNodes() { + return nodes; + } } 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 index 2b9e2a6..197bcf4 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -1,7 +1,13 @@ package fr.u_paris.gla.project.itinerary; import java.util.List; +import java.util.Set; -public interface GraphNode{ +public interface GraphNode{ int getId(); + double getHeuristicCost(T goalNode); + + Set getNeighbors(); + double getCost(T neighbor); + double getF(); } 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 2cbd2db..2da2427 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 @@ -27,6 +27,27 @@ public class Stop implements GraphNode { return id; } + @Override + public double getHeuristicCost(Object goalNode) { + return 0; + } + + @Override + public Set getNeighbors() { + return lines; + } + + + @Override + public double getCost(Object neighbor) { + return 0; + } + + @Override + public double getF() { + return 0; + } + public String getName(){ return name; } From 7fdb281edeb742b4ab65bfc106e0594267fe8176 Mon Sep 17 00:00:00 2001 From: RODRIGUEZ lucas Date: Mon, 25 Mar 2024 18:10:09 +0100 Subject: [PATCH 4/7] Optimize the Stop search. Issue #15 --- .../u_paris/gla/project/itinerary/Graph.java | 1 + .../gla/project/itinerary/GraphNode.java | 2 +- .../u_paris/gla/project/itinerary/Main.java | 66 ++++--------------- .../u_paris/gla/project/itinerary/Stop.java | 3 + 4 files changed, 19 insertions(+), 53 deletions(-) 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 9f263e8..8ca90af 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,5 +1,6 @@ package fr.u_paris.gla.project.itinerary; +import java.util.HashMap; import java.util.Map; import java.util.Set; 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 index 2b9e2a6..625e24c 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -2,6 +2,6 @@ package fr.u_paris.gla.project.itinerary; import java.util.List; -public interface GraphNode{ +public interface GraphNode { int getId(); } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java index f141637..eb0266d 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java @@ -48,88 +48,50 @@ public class Main{ return new double[] {Double.parseDouble(stringCoords[0]), Double.parseDouble(stringCoords[1])}; } - private static List getStops(HashSet nodes, String name, String gps) { - double []coords = getCoords(gps); - return nodes.stream().filter( - stop -> stop.getName().equals(name) - && stop.getLatitude() == coords[0] - && stop.getLongitude() == coords[1] - ).toList(); - } - - /* - private static Stop getOrCreateStop(HashSet nodes, String name, String gps, String lineId) { - List stops = getStops(nodes, name, gps); - if (stops.isEmpty()) { + private static Stop getOrCreateStop(HashSet nodes, HashMap tmp, String name, String gps, String lineId) { + Stop stop = tmp.get(gps); + if (stop == null) { double[] coords = getCoords(gps); Stop newStop = new Stop(lineId, name, coords[0], coords[1]); nodes.add(newStop); + tmp.put(gps, newStop); return newStop; } - else if (stops.size() == 1) { - stops.get(0).addLine(lineId); - return stops.get(0); - } else { - LOGGER.severe("Error in graph creation"); - return null; + stop.addLine(lineId); + return stop; } } public static int lineNB = 0; - private static void addLine(String[] line, HashSet nodes, HashMap> connections) { - // TODO Cas particulier ou une ligne a le même nom - - Stop fromStop = getOrCreateStop(nodes, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); - Stop toStop = getOrCreateStop(nodes, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); - - if (fromStop == null || toStop == null) - return; + private static void addLine(String[] line, HashSet nodes, HashMap tmp, HashMap> connections) { + Stop fromStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); + Stop toStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); - Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); - - connections.computeIfAbsent(fromStop.getId(), k -> new HashSet<>()).add(connection); - -/* - Set linesConnections = connections.get(fromStop.getId()); - - String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); - int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); - Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); - if (linesConnections == null) - connections.put(fromStop.getId(), new HashSet<>(List.of(connection))); - else - linesConnections.add(connection); - - if (lineNB++ % 1000 == 0) { - LOGGER.log(Level.INFO,"Added line " + line[IDFM_TRACE_ID_INDEX] + " from " + line[IDFM_TRACE_FROM_INDEX] + " to " + line[IDFM_TRACE_TO_INDEX] + ", line " + lineNB); - } + Connection connection = new Connection(toStop, line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + connections.computeIfAbsent(fromStop, k -> new HashSet<>()).add(connection); } - */ public static void main(String[] args){ - // TODO ajouter option du nom en argument if (args.length != 0) { LOGGER.severe("Invalid command line. Target file names are in the main file for now."); return; } - /* try { HashSet nodes = new HashSet<>(); - HashMap> connections = new HashMap<>(); + HashMap> connections = new HashMap<>(); + HashMap tmp = new HashMap<>(); CSVTools.readCSVFromFile(TRACE_FILE_NAME, - (String[] line) -> addLine(line, nodes, connections)); + (String[] line) -> addLine(line, nodes, tmp, connections)); Graph graph = new Graph<>(nodes, connections); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error while reading the line paths", e); } - - */ } } 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 2cbd2db..fc9473e 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 @@ -9,8 +9,11 @@ public class Stop implements GraphNode { private final int id; private final Set lines; + private final String name; + private final double latitude; + private final double longitude; public Stop(String line, String name, double latitude, double longitude) { From 62b1e9be3601ac993a8b1952d262fc34f78003a8 Mon Sep 17 00:00:00 2001 From: HU helene Date: Mon, 25 Mar 2024 22:50:47 +0100 Subject: [PATCH 5/7] =?UTF-8?q?#16=20Adaptation=20mod=C3=A9lisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gla/project/itinerary/Connection.java | 17 ++++++++++ .../u_paris/gla/project/itinerary/Finder.java | 33 ++++++++++--------- .../u_paris/gla/project/itinerary/Graph.java | 16 ++++++--- .../gla/project/itinerary/GraphNode.java | 8 ++--- .../u_paris/gla/project/itinerary/Stop.java | 5 ++- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java index 4c7b855..ccc3bfd 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java @@ -15,4 +15,21 @@ public class Connection{ this.distance = distance; this.time = time; } + + + public String getLineName() { + return lineName; + } + + public double getDistance() { + return distance; + } + + public int getTime() { + return time; + } + + public Stop getStop() { + return stop; + } } 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 f0d67a0..6bfcf95 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 @@ -2,21 +2,21 @@ package fr.u_paris.gla.project.itinerary; import java.util.*; -public class Finder > { - private Graph graph; - public Finder(Graph graph) { +public class Finder { + private Graph graph; + public Finder(Graph graph) { this.graph = graph; } - public List findPath(T startNode, T goalNode) { - PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF)); - HashSet closedSet = new HashSet<>(); - HashMap cameFrom = new HashMap<>(); - HashMap gScore = new HashMap<>(); - HashMap fScore = new HashMap<>(); + public List findPath(Stop startNode, Stop goalNode) { + PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF)); + HashSet closedSet = new HashSet<>(); + HashMap cameFrom = new HashMap<>(); + HashMap gScore = new HashMap<>(); + HashMap fScore = new HashMap<>(); // Initialize scores for all nodes to infinity - for (T node : graph.getNodes()) { + for (Stop node : graph.getNodes()) { gScore.put(node, Double.POSITIVE_INFINITY); fScore.put(node, Double.POSITIVE_INFINITY); } @@ -28,7 +28,7 @@ public class Finder > { openSet.add(startNode); while (!openSet.isEmpty()) { - T current = openSet.poll(); + Stop current = openSet.poll(); if (current.equals(goalNode)) { return reconstructPath(cameFrom, current); @@ -36,12 +36,13 @@ public class Finder > { closedSet.add(current); - for (T neighbor : current.getNeighbors()) { + for (Connection connection : graph.getConnections(current) ) { + Stop neighbor = connection.getStop(); if (closedSet.contains(neighbor)) { continue; // Ignore the neighbor which is already evaluated. } - double tentativeGScore = gScore.get(current) + current.getCost(neighbor); + double tentativeGScore = gScore.get(current) + connection.getDistance(); if (!openSet.contains(neighbor)) { openSet.add(neighbor); @@ -60,8 +61,8 @@ public class Finder > { return null; } - private List reconstructPath(HashMap cameFrom, T current) { - List totalPath = new ArrayList<>(); + private List reconstructPath(HashMap cameFrom, Stop current) { + List totalPath = new ArrayList<>(); totalPath.add(current); while (cameFrom.containsKey(current)) { @@ -73,7 +74,7 @@ public class Finder > { } //TODO: - public List findPath(double longitude, double latitude){ + public List findPath(double longitude, double latitude){ return null; } 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 23f47e2..4d31dd9 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 @@ -3,17 +3,23 @@ package fr.u_paris.gla.project.itinerary; import java.util.Map; import java.util.Set; -public class Graph { - private final Set nodes; +public class Graph{ + private final Set nodes; - private final Map> connections; + private final Map> connections; - public Graph(Set nodes, Map> connections) { + public Graph(Set nodes, Map> connections) { this.nodes = nodes; this.connections = connections; } - public Set getNodes() { + + + public Set getConnections(Stop node) { + return connections.get(node); + } + + public Set getNodes() { return nodes; } } 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 index 197bcf4..9067cb4 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -3,11 +3,11 @@ package fr.u_paris.gla.project.itinerary; import java.util.List; import java.util.Set; -public interface GraphNode{ +public interface GraphNode{ int getId(); - double getHeuristicCost(T goalNode); + double getHeuristicCost(Stop goalNode); - Set getNeighbors(); - double getCost(T neighbor); + Set getNeighbors(); + double getCost(Stop neighbor); double getF(); } 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 2da2427..da0d4ca 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 @@ -28,7 +28,7 @@ public class Stop implements GraphNode { } @Override - public double getHeuristicCost(Object goalNode) { + public double getHeuristicCost(Stop goalNode) { return 0; } @@ -37,9 +37,8 @@ public class Stop implements GraphNode { return lines; } - @Override - public double getCost(Object neighbor) { + public double getCost(Stop neighbor) { return 0; } From 0df767c9b5168e003adb92cb23bbf450129cc829 Mon Sep 17 00:00:00 2001 From: HU helene Date: Fri, 29 Mar 2024 09:56:22 +0100 Subject: [PATCH 6/7] =?UTF-8?q?#16=20Adaptation=20mod=C3=A9lisation=20debu?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../u_paris/gla/project/itinerary/Finder.java | 2 ++ .../u_paris/gla/project/itinerary/Graph.java | 7 +++-- .../u_paris/gla/project/itinerary/Main.java | 31 ++++++++++++++++++- .../u_paris/gla/project/itinerary/Stop.java | 11 +++++++ 4 files changed, 48 insertions(+), 3 deletions(-) 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 6bfcf95..a8a8312 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 @@ -29,6 +29,8 @@ public class Finder { while (!openSet.isEmpty()) { Stop current = openSet.poll(); + //System.out.println(current); + //System.out.println(graph.getConnections(current)); if (current.equals(goalNode)) { return reconstructPath(cameFrom, current); 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 3009bbc..3025a04 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 @@ -14,8 +14,6 @@ public class Graph{ this.connections = connections; } - - public Set getConnections(Stop node) { return connections.get(node); } @@ -23,4 +21,9 @@ public class Graph{ public Set getNodes() { return nodes; } + + + public Map> getConnections() { + return connections; + } } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java index eb0266d..781ed19 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java @@ -77,6 +77,11 @@ public class Main{ connections.computeIfAbsent(fromStop, k -> new HashSet<>()).add(connection); } + public static void find(Graph graph){ + + + } + public static void main(String[] args){ if (args.length != 0) { LOGGER.severe("Invalid command line. Target file names are in the main file for now."); @@ -89,9 +94,33 @@ public class Main{ HashMap tmp = new HashMap<>(); CSVTools.readCSVFromFile(TRACE_FILE_NAME, (String[] line) -> addLine(line, nodes, tmp, connections)); - Graph graph = new Graph<>(nodes, connections); + + Stop porteivry = tmp.get("48.821352988336876, 2.369294978223312"); + Stop repu = tmp.get("48.867687468165165, 2.3640990472225725"); + + Graph graph = new Graph(nodes, connections); + int cpt = 0; + for (Map.Entry> entry : graph.getConnections().entrySet()) { + if (entry.getValue() == null) cpt +=1; + } + Stop garenord = tmp.get("48.88143149182458, 2.357767843520973"); + + Stop chatelet = tmp.get("48.856953460785334, 2.3481609912345776"); + //System.out.println(graph.getConnections(garenord)); + //System.out.println(cpt); + //System.out.println(graph.getConnections(porteivry)); + Finder finder = new Finder(graph); + + List res = finder.findPath(porteivry, chatelet); + + for (Stop element : res) { + System.out.println(element); + } + } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error while reading the line paths", e); } + + } } 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 bc0ac4d..70c6e79 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 @@ -25,6 +25,17 @@ public class Stop implements GraphNode { this.longitude = longitude; } + @Override + public String toString() { + return "Stop{" + + "id=" + id + + ", lines=" + lines + + ", name='" + name + '\'' + + ", latitude=" + latitude + + ", longitude=" + longitude + + '}'; + } + @Override public int getId(){ return id; From 821c8f0098137fc731caf3ef9f6dbee7faacb1f6 Mon Sep 17 00:00:00 2001 From: francois Date: Fri, 29 Mar 2024 19:54:25 +0100 Subject: [PATCH 7/7] Corrections de bugs --- .../u_paris/gla/project/itinerary/Finder.java | 5 ++ .../gla/project/itinerary/GraphNode.java | 1 + .../u_paris/gla/project/itinerary/Main.java | 47 ++++++++++++------- .../u_paris/gla/project/itinerary/Stop.java | 13 +++-- .../fr/u_paris/gla/project/utils/GPS.java | 10 ++-- 5 files changed, 51 insertions(+), 25 deletions(-) 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 a8a8312..f91047d 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 @@ -38,6 +38,10 @@ public class Finder { closedSet.add(current); + if(graph.getConnections(current) == null) { + continue; + } + for (Connection connection : graph.getConnections(current) ) { Stop neighbor = connection.getStop(); if (closedSet.contains(neighbor)) { @@ -56,6 +60,7 @@ public class Finder { cameFrom.put(neighbor, current); gScore.put(neighbor, tentativeGScore); fScore.put(neighbor, tentativeGScore + neighbor.getHeuristicCost(goalNode)); + neighbor.setF(fScore.get(neighbor)); } } 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 index d14f37b..175db6e 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -10,4 +10,5 @@ public interface GraphNode { Set getNeighbors(); double getCost(Stop neighbor); double getF(); + void setF(double value); } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java index 781ed19..5a75b34 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java @@ -2,6 +2,7 @@ package fr.u_paris.gla.project.itinerary; import fr.u_paris.gla.project.idfm.*; import fr.u_paris.gla.project.utils.CSVTools; +import fr.u_paris.gla.project.utils.GPS; import java.io.IOException; import java.util.*; @@ -35,6 +36,8 @@ public class Main{ private static final int IDFM_TRACE_DISTANCE_INDEX = 7; + private static final double MARGE_ERREUR = 1.; + // Parser helper values /** @@ -48,24 +51,31 @@ public class Main{ return new double[] {Double.parseDouble(stringCoords[0]), Double.parseDouble(stringCoords[1])}; } - private static Stop getOrCreateStop(HashSet nodes, HashMap tmp, String name, String gps, String lineId) { - Stop stop = tmp.get(gps); - if (stop == null) { - double[] coords = getCoords(gps); - Stop newStop = new Stop(lineId, name, coords[0], coords[1]); - nodes.add(newStop); - tmp.put(gps, newStop); - return newStop; - } - else { - stop.addLine(lineId); - return stop; + private static Stop getOrCreateStop(HashSet nodes, HashMap> tmp, String name, String gps, String lineId) { + ArrayList stopList = tmp.get(name); + if (stopList != null) { + for(Stop stop : stopList) { + double[] coords = getCoords(gps); + double dist = GPS.distance(coords[0], coords[1], stop.getLatitude(), stop.getLongitude()); + if(dist < MARGE_ERREUR) { + stop.addLine(lineId); + return stop; + } + } } + + double[] coords = getCoords(gps); + Stop newStop = new Stop(lineId, name, coords[0], coords[1]); + nodes.add(newStop); + stopList = stopList == null ? new ArrayList<>() : stopList; + stopList.add(newStop); + tmp.put(name, stopList); + return newStop; } public static int lineNB = 0; - private static void addLine(String[] line, HashSet nodes, HashMap tmp, HashMap> connections) { + private static void addLine(String[] line, HashSet nodes, HashMap> tmp, HashMap> connections) { Stop fromStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); Stop toStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); @@ -91,21 +101,22 @@ public class Main{ try { HashSet nodes = new HashSet<>(); HashMap> connections = new HashMap<>(); - HashMap tmp = new HashMap<>(); + HashMap> tmp = new HashMap<>(); CSVTools.readCSVFromFile(TRACE_FILE_NAME, (String[] line) -> addLine(line, nodes, tmp, connections)); - Stop porteivry = tmp.get("48.821352988336876, 2.369294978223312"); - Stop repu = tmp.get("48.867687468165165, 2.3640990472225725"); + + Stop porteivry = tmp.get("Porte d'Ivry").get(0); + Stop repu = tmp.get("République").get(0); Graph graph = new Graph(nodes, connections); int cpt = 0; for (Map.Entry> entry : graph.getConnections().entrySet()) { if (entry.getValue() == null) cpt +=1; } - Stop garenord = tmp.get("48.88143149182458, 2.357767843520973"); + Stop garenord = tmp.get("Gare du Nord").get(0); - Stop chatelet = tmp.get("48.856953460785334, 2.3481609912345776"); + Stop chatelet = tmp.get("Châtelet").get(0); //System.out.println(graph.getConnections(garenord)); //System.out.println(cpt); //System.out.println(graph.getConnections(porteivry)); 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 70c6e79..240c9b0 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 @@ -16,6 +16,8 @@ public class Stop implements GraphNode { private final double longitude; + private double f; + public Stop(String line, String name, double latitude, double longitude) { lines = new HashSet<>(); lines.add(line); @@ -23,6 +25,7 @@ public class Stop implements GraphNode { this.name = name; this.latitude = latitude; this.longitude = longitude; + this.f = 0; } @Override @@ -47,8 +50,8 @@ public class Stop implements GraphNode { } @Override - public Set getNeighbors() { - return lines; + public Set getNeighbors() { + return null; } @Override @@ -58,7 +61,11 @@ public class Stop implements GraphNode { @Override public double getF() { - return 0; + return f; + } + + public void setF(double value) { + this.f = value; } public String getName(){ diff --git a/src/main/java/fr/u_paris/gla/project/utils/GPS.java b/src/main/java/fr/u_paris/gla/project/utils/GPS.java index 1aaca79..0eb7130 100644 --- a/src/main/java/fr/u_paris/gla/project/utils/GPS.java +++ b/src/main/java/fr/u_paris/gla/project/utils/GPS.java @@ -30,14 +30,16 @@ public final class GPS { * @param longitude1 the longitude of the first position * @param latitude2 the latitude of the second position * @param longitude2 the longitude of the second position - * @return the flying distance */ + * @return the flying distance in km*/ public static double distance(double latitude1, double longitude1, double latitude2, double longitude2) { - double deltaLatitude = degreeToRadian(latitude2 - latitude1); + latitude1 = degreeToRadian(latitude1); + latitude2 = degreeToRadian(latitude2); + double deltaLatitude = latitude2 - latitude1; double deltaLongitude = degreeToRadian(longitude2 - longitude1); double a = Math.pow(Math.sin(deltaLatitude / 2), 2) - + Math.pow(Math.sin(deltaLongitude), 2) * Math.cos(latitude1) + + Math.pow(Math.sin(deltaLongitude / 2), 2) * Math.cos(latitude1) * Math.cos(latitude2); - return EARTH_RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + return 2 * EARTH_RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } }