#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.distance = distance;
|
||||||
this.time = time;
|
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.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Finder <T extends GraphNode<T>> {
|
public class Finder {
|
||||||
private Graph<T> graph;
|
private Graph graph;
|
||||||
public Finder(Graph<T> graph) {
|
public Finder(Graph graph) {
|
||||||
this.graph = graph;
|
this.graph = graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<T> findPath(T startNode, T goalNode) {
|
public List<Stop> findPath(Stop startNode, Stop goalNode) {
|
||||||
PriorityQueue<T> openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF));
|
PriorityQueue<Stop> openSet = new PriorityQueue<>(Comparator.comparingDouble(GraphNode::getF));
|
||||||
HashSet<T> closedSet = new HashSet<>();
|
HashSet<Stop> closedSet = new HashSet<>();
|
||||||
HashMap<T, T> cameFrom = new HashMap<>();
|
HashMap<Stop, Stop> cameFrom = new HashMap<>();
|
||||||
HashMap<T, Double> gScore = new HashMap<>();
|
HashMap<Stop, Double> gScore = new HashMap<>();
|
||||||
HashMap<T, Double> fScore = new HashMap<>();
|
HashMap<Stop, Double> fScore = new HashMap<>();
|
||||||
|
|
||||||
// Initialize scores for all nodes to infinity
|
// Initialize scores for all nodes to infinity
|
||||||
for (T node : graph.getNodes()) {
|
for (Stop node : graph.getNodes()) {
|
||||||
gScore.put(node, Double.POSITIVE_INFINITY);
|
gScore.put(node, Double.POSITIVE_INFINITY);
|
||||||
fScore.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);
|
openSet.add(startNode);
|
||||||
|
|
||||||
while (!openSet.isEmpty()) {
|
while (!openSet.isEmpty()) {
|
||||||
T current = openSet.poll();
|
Stop current = openSet.poll();
|
||||||
|
|
||||||
if (current.equals(goalNode)) {
|
if (current.equals(goalNode)) {
|
||||||
return reconstructPath(cameFrom, current);
|
return reconstructPath(cameFrom, current);
|
||||||
|
@ -36,12 +36,13 @@ public class Finder <T extends GraphNode<T>> {
|
||||||
|
|
||||||
closedSet.add(current);
|
closedSet.add(current);
|
||||||
|
|
||||||
for (T neighbor : current.getNeighbors()) {
|
for (Connection connection : graph.getConnections(current) ) {
|
||||||
|
Stop neighbor = connection.getStop();
|
||||||
if (closedSet.contains(neighbor)) {
|
if (closedSet.contains(neighbor)) {
|
||||||
continue; // Ignore the neighbor which is already evaluated.
|
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)) {
|
if (!openSet.contains(neighbor)) {
|
||||||
openSet.add(neighbor);
|
openSet.add(neighbor);
|
||||||
|
@ -60,8 +61,8 @@ public class Finder <T extends GraphNode<T>> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<T> reconstructPath(HashMap<T, T> cameFrom, T current) {
|
private List<Stop> reconstructPath(HashMap<Stop, Stop> cameFrom, Stop current) {
|
||||||
List<T> totalPath = new ArrayList<>();
|
List<Stop> totalPath = new ArrayList<>();
|
||||||
totalPath.add(current);
|
totalPath.add(current);
|
||||||
|
|
||||||
while (cameFrom.containsKey(current)) {
|
while (cameFrom.containsKey(current)) {
|
||||||
|
@ -73,7 +74,7 @@ public class Finder <T extends GraphNode<T>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO:
|
//TODO:
|
||||||
public List<T> findPath(double longitude, double latitude){
|
public List<Stop> findPath(double longitude, double latitude){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,17 +3,23 @@ package fr.u_paris.gla.project.itinerary;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class Graph <T extends GraphNode> {
|
public class Graph{
|
||||||
private final Set<T> nodes;
|
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.nodes = nodes;
|
||||||
this.connections = connections;
|
this.connections = connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<T> getNodes() {
|
|
||||||
|
|
||||||
|
public Set<Connection> getConnections(Stop node) {
|
||||||
|
return connections.get(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Stop> getNodes() {
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,11 @@ package fr.u_paris.gla.project.itinerary;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public interface GraphNode<T>{
|
public interface GraphNode{
|
||||||
int getId();
|
int getId();
|
||||||
double getHeuristicCost(T goalNode);
|
double getHeuristicCost(Stop goalNode);
|
||||||
|
|
||||||
Set<T> getNeighbors();
|
Set<Stop> getNeighbors();
|
||||||
double getCost(T neighbor);
|
double getCost(Stop neighbor);
|
||||||
double getF();
|
double getF();
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ public class Stop implements GraphNode {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getHeuristicCost(Object goalNode) {
|
public double getHeuristicCost(Stop goalNode) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,9 +37,8 @@ public class Stop implements GraphNode {
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getCost(Object neighbor) {
|
public double getCost(Stop neighbor) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue