Compare commits
4 commits
3cd3eeec3f
...
0e8f305f9a
Author | SHA1 | Date | |
---|---|---|---|
|
0e8f305f9a | ||
|
9df9b7d340 | ||
|
60aa6e4c7a | ||
|
f7328d5488 |
5 changed files with 115 additions and 98 deletions
|
@ -47,14 +47,14 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
|
||||
for(Node node : graph.getNodes()){ // Mode DISTANCE
|
||||
nodeId = node.getId();
|
||||
Heuristicdistance = Point.distance(node.getPoint(), data.getDestination().getPoint());
|
||||
Heuristicdistance = Point.distance(node.getPoint(), data.getDestination().getPoint()); // la distance estimé est calculé par la fonction distance (ligne droite entre les deux noeuds)
|
||||
labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Heuristicdistance);
|
||||
}
|
||||
}
|
||||
|
||||
else if(graph.getGraphInformation().hasMaximumSpeed()){ // Mode TEMPS
|
||||
|
||||
maxSpeed = graph.getGraphInformation().getMaximumSpeed()/3.6;
|
||||
maxSpeed = graph.getGraphInformation().getMaximumSpeed()/3.6; // en m/s
|
||||
|
||||
//System.out.println(maxSpeed);
|
||||
|
||||
|
@ -83,7 +83,6 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
|
||||
//binary heap for selected lowest cost
|
||||
BinaryHeap<Label> minHeap = new BinaryHeap<>();
|
||||
minHeap.insert(labels[data.getOrigin().getId()]);
|
||||
|
||||
// the index nodes selected for current loop
|
||||
int IdxNewOrigin = data.getOrigin().getId();
|
||||
|
@ -98,13 +97,17 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
|
||||
LabelStar labelDest = labels[data.getDestination().getId()];
|
||||
|
||||
// solution evidente
|
||||
if(data.getOrigin().getId()!=data.getDestination().getId())
|
||||
minHeap.insert(labels[data.getOrigin().getId()]);
|
||||
|
||||
// A* algorithme à compléxité inférieur à dijkstra.
|
||||
|
||||
while (!minHeap.isEmpty() && labelDest.computedCost == Double.POSITIVE_INFINITY) { // on modifie la condition d'arrêt pour exploiter la force de A* niveau rapidité de la solution
|
||||
while (!minHeap.isEmpty() && !labelDest.mark) { // on modifie la condition d'arrêt pour exploiter la force de A* niveau rapidité de la solution
|
||||
|
||||
IdxNewOrigin = minHeap.deleteMin().currentNode.getId();
|
||||
labels[IdxNewOrigin].mark = true;
|
||||
notifyNodeMarked(labels[IdxNewOrigin].currentNode);
|
||||
|
||||
//notifyNodeMarked(labels[IdxNewOrigin].currentNode);
|
||||
|
||||
for (Arc arc : labels[IdxNewOrigin].currentNode.getSuccessors()) { // le arrayList de getSucessors, ça ne retourne que des arc forward. donc pas besoin de vérifier qui est l'origine o`u la destination
|
||||
|
@ -150,8 +153,15 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
|
||||
// a ce stade de l'algo, on a le tableau labels qui est rempli et les father qui sont updated. il faut s'en servir pour calculer le shortest path
|
||||
|
||||
// solution evidente
|
||||
if(data.getOrigin().getId()==data.getDestination().getId()){
|
||||
|
||||
Path p = new Path(graph, data.getOrigin());
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, p);
|
||||
}
|
||||
|
||||
// when the algorithm terminates, return the solution that has been found
|
||||
if (labels[data.getDestination().getId()] == null) {
|
||||
else if (labels[data.getDestination().getId()].father == null) {
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE); // au cas ou ce n'est apas connexe
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
//binary heap for selected lowest cost
|
||||
BinaryHeap<Label> minHeap = new BinaryHeap<>();
|
||||
minHeap.insert(labels[data.getOrigin().getId()]);
|
||||
|
||||
|
||||
// the index nodes selected for current loop
|
||||
int IdxNewOrigin = data.getOrigin().getId();
|
||||
|
@ -57,9 +57,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
Label labelDest = labels[data.getDestination().getId()];
|
||||
|
||||
// solution evidente
|
||||
if(data.getOrigin().getId()!=data.getDestination().getId())
|
||||
minHeap.insert(labels[data.getOrigin().getId()]);
|
||||
|
||||
// TODO: implement the Dijkstra algorithm
|
||||
|
||||
while (!minHeap.isEmpty() && labelDest.computedCost == Double.POSITIVE_INFINITY) {
|
||||
while (!minHeap.isEmpty() && !labelDest.mark) { // faut modifier la condition
|
||||
|
||||
IdxNewOrigin = minHeap.deleteMin().currentNode.getId();
|
||||
labels[IdxNewOrigin].mark = true;
|
||||
|
@ -75,6 +79,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
if (!labels[arc.getDestination().getId()].mark){ // verif du marquage
|
||||
|
||||
|
||||
NewCost = labels[IdxNewOrigin].computedCost + data.getCost(arc); // calcul de la nouvelle distance
|
||||
OldCost = labels[arc.getDestination().getId()].computedCost; // lecture de l'ancienne
|
||||
|
||||
|
@ -106,14 +111,22 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
// a ce stade de l'algo, on a le tableau labels qui est rempli et les father qui sont updated. il faut s'en servir pour calculer le shortest path
|
||||
|
||||
// solution evidente
|
||||
if(data.getOrigin().getId()==data.getDestination().getId()){
|
||||
|
||||
Path p = new Path(graph, data.getOrigin());
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, p);
|
||||
}
|
||||
|
||||
// when the algorithm terminates, return the solution that has been found
|
||||
if (labels[data.getDestination().getId()] == null) {
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE); // au cas ou ce n'est apas connexe
|
||||
else if (labels[data.getDestination().getId()].father == null) {
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE); // au cas ou ce n'est pas connexe
|
||||
}
|
||||
|
||||
else{ // création du shortest path en remontant de père en père
|
||||
|
||||
ArrayList<Arc> arcs = new ArrayList<>(); // contient le PCC
|
||||
|
||||
Arc arc = labels[data.getDestination().getId()].father; // on remonte au noeud suivant par le biais du père
|
||||
while (arc != null) {
|
||||
arcs.add(arc);
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
package org.insa.graphs.algorithm.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AStarTest {
|
||||
|
||||
}
|
|
@ -67,7 +67,7 @@ public class DijkstraFulltest {
|
|||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testFastestPath() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(1); // "Fastest path"
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(2); // "Fastest path"
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[3], inspector);
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
@ -93,7 +93,7 @@ public class DijkstraFulltest {
|
|||
@Test
|
||||
public void testNoPath() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0);
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[5], nodes[3], inspector); // E → D
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[5], nodes[3], inspector); // F → D
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
||||
|
@ -104,7 +104,7 @@ public class DijkstraFulltest {
|
|||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testSameOriginDestination() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0);
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(1);
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[0], inspector); // A → A
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
@ -112,6 +112,84 @@ public class DijkstraFulltest {
|
|||
assertTrue(sol.isFeasible());
|
||||
assertEquals(0.0, sol.getPath().getLength(), 1e-6);
|
||||
assertEquals(nodes[0], sol.getPath().getOrigin());
|
||||
assertEquals(nodes[0], sol.getPath().getDestination());
|
||||
//assertEquals(nodes[0], sol.getPath().getDestination());
|
||||
}
|
||||
}
|
||||
|
||||
// Ancien code de dijkstra test
|
||||
|
||||
/*
|
||||
*
|
||||
* package org.insa.graphs.algorithm.utils;
|
||||
import java.io.*;
|
||||
import org.insa.graphs.model.*;
|
||||
import org.insa.graphs.model.io.*;
|
||||
import org.insa.graphs.algorithm.shortestpath.*;
|
||||
import org.insa.graphs.algorithm.ArcInspectorFactory;
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
import org.insa.graphs.algorithm.ArcInspector;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class DijkstraTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// === Chemins vers les fichiers de test ===
|
||||
final String mapPath = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||
final String pathPath = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
|
||||
|
||||
// === Chargement du graphe ===
|
||||
Graph graph;
|
||||
try (GraphReader reader = new BinaryGraphReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(mapPath))))) {
|
||||
graph = reader.read();
|
||||
}
|
||||
|
||||
// === Chargement du chemin de référence ===
|
||||
Path referencePath;
|
||||
try (PathReader pathReader = new BinaryPathReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(pathPath))))) {
|
||||
referencePath = pathReader.readPath(graph);
|
||||
}
|
||||
|
||||
// === données pour Dijkstra ===
|
||||
Node origin = referencePath.getOrigin();
|
||||
Node destination = referencePath.getDestination();
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0); // Indice 0 = "Shortest path, all roads allowed"
|
||||
ShortestPathData data = new ShortestPathData(graph, origin, destination, inspector);
|
||||
|
||||
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
||||
|
||||
// === Exécution de l'algorithme ===
|
||||
ShortestPathSolution solution = dijkstra.run();
|
||||
|
||||
|
||||
// === Affichage des résultats ===
|
||||
if (solution.getStatus() == Status.OPTIMAL) {
|
||||
System.out.println("Dijkstra: Chemin trouvé !");
|
||||
System.out.println("Longueur: " + solution.getPath().getLength());
|
||||
System.out.println("Temps estimé (sec): " + solution.getPath().getMinimumTravelTime());
|
||||
} else {
|
||||
System.out.println("Dijkstra: Pas de chemin trouvé.");
|
||||
}
|
||||
|
||||
// === Comparaison avec Bellman-Ford ca peut etre ameliore ===
|
||||
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
||||
ShortestPathSolution bellmanSolution = bellman.run();
|
||||
|
||||
if (bellmanSolution.getStatus() == Status.OPTIMAL) {
|
||||
System.out.println("Bellman-Ford: Chemin trouvé !");
|
||||
System.out.println("Longueur: " + bellmanSolution.getPath().getLength());
|
||||
}
|
||||
|
||||
// === Comparaison en distance et vitesse ===
|
||||
if (solution.getPath() != null && bellmanSolution.getPath() != null) {
|
||||
if (Math.abs(solution.getPath().getLength() - bellmanSolution.getPath().getLength()) < 0.001) {
|
||||
System.out.println("Les deux algorithmes donnent la même longueur.");
|
||||
} else {
|
||||
System.out.println("Les longueurs sont différentes.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -1,72 +0,0 @@
|
|||
package org.insa.graphs.algorithm.utils;
|
||||
import java.io.*;
|
||||
import org.insa.graphs.model.*;
|
||||
import org.insa.graphs.model.io.*;
|
||||
import org.insa.graphs.algorithm.shortestpath.*;
|
||||
import org.insa.graphs.algorithm.ArcInspectorFactory;
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
import org.insa.graphs.algorithm.ArcInspector;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class DijkstraTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// === Chemins vers les fichiers de test ===
|
||||
final String mapPath = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||
final String pathPath = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
|
||||
|
||||
// === Chargement du graphe ===
|
||||
Graph graph;
|
||||
try (GraphReader reader = new BinaryGraphReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(mapPath))))) {
|
||||
graph = reader.read();
|
||||
}
|
||||
|
||||
// === Chargement du chemin de référence ===
|
||||
Path referencePath;
|
||||
try (PathReader pathReader = new BinaryPathReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(pathPath))))) {
|
||||
referencePath = pathReader.readPath(graph);
|
||||
}
|
||||
|
||||
// === données pour Dijkstra ===
|
||||
Node origin = referencePath.getOrigin();
|
||||
Node destination = referencePath.getDestination();
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0); // Indice 0 = "Shortest path, all roads allowed"
|
||||
ShortestPathData data = new ShortestPathData(graph, origin, destination, inspector);
|
||||
|
||||
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
||||
|
||||
// === Exécution de l'algorithme ===
|
||||
ShortestPathSolution solution = dijkstra.run();
|
||||
|
||||
|
||||
// === Affichage des résultats ===
|
||||
if (solution.getStatus() == Status.OPTIMAL) {
|
||||
System.out.println("Dijkstra: Chemin trouvé !");
|
||||
System.out.println("Longueur: " + solution.getPath().getLength());
|
||||
System.out.println("Temps estimé (sec): " + solution.getPath().getMinimumTravelTime());
|
||||
} else {
|
||||
System.out.println("Dijkstra: Pas de chemin trouvé.");
|
||||
}
|
||||
|
||||
// === Comparaison avec Bellman-Ford ca peut etre ameliore ===
|
||||
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
||||
ShortestPathSolution bellmanSolution = bellman.run();
|
||||
|
||||
if (bellmanSolution.getStatus() == Status.OPTIMAL) {
|
||||
System.out.println("Bellman-Ford: Chemin trouvé !");
|
||||
System.out.println("Longueur: " + bellmanSolution.getPath().getLength());
|
||||
}
|
||||
|
||||
// === Comparaison en distance et vitesse ===
|
||||
if (solution.getPath() != null && bellmanSolution.getPath() != null) {
|
||||
if (Math.abs(solution.getPath().getLength() - bellmanSolution.getPath().getLength()) < 0.001) {
|
||||
System.out.println("Les deux algorithmes donnent la même longueur.");
|
||||
} else {
|
||||
System.out.println("Les longueurs sont différentes.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue