forgot to commit this part last time, final version

This commit is contained in:
Ksan 2025-09-14 15:53:24 +02:00
parent 5ebf9d2b2b
commit 02acc9643a

View File

@ -4,15 +4,13 @@ import dev.ksan.travelpathoptimizer.model.City;
import dev.ksan.travelpathoptimizer.model.Departure; import dev.ksan.travelpathoptimizer.model.Departure;
import dev.ksan.travelpathoptimizer.model.Location; import dev.ksan.travelpathoptimizer.model.Location;
import dev.ksan.travelpathoptimizer.model.TransportType; 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.Duration;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.*; import java.util.*;
/** /**
* Simulates pathfinding in a transportation network, managing paths between cities * Simulates pathfinding in a transportation network, managing paths between cities and calculating
* and calculating top paths based on different cost metrics (e.g., time, price, hops). * top paths based on different cost metrics (e.g., time, price, hops).
*/ */
public class GraphSimulation { public class GraphSimulation {
@ -35,9 +33,7 @@ public class GraphSimulation {
return pathList; 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() { public void reset() {
topPaths.clear(); topPaths.clear();
pathIdCounter = 1; pathIdCounter = 1;
@ -87,8 +83,8 @@ public class GraphSimulation {
} }
/** /**
* Recursively calculates the top paths from a starting city to an ending city, considering * Recursively calculates the top paths from a starting city to an ending city, considering the
* the type of cost metric (e.g., time, price, or hops). * type of cost metric (e.g., time, price, or hops).
* *
* @param currentCity the city where the journey is currently at. * @param currentCity the city where the journey is currently at.
* @param endCity the destination city of the journey. * @param endCity the destination city of the journey.
@ -132,23 +128,26 @@ public class GraphSimulation {
Duration duration = Duration.between(currentTime, arrivalTime); Duration duration = Duration.between(currentTime, arrivalTime);
duration = duration.abs(); duration = duration.abs();
if (type.equals("time")) { if (type.equals("time")) {
cost += duration.toMinutes(); cost += duration.toMinutes();
if(lastType == TransportType.NOT_ASSIGNED){ if (lastType == TransportType.NOT_ASSIGNED) {
cost += dep.getMinTransferTime(); cost += dep.getMinTransferTime();
}else if(lastType != dep.getType()){ } else if (lastType != dep.getType()) {
cost += dep.getMinTransferTime(); cost += dep.getMinTransferTime();
} }
//cost += dep.getMinTransferTime(); // cost += dep.getMinTransferTime();
} else if (type.equals("price")) { } else if (type.equals("price")) {
cost += dep.getPrice(); cost += dep.getPrice();
} else if (type.equals("hops")) { } else if (type.equals("hops")) {
cost++; cost++;
/*if(!(lastType == dep.getType())){
cost++;
}
*/
if (!topPaths.isEmpty() && totalCost + cost >= topPaths.peek().getCost()) continue; if (!topPaths.isEmpty() && totalCost + cost >= topPaths.peek().getCost()) continue;
} else { } else {
return; return;
@ -160,7 +159,8 @@ public class GraphSimulation {
path.add(nextCity); path.add(nextCity);
departures.add(dep.getIdCounter()); 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); departures.remove(departures.size() - 1);
path.remove(path.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() { public static void printTopPaths() {
if (topPaths.isEmpty()) { if (topPaths.isEmpty()) {
System.out.println("No Paths"); 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 startCity the starting city of the path.
* @param endCity the destination 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"). * @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. * @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<Location, Double> calculateShortestPath(City startCity, City endCity, String type) { public Map<Location, Double> calculateShortestPath(City startCity, City endCity, String type) {
int n = matrix.length; int n = matrix.length;
int m = matrix[0].length; int m = matrix[0].length;
@ -302,3 +303,4 @@ public class GraphSimulation {
return topPaths; return topPaths;
} }
} }