From 821c8f0098137fc731caf3ef9f6dbee7faacb1f6 Mon Sep 17 00:00:00 2001 From: francois Date: Fri, 29 Mar 2024 19:54:25 +0100 Subject: [PATCH] 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)); } }