fusion
This commit is contained in:
commit
60aa6e4c7a
4 changed files with 191 additions and 2 deletions
|
@ -104,7 +104,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
|
||||
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
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.insa.graphes.model;
|
||||
package org.insa.graphs.algorithm.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
|
@ -0,0 +1,117 @@
|
|||
package org.insa.graphs.algorithm.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.Arrays;
|
||||
//import java.util.Collections;
|
||||
|
||||
import org.insa.graphs.model.*;
|
||||
import org.insa.graphs.algorithm.shortestpath.*;
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
import org.insa.graphs.algorithm.ArcInspector;
|
||||
import org.insa.graphs.algorithm.ArcInspectorFactory;
|
||||
import org.insa.graphs.model.RoadInformation.RoadType;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DijkstraFulltest {
|
||||
|
||||
private static Graph graph;
|
||||
private static Node[] nodes;
|
||||
|
||||
// List of arcs in the graph, a2b is the arc from node A (0) to B (1).
|
||||
@SuppressWarnings("unused")
|
||||
private static Arc a2b, b2c, c2d, a2d, a2c, c2d2, b2d, a2a;
|
||||
|
||||
@BeforeClass
|
||||
public static void initAll() {
|
||||
RoadInformation fastRoad = new RoadInformation(RoadType.MOTORWAY, null, true, 360, "");
|
||||
RoadInformation normalRoad = new RoadInformation(RoadType.PRIMARY, null, true, 50, "");
|
||||
|
||||
// Create 6 nodes (0 = A, ..., 5 = E)
|
||||
nodes = new Node[6];
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
nodes[i] = new Node(i, null);
|
||||
}
|
||||
|
||||
// Arcs
|
||||
a2b = Node.linkNodes(nodes[0], nodes[1], 10, normalRoad, null); // A → B
|
||||
b2c = Node.linkNodes(nodes[1], nodes[2], 20, normalRoad, null); // B → C
|
||||
c2d = Node.linkNodes(nodes[2], nodes[3], 30, normalRoad, null); // C → D
|
||||
a2d = Node.linkNodes(nodes[0], nodes[3], 100, fastRoad, null); // A → D (direct, long mais rapide)
|
||||
a2c = Node.linkNodes(nodes[0], nodes[2], 15, normalRoad, null); // A → C
|
||||
c2d2 = Node.linkNodes(nodes[2], nodes[3], 5, normalRoad, null); // C → D (alternative courte)
|
||||
b2d = Node.linkNodes(nodes[1], nodes[3], 10, normalRoad, null); // B → D (chemin équivalent)
|
||||
// E = nodes[5] n’a aucune liaison
|
||||
a2a = Node.linkNodes(nodes[0], nodes[0], 0, normalRoad, null); // A → A (test vers soi-même)
|
||||
|
||||
graph = new Graph("test-graph", "", Arrays.asList(nodes), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testShortestPath() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0); // "Shortest path"
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[3], inspector);
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
||||
// Le chemin le plus court (A → C → D2 = 15 + 5 = 20m) ou (A → B → D = 10 + 10 = 20m)
|
||||
assertTrue(sol.isFeasible());
|
||||
assertEquals(20.0, sol.getPath().getLength(), 1e-6);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testFastestPath() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(1); // "Fastest path"
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[3], inspector);
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
||||
// A → D direct : 100m à 100m/s = 1s (vs A → B → D : 20m à 50km/h = 1.44s)
|
||||
assertTrue(sol.isFeasible());
|
||||
assertEquals(1.0, sol.getPath().getMinimumTravelTime(), 1e-2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testEquivalentPaths() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0); // Shortest
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[3], inspector);
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
||||
// Deux chemins possibles de 20m : A→C→D et A→B→D
|
||||
assertTrue(sol.isFeasible());
|
||||
assertEquals(20.0, sol.getPath().getLength(), 1e-6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoPath() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0);
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[5], nodes[3], inspector); // E → D
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
||||
assertFalse(sol.isFeasible());
|
||||
assertEquals(Status.INFEASIBLE, sol.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testSameOriginDestination() {
|
||||
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(0);
|
||||
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[0], inspector); // A → A
|
||||
DijkstraAlgorithm algo = new DijkstraAlgorithm(data);
|
||||
ShortestPathSolution sol = algo.run();
|
||||
|
||||
assertTrue(sol.isFeasible());
|
||||
assertEquals(0.0, sol.getPath().getLength(), 1e-6);
|
||||
assertEquals(nodes[0], sol.getPath().getOrigin());
|
||||
assertEquals(nodes[0], sol.getPath().getDestination());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
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