From 458d31f2c4cbdadbecd7520a5fdc24b66ff8242a Mon Sep 17 00:00:00 2001 From: Francois Date: Sat, 4 May 2024 20:37:38 +0200 Subject: [PATCH] Some algorithm optimizations --- .../gla/project/idfm/CSVStreamProvider.java | 2 +- .../u_paris/gla/project/itinerary/Connection.java | 2 +- .../fr/u_paris/gla/project/itinerary/Finder.java | 7 ++++++- .../fr/u_paris/gla/project/itinerary/Graph.java | 15 +++++++++++++++ .../fr/u_paris/gla/project/itinerary/Stop.java | 8 +++++++- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java index fe8133f..f31339d 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java @@ -271,7 +271,7 @@ public final class CSVStreamProvider { Double max_speed = max_speed_by_type.get(type); Double two_acc_distance = two_acceleration_distance_by_type.get(type); return Math.max(0, distance - two_acc_distance) / max_speed - + Math.pow(Math.min(distance, two_acc_distance) / max_speed, 2); + + 2 * Math.sqrt(Math.min(distance, two_acc_distance) * two_acc_distance)/max_speed; } private void fillTransports(int bif) { 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 c9b8608..c1eb1d3 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 @@ -87,7 +87,7 @@ public class Connection{ public double getCost(double currentTime) { if(this.schedules.size() == 0) { - if(this.lineName.equals("WALK")) { + if(this.lineName.equals("WALK") || this.lineName.equals("")) { return this.time; } return this.time + 900; 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 2cd8bba..1d469f6 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 @@ -27,7 +27,12 @@ public class Finder { graph.addNode(fromNode); graph.addNode(toNode); - return findPath(fromNode, toNode, startTime); + List result = findPath(fromNode, toNode, startTime); + + graph.removeNode(fromNode); + graph.removeNode(toNode); + + return result; } /** 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 76af8dd..a8919eb 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.ArrayList; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -43,6 +44,20 @@ public class Graph { } public void removeNode(Stop s) { + for(Stop stop : nodes) { + if(getConnections(stop) == null) { + continue; + } + ArrayList toRemove = new ArrayList<>(); + for(Connection c : getConnections(stop)) { + if(c.getStop() == s) { + toRemove.add(c); + } + } + for(Connection c : toRemove) { + getConnections(stop).remove(c); + } + } nodes.remove(s); connections.remove(s); } 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 c814f37..0305d79 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 @@ -1,5 +1,7 @@ package fr.u_paris.gla.project.itinerary; +import fr.u_paris.gla.project.utils.GPS; + import java.util.HashSet; import java.util.Set; @@ -20,6 +22,9 @@ public class Stop { private double f; + //Maximal speed in m/s + private final double MAX_SPEED = 14.; + public Stop(String line, String name, double latitude, double longitude) { lines = new HashSet<>(); lines.add(line); @@ -40,7 +45,8 @@ public class Stop { } public double getHeuristicCost(Stop goalNode) { - return 0; + double distance = GPS.distance(this.latitude, this.longitude, goalNode.latitude, goalNode.longitude); + return distance/MAX_SPEED; } public Set getNeighbors() {