Corrections de bugs

This commit is contained in:
francois 2024-03-29 19:54:25 +01:00
parent 0df767c9b5
commit 821c8f0098
5 changed files with 51 additions and 25 deletions

View file

@ -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));
}
}

View file

@ -10,4 +10,5 @@ public interface GraphNode {
Set<Stop> getNeighbors();
double getCost(Stop neighbor);
double getF();
void setF(double value);
}

View file

@ -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<Stop> nodes, HashMap<String, Stop> tmp, String name, String gps, String lineId) {
Stop stop = tmp.get(gps);
if (stop == null) {
private static Stop getOrCreateStop(HashSet<Stop> nodes, HashMap<String, ArrayList<Stop>> tmp, String name, String gps, String lineId) {
ArrayList<Stop> stopList = tmp.get(name);
if (stopList != null) {
for(Stop stop : stopList) {
double[] coords = getCoords(gps);
Stop newStop = new Stop(lineId, name, coords[0], coords[1]);
nodes.add(newStop);
tmp.put(gps, newStop);
return newStop;
}
else {
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<Stop> nodes, HashMap<String, Stop> tmp, HashMap<Stop, Set<Connection>> connections) {
private static void addLine(String[] line, HashSet<Stop> nodes, HashMap<String, ArrayList<Stop>> tmp, HashMap<Stop, Set<Connection>> 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<Stop> nodes = new HashSet<>();
HashMap<Stop, Set<Connection>> connections = new HashMap<>();
HashMap<String, Stop> tmp = new HashMap<>();
HashMap<String, ArrayList<Stop>> 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<Stop, Set<Connection>> 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));

View file

@ -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<Stop> 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(){

View file

@ -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));
}
}