From 02acc9643a7d1aa758bd11fcd6e12c779c011138 Mon Sep 17 00:00:00 2001 From: Ksan Date: Sun, 14 Sep 2025 15:53:24 +0200 Subject: [PATCH] forgot to commit this part last time, final version --- .../graphSimulation/GraphSimulation.java | 88 ++++++++++--------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/src/main/java/dev/ksan/travelpathoptimizer/graphSimulation/GraphSimulation.java b/src/main/java/dev/ksan/travelpathoptimizer/graphSimulation/GraphSimulation.java index 7bbc497..4fb9ace 100644 --- a/src/main/java/dev/ksan/travelpathoptimizer/graphSimulation/GraphSimulation.java +++ b/src/main/java/dev/ksan/travelpathoptimizer/graphSimulation/GraphSimulation.java @@ -4,15 +4,13 @@ import dev.ksan.travelpathoptimizer.model.City; import dev.ksan.travelpathoptimizer.model.Departure; import dev.ksan.travelpathoptimizer.model.Location; import dev.ksan.travelpathoptimizer.model.TransportType; -import dev.ksan.travelpathoptimizer.service.CityManager; -import dev.ksan.travelpathoptimizer.util.JsonParser; import java.time.Duration; import java.time.LocalTime; import java.util.*; /** - * Simulates pathfinding in a transportation network, managing paths between cities - * and calculating top paths based on different cost metrics (e.g., time, price, hops). + * Simulates pathfinding in a transportation network, managing paths between cities and calculating + * top paths based on different cost metrics (e.g., time, price, hops). */ public class GraphSimulation { @@ -22,7 +20,7 @@ public class GraphSimulation { private static int nextid = 0; private static Set visitedRoutes = new HashSet<>(); public static PriorityQueue topPaths = - new PriorityQueue<>(5, Comparator.comparingDouble(PathResult::getCost).reversed()); + new PriorityQueue<>(5, Comparator.comparingDouble(PathResult::getCost).reversed()); /** * Gets the list of top 5 paths sorted by cost in ascending order. @@ -35,9 +33,7 @@ public class GraphSimulation { return pathList; } - /** - * Resets the simulation, clearing all paths, visited routes, and top paths. - */ + /** Resets the simulation, clearing all paths, visited routes, and top paths. */ public void reset() { topPaths.clear(); pathIdCounter = 1; @@ -87,35 +83,35 @@ public class GraphSimulation { } /** - * Recursively calculates the top paths from a starting city to an ending city, considering - * the type of cost metric (e.g., time, price, or hops). + * Recursively calculates the top paths from a starting city to an ending city, considering the + * type of cost metric (e.g., time, price, or hops). * * @param currentCity the city where the journey is currently at. - * @param endCity the destination city of the journey. - * @param path the list of cities visited so far. - * @param totalCost the total cost accumulated up to the current city. + * @param endCity the destination city of the journey. + * @param path the list of cities visited so far. + * @param totalCost the total cost accumulated up to the current city. * @param currentTime the current time at the city. - * @param departures the list of departure IDs taken so far. - * @param type the type of cost metric to use ("time", "price", or "hops"). + * @param departures the list of departure IDs taken so far. + * @param type the type of cost metric to use ("time", "price", or "hops"). */ public static void calculateTopPaths( - City currentCity, - City endCity, - List path, - double totalCost, - LocalTime currentTime, - List departures, - String type, - TransportType lastType) { + City currentCity, + City endCity, + List path, + double totalCost, + LocalTime currentTime, + List departures, + String type, + TransportType lastType) { if (currentCity.getLocation().equals(endCity.getLocation())) { addToTopPaths( - new PathResult( - nextid++, - new ArrayList<>(path), - new ArrayList<>(departures), - totalCost, - currentTime)); + new PathResult( + nextid++, + new ArrayList<>(path), + new ArrayList<>(departures), + totalCost, + currentTime)); return; } @@ -132,23 +128,26 @@ public class GraphSimulation { Duration duration = Duration.between(currentTime, arrivalTime); duration = duration.abs(); - if (type.equals("time")) { cost += duration.toMinutes(); - if(lastType == TransportType.NOT_ASSIGNED){ + if (lastType == TransportType.NOT_ASSIGNED) { cost += dep.getMinTransferTime(); - }else if(lastType != dep.getType()){ + } else if (lastType != dep.getType()) { cost += dep.getMinTransferTime(); } - //cost += dep.getMinTransferTime(); + // cost += dep.getMinTransferTime(); } else if (type.equals("price")) { cost += dep.getPrice(); } else if (type.equals("hops")) { cost++; + /*if(!(lastType == dep.getType())){ + cost++; + } + */ if (!topPaths.isEmpty() && totalCost + cost >= topPaths.peek().getCost()) continue; } else { return; @@ -160,7 +159,8 @@ public class GraphSimulation { path.add(nextCity); departures.add(dep.getIdCounter()); - calculateTopPaths(nextCity, endCity, path, totalCost + cost, arrivalTime, departures, type, dep.getType()); + calculateTopPaths( + nextCity, endCity, path, totalCost + cost, arrivalTime, departures, type, dep.getType()); departures.remove(departures.size() - 1); path.remove(path.size() - 1); @@ -170,9 +170,7 @@ public class GraphSimulation { } } - /** - * Prints the top 5 paths to the console. - */ + /** Prints the top 5 paths to the console. */ public static void printTopPaths() { if (topPaths.isEmpty()) { System.out.println("No Paths"); @@ -192,13 +190,16 @@ public class GraphSimulation { } /** - * Calculates the shortest path between two cities using a specified cost metric (e.g., price, time, hops). + * Calculates the shortest path between two cities using a specified cost metric (e.g., price, + * time, hops). * * @param startCity the starting city of the path. - * @param endCity the destination city of the path. - * @param type the type of cost metric to use ("price", "time", or "hops"). - * @return a map where the keys are city locations and the values are the shortest cost to that location. + * @param endCity the destination city of the path. + * @param type the type of cost metric to use ("price", "time", or "hops"). + * @return a map where the keys are city locations and the values are the shortest cost to that + * location. */ + // old function used just for testing (missing come features and not used in the final product) public Map calculateShortestPath(City startCity, City endCity, String type) { int n = matrix.length; int m = matrix[0].length; @@ -213,7 +214,7 @@ public class GraphSimulation { distances.put(startCity.getLocation(), 0.0); PriorityQueue pq = - new PriorityQueue<>(Comparator.comparingDouble(city -> distances.get(city.getLocation()))); + new PriorityQueue<>(Comparator.comparingDouble(city -> distances.get(city.getLocation()))); pq.add(startCity); while (!pq.isEmpty()) { @@ -301,4 +302,5 @@ public class GraphSimulation { public static PriorityQueue getTopPaths() { return topPaths; } -} \ No newline at end of file +} +