From e97a517f1fea40804c16157cfcda985cd229cd17 Mon Sep 17 00:00:00 2001 From: AngeHerman Date: Fri, 5 Apr 2024 11:14:34 +0200 Subject: [PATCH] build bifurcation optimized --- .../project/idfm/CSVSchedulesProvider.java | 2 +- .../project/idfm/IDFMNetworkExtractor.java | 18 ++- .../fr/u_paris/gla/project/idfm/Stop.java | 48 ++++--- .../u_paris/gla/project/idfm/Transport.java | 130 +++++++++++++----- 4 files changed, 146 insertions(+), 52 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java index 7a805e8..37a3553 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java @@ -24,7 +24,7 @@ public class CSVSchedulesProvider { put("Bus", new int[]{11, 8, 7}); put("Funicular", new int[]{15, 25, 20}); put("Tram", new int[]{6, 7, 8, 9}); - put("Rail", new int[]{10, 11,15,12}); + put("Rail", new int[]{10, 11,15,12,20}); put("Subway", new int[]{4, 2, 6,3,3,4}); }}; 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 53622c4..1ad8fa4 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 @@ -127,7 +127,7 @@ public class IDFMNetworkExtractor { Stop maisonBlanche = ligne_7.stopsMap.get("Maison Blanche"); System.out.println(maisonBlanche.name); - for (BifStop nextEntry : maisonBlanche.connected) { + for (BifStop nextEntry : maisonBlanche.connected.values()) { System.out.println(nextEntry.bifurc+ nextEntry.stop.name); } System.out.println("****** AFFICHAGE LIGNE ******"); @@ -135,7 +135,7 @@ public class IDFMNetworkExtractor { Stop corientin = ligne_7.stopsMap.get("Corentin Cariou"); System.out.println(corientin.name); - for (BifStop nextEntry : corientin.connected) { + for (BifStop nextEntry : corientin.connected.values()) { System.out.println(nextEntry.bifurc+ nextEntry.stop.name); } System.out.println("***************************"); @@ -154,9 +154,21 @@ public class IDFMNetworkExtractor { b54b.buildBifurcation(); System.out.println(b54b.descriptions); System.out.println("******************Building bifurcations ************************"); + long startTime = System.currentTimeMillis(); + for (Transport entry : transports.values()) { - entry.buildBifurcation(); + entry.buildBifurcationOptimzed(); } + + long endTime = System.currentTimeMillis(); + long tempsPasse = endTime - startTime; + + long minutes = (tempsPasse / 1000) / 60; + long seconds = (tempsPasse / 1000) % 60; + long milliseconds = tempsPasse % 1000; + + System.out.println("Temps écoulé : " + minutes + " minutess, " + seconds + " secndes et " + milliseconds + " millis"); + System.out.println("******************Fin Building bifurcations ************************"); // System.out.println("Transport size :"+transports.size()); // System.out.println("Trace size :"+traces.size()); diff --git a/src/main/java/fr/u_paris/gla/project/idfm/Stop.java b/src/main/java/fr/u_paris/gla/project/idfm/Stop.java index 72d8d44..eabed68 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/Stop.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/Stop.java @@ -7,32 +7,46 @@ import java.util.HashMap; public class Stop { - List connected = new ArrayList<>(); + Map connected = new HashMap<>(); public String name; public Stop(String n){ name = n; } + // public boolean isStopConnected(String stopName) { + // // for (BifStop b : connected) { + // // if (b.stop.name.equals(stopName)) { + // // return true; + // // } + // // } + // // return false; + // return getStop(stopName) != null; + // } + + // public Stop getStop(String stopName) { + // // if (this.name.equals(stopName)) { + // // return this; + // // } + // // for (BifStop b : connected) { + // // if (b.stop.name.equals(stopName)) { + // // return b.stop; + // // } + // // } + // // return null; + // return transport.getStopFromIndex(stopName); + // } public boolean isStopConnected(String stopName) { - for (BifStop b : connected) { - if (b.stop.name.equals(stopName)) { - return true; - } - } - return false; + return connected.containsKey(stopName); } - public Stop getStop(String stopName) { - if (this.name.equals(stopName)) { - return this; - } - for (BifStop b : connected) { - if (b.stop.name.equals(stopName)) { - return b.stop; - } - } - return null; + public void addConnectedStop(BifStop stop) { + connected.put(stop.stop.name, stop); } + + public BifStop getConnectedStop(String stopName) { + return connected.get(stopName); + } + } diff --git a/src/main/java/fr/u_paris/gla/project/idfm/Transport.java b/src/main/java/fr/u_paris/gla/project/idfm/Transport.java index e16e7e1..5dd2404 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/Transport.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/Transport.java @@ -2,14 +2,16 @@ package fr.u_paris.gla.project.idfm; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.AbstractMap.SimpleEntry; public class Transport { - Map stopsMap = new LinkedHashMap<>(); + Map stopsMap = new HashMap<>(); public String name; public String type; List descriptions = new ArrayList<>(); @@ -38,6 +40,23 @@ public class Transport { // System.out.println("J'en ai trouvé "+found); } + public void buildBifurcationOptimzed() { + int found = 0; + for (TraceDescription d : descriptions) { + Stop debut = stopsMap.get(d.from); + Stop fin = stopsMap.get(d.to); + if (debut != null && fin != null) { + Set alreadyVisited = new HashSet<>(); // Utiliser un HashSet pour les arrêts déjà visités + SimpleEntry> sol = roadToLastOptimized(debut.name, debut.name, fin.name, alreadyVisited, new ArrayList()); + if (sol.getKey()) { + found++; + d.bifurcation = sol.getValue(); + } + } + } + // System.out.println("J'en ai trouvé " + found); +} + public boolean isTerminus(String stop){ for(TraceDescription t: descriptions){ if(stop.equals(t.first) || stop.equals(t.last)) @@ -61,7 +80,7 @@ public class Transport { Stop current = stopsMap.get(currentStop); List > > solutions = new ArrayList<>(); - for(BifStop b: current.connected){ + for(BifStop b: current.connected.values()){ if(!visitedCopy.contains(b.stop.name)){ List bifCopy = new ArrayList<>(bif); if(b.bifurc!= 0) @@ -81,26 +100,75 @@ public class Transport { return new SimpleEntry<>(trouve,bifSol) ; } - public void addStop(String start,String end,int bifurcation){ - Stop start_stop; - Stop end_stop; - if (!stopsMap.containsKey(start)){ - start_stop = new Stop(start); - stopsMap.put(start, start_stop); - }else{ - start_stop = stopsMap.get(start); - } - - if (!stopsMap.containsKey(end)){ - end_stop = new Stop(end); - stopsMap.put(end, end_stop); - }else{ - end_stop = stopsMap.get(end); - } - start_stop.connected.add( new BifStop(bifurcation,end_stop) ); - + public SimpleEntry> roadToLastOptimized(String currentStop, String first, String last, Set alreadyVisited, List bif) { + if (currentStop.equals(last)) { + return new SimpleEntry<>(true, bif); } + // Checker if the current stop is the bad terminus + if (isTerminus(currentStop)) { + return new SimpleEntry<>(false, bif); + } + + alreadyVisited.add(currentStop); + + Stop current = stopsMap.get(currentStop); + List>> solutions = new ArrayList<>(); + for (BifStop b : current.connected.values()) { + if (!alreadyVisited.contains(b.stop.name)) { + List bifCopy = new ArrayList<>(bif); + if (b.bifurc != 0) { + bifCopy.add(b.bifurc); + } + solutions.add(roadToLastOptimized(b.stop.name, first, last, alreadyVisited, bifCopy)); + } + } + + // Todo: Send a list on list of integer in case there is a lot of path for the same direction + List bifSol = new ArrayList<>(); + boolean trouve = false; + for (SimpleEntry> se : solutions) { + if (se.getKey()) { + trouve = true; + bifSol = se.getValue(); + break; // Exit loop if a solution is found + } + } + + alreadyVisited.remove(currentStop); // Remove current stop from visited after processing + + return new SimpleEntry<>(trouve, bifSol); +} + + + public void addStop(String start, String end, int bifurcation){ + Stop startStop = stopsMap.computeIfAbsent(start, Stop::new); + Stop endStop = stopsMap.computeIfAbsent(end, Stop::new); + + BifStop connectedStop = new BifStop(bifurcation, endStop); + startStop.addConnectedStop(connectedStop); + } + + // public void addStop(String start,String end,int bifurcation){ + // Stop start_stop; + // Stop end_stop; + // if (!stopsMap.containsKey(start)){ + // start_stop = new Stop(start); + // stopsMap.put(start, start_stop); + // }else{ + // start_stop = stopsMap.get(start); + // } + + // if (!stopsMap.containsKey(end)){ + // end_stop = new Stop(end); + // stopsMap.put(end, end_stop); + // }else{ + // end_stop = stopsMap.get(end); + // } + // start_stop.connected.add( new BifStop(bifurcation,end_stop) ); + + // } + /** * @@ -121,17 +189,17 @@ public class Transport { return false; } - public void printAllConnectionStops() { - System.out.println("Affichage des couples (stop, next du stop):"); - for (Map.Entry entry : stopsMap.entrySet()) { - Stop stop = entry.getValue(); - System.out.println("Stop: " + stop.name); - System.out.println("Next:"); - for (BifStop nextEntry : stop.connected) { - System.out.println(nextEntry.bifurc + ": " + nextEntry.stop.name); - } - } - } + // public void printAllConnectionStops() { + // System.out.println("Affichage des couples (stop, next du stop):"); + // for (Map.Entry entry : stopsMap.entrySet()) { + // Stop stop = entry.getValue(); + // System.out.println("Stop: " + stop.name); + // System.out.println("Next:"); + // for (BifStop nextEntry : stop.connected) { + // System.out.println(nextEntry.bifurc + ": " + nextEntry.stop.name); + // } + // } + // } /**