From 62b1e9be3601ac993a8b1952d262fc34f78003a8 Mon Sep 17 00:00:00 2001 From: HU helene Date: Mon, 25 Mar 2024 22:50:47 +0100 Subject: [PATCH] =?UTF-8?q?#16=20Adaptation=20mod=C3=A9lisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gla/project/itinerary/Connection.java | 17 ++++++++++ .../u_paris/gla/project/itinerary/Finder.java | 33 ++++++++++--------- .../u_paris/gla/project/itinerary/Graph.java | 16 ++++++--- .../gla/project/itinerary/GraphNode.java | 8 ++--- .../u_paris/gla/project/itinerary/Stop.java | 5 ++- 5 files changed, 51 insertions(+), 28 deletions(-) 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 4c7b855..ccc3bfd 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 @@ -15,4 +15,21 @@ public class Connection{ this.distance = distance; this.time = time; } + + + public String getLineName() { + return lineName; + } + + public double getDistance() { + return distance; + } + + public int getTime() { + return time; + } + + public Stop getStop() { + return stop; + } } 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 f0d67a0..6bfcf95 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 @@ -2,21 +2,21 @@ package fr.u_paris.gla.project.itinerary; import java.util.*; -public class Finder > { - private Graph graph; - public Finder(Graph graph) { +public class Finder { + private Graph graph; + public Finder(Graph graph) { this.graph = graph; } - public List findPath(T startNode, T goalNode) { - PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF)); - HashSet closedSet = new HashSet<>(); - HashMap cameFrom = new HashMap<>(); - HashMap gScore = new HashMap<>(); - HashMap fScore = new HashMap<>(); + public List findPath(Stop startNode, Stop goalNode) { + PriorityQueue openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF)); + HashSet closedSet = new HashSet<>(); + HashMap cameFrom = new HashMap<>(); + HashMap gScore = new HashMap<>(); + HashMap fScore = new HashMap<>(); // Initialize scores for all nodes to infinity - for (T node : graph.getNodes()) { + for (Stop node : graph.getNodes()) { gScore.put(node, Double.POSITIVE_INFINITY); fScore.put(node, Double.POSITIVE_INFINITY); } @@ -28,7 +28,7 @@ public class Finder > { openSet.add(startNode); while (!openSet.isEmpty()) { - T current = openSet.poll(); + Stop current = openSet.poll(); if (current.equals(goalNode)) { return reconstructPath(cameFrom, current); @@ -36,12 +36,13 @@ public class Finder > { closedSet.add(current); - for (T neighbor : current.getNeighbors()) { + for (Connection connection : graph.getConnections(current) ) { + Stop neighbor = connection.getStop(); if (closedSet.contains(neighbor)) { continue; // Ignore the neighbor which is already evaluated. } - double tentativeGScore = gScore.get(current) + current.getCost(neighbor); + double tentativeGScore = gScore.get(current) + connection.getDistance(); if (!openSet.contains(neighbor)) { openSet.add(neighbor); @@ -60,8 +61,8 @@ public class Finder > { return null; } - private List reconstructPath(HashMap cameFrom, T current) { - List totalPath = new ArrayList<>(); + private List reconstructPath(HashMap cameFrom, Stop current) { + List totalPath = new ArrayList<>(); totalPath.add(current); while (cameFrom.containsKey(current)) { @@ -73,7 +74,7 @@ public class Finder > { } //TODO: - public List findPath(double longitude, double latitude){ + public List findPath(double longitude, double latitude){ return null; } 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 23f47e2..4d31dd9 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 @@ -3,17 +3,23 @@ package fr.u_paris.gla.project.itinerary; import java.util.Map; import java.util.Set; -public class Graph { - private final Set nodes; +public class Graph{ + private final Set nodes; - private final Map> connections; + private final Map> connections; - public Graph(Set nodes, Map> connections) { + public Graph(Set nodes, Map> connections) { this.nodes = nodes; this.connections = connections; } - public Set getNodes() { + + + public Set getConnections(Stop node) { + return connections.get(node); + } + + public Set getNodes() { return nodes; } } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java index 197bcf4..9067cb4 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -3,11 +3,11 @@ package fr.u_paris.gla.project.itinerary; import java.util.List; import java.util.Set; -public interface GraphNode{ +public interface GraphNode{ int getId(); - double getHeuristicCost(T goalNode); + double getHeuristicCost(Stop goalNode); - Set getNeighbors(); - double getCost(T neighbor); + Set getNeighbors(); + double getCost(Stop neighbor); double getF(); } 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 2da2427..da0d4ca 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 @@ -28,7 +28,7 @@ public class Stop implements GraphNode { } @Override - public double getHeuristicCost(Object goalNode) { + public double getHeuristicCost(Stop goalNode) { return 0; } @@ -37,9 +37,8 @@ public class Stop implements GraphNode { return lines; } - @Override - public double getCost(Object neighbor) { + public double getCost(Stop neighbor) { return 0; }