#16 Adaptation modélisation
This commit is contained in:
parent
14b080ff4d
commit
62b1e9be36
5 changed files with 51 additions and 28 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,21 +2,21 @@ package fr.u_paris.gla.project.itinerary;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
public class Finder <T extends GraphNode<T>> {
|
||||
private Graph<T> graph;
|
||||
public Finder(Graph<T> graph) {
|
||||
public class Finder {
|
||||
private Graph graph;
|
||||
public Finder(Graph graph) {
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
public List<T> findPath(T startNode, T goalNode) {
|
||||
PriorityQueue<T> openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF));
|
||||
HashSet<T> closedSet = new HashSet<>();
|
||||
HashMap<T, T> cameFrom = new HashMap<>();
|
||||
HashMap<T, Double> gScore = new HashMap<>();
|
||||
HashMap<T, Double> fScore = new HashMap<>();
|
||||
public List<Stop> findPath(Stop startNode, Stop goalNode) {
|
||||
PriorityQueue<Stop> openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF));
|
||||
HashSet<Stop> closedSet = new HashSet<>();
|
||||
HashMap<Stop, Stop> cameFrom = new HashMap<>();
|
||||
HashMap<Stop, Double> gScore = new HashMap<>();
|
||||
HashMap<Stop, Double> 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 <T extends GraphNode<T>> {
|
|||
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 <T extends GraphNode<T>> {
|
|||
|
||||
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 <T extends GraphNode<T>> {
|
|||
return null;
|
||||
}
|
||||
|
||||
private List<T> reconstructPath(HashMap<T, T> cameFrom, T current) {
|
||||
List<T> totalPath = new ArrayList<>();
|
||||
private List<Stop> reconstructPath(HashMap<Stop, Stop> cameFrom, Stop current) {
|
||||
List<Stop> totalPath = new ArrayList<>();
|
||||
totalPath.add(current);
|
||||
|
||||
while (cameFrom.containsKey(current)) {
|
||||
|
@ -73,7 +74,7 @@ public class Finder <T extends GraphNode<T>> {
|
|||
}
|
||||
|
||||
//TODO:
|
||||
public List<T> findPath(double longitude, double latitude){
|
||||
public List<Stop> findPath(double longitude, double latitude){
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,17 +3,23 @@ package fr.u_paris.gla.project.itinerary;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class Graph <T extends GraphNode> {
|
||||
private final Set<T> nodes;
|
||||
public class Graph{
|
||||
private final Set<Stop> nodes;
|
||||
|
||||
private final Map<T, Set<Connection>> connections;
|
||||
private final Map<Stop, Set<Connection>> connections;
|
||||
|
||||
public Graph(Set<T> nodes, Map<T, Set<Connection>> connections) {
|
||||
public Graph(Set<Stop> nodes, Map<Stop, Set<Connection>> connections) {
|
||||
this.nodes = nodes;
|
||||
this.connections = connections;
|
||||
}
|
||||
|
||||
public Set<T> getNodes() {
|
||||
|
||||
|
||||
public Set<Connection> getConnections(Stop node) {
|
||||
return connections.get(node);
|
||||
}
|
||||
|
||||
public Set<Stop> getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ package fr.u_paris.gla.project.itinerary;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public interface GraphNode<T>{
|
||||
public interface GraphNode{
|
||||
int getId();
|
||||
double getHeuristicCost(T goalNode);
|
||||
double getHeuristicCost(Stop goalNode);
|
||||
|
||||
Set<T> getNeighbors();
|
||||
double getCost(T neighbor);
|
||||
Set<Stop> getNeighbors();
|
||||
double getCost(Stop neighbor);
|
||||
double getF();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue