这个提交包含在:
Matteo Sabben 2025-05-20 10:48:33 +02:00
当前提交 2835497c61
共有 2 个文件被更改,包括 28 次插入19 次删除

查看文件

@ -11,8 +11,8 @@ import javax.swing.SwingUtilities;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.MyArcInspector;
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
@ -24,9 +24,7 @@ import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.BinaryPathReader;
import org.insa.graphs.model.io.GraphReader;
import org.insa.graphs.model.io.PathReader;
public class TestDijkstra {
@ -48,7 +46,7 @@ public class TestDijkstra {
}
// fonction pour tester un scénario
public static void testScenario(String map, int depart, int arrivee, Mode mode, boolean petitGraphe) throws Exception {
public static void testScenario(String map, int depart, int arrivee, Mode mode, boolean petitGraphe, boolean Astar) throws Exception {
final Graph graph;
final Path path;
int maxSpeed = 130; // La vitesse maximale en km/h
@ -68,14 +66,21 @@ public class TestDijkstra {
ArcInspector arcInspector = new MyArcInspector(mode, maxSpeed);
ShortestPathData data = new ShortestPathData(graph, origin, destination, arcInspector);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
DijkstraAlgorithm dijkstra;
if (Astar) {
dijkstra = new AStarAlgorithm(data);
} else {
dijkstra = new DijkstraAlgorithm(data);
}
ShortestPathSolution solution = dijkstra.run();
if (solution.getStatus() == ShortestPathSolution.Status.OPTIMAL) {
path = solution.getPath();
// Vérif validité du chemin
boolean valid = path.isValid();
System.out.println("Chemin Dijkstra valide ? " + valid);
System.out.println("Chemin Algo valide ? " + valid);
// Récupérer la liste des noeuds du chemin trouvé
java.util.List<Node> nodeList = new java.util.ArrayList<>();
@ -101,14 +106,18 @@ public class TestDijkstra {
coutPath = pathFromNodes.getLength();
}
System.out.println("Coût Dijkstra: " + coutAlgo);
if (Astar){
System.out.println("Coût DijkstraAstar: " + coutAlgo);
}else{
System.out.println("Coût Dijkstra: " + coutAlgo);
}
boolean coutOk = Math.abs(coutAlgo - coutPath) < 1e-6; // pour comparer des réels
if(coutOk){
System.out.println("Coût Dijkstra vs Path: " + coutAlgo + " vs " + coutPath + " => OK");
System.out.println("Coût Algo vs Path: " + coutAlgo + " vs " + coutPath + " => OK");
} else {
System.out.println("Coût Dijkstra vs Path: " + coutAlgo + " vs " + coutPath + " => (différence attendue)");
System.out.println("Coût Algo vs Path: " + coutAlgo + " vs " + coutPath + " => (différence attendue)");
}
// Pour petits graphes, comparaison Bellman-Ford
@ -116,7 +125,7 @@ public class TestDijkstra {
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
ShortestPathSolution solBellman = bellman.run();
boolean memeStatut = solution.getStatus() == solBellman.getStatus();
System.out.println("Statut Dijkstra == Bellman-Ford ? " + memeStatut);
System.out.println("Statut Algo == Bellman-Ford ? " + memeStatut);
if (solBellman.getPath() != null) {
double coutBellman;
@ -128,9 +137,9 @@ public class TestDijkstra {
boolean memeCout = Math.abs(coutAlgo - coutBellman) < 1e-6; //méthode pour comparer des floats.
if(memeCout){
System.out.println("Coût Dijkstra == Bellman-Ford ? OK");
System.out.println("Coût Algo == Bellman-Ford ? OK");
} else {
System.out.println("Coût Dijkstra == Bellman-Ford ? (différence entre Bellman et Dijkstra.)");
System.out.println("Coût Algo == Bellman-Ford ? (différence présente entre Bellman et Algo.)");
}
}
}
@ -149,20 +158,20 @@ public class TestDijkstra {
public static void main(String[] args) throws Exception {
//cartes non routières
System.out.println("== Chemin de longueur nulle ==");
testScenario("carre.mapgr", 9, 9, Mode.LENGTH, true);
testScenario("carre.mapgr", 9, 9, Mode.LENGTH, true,false);
System.out.println("== Sommet hors du graphe ==");
testScenario("carre.mapgr", 0, 9999, Mode.LENGTH, true);
testScenario("carre.mapgr", 0, 9999, Mode.LENGTH, true,false);
//cartes routières
System.out.println("== Test en distance ==");
testScenario("insa.mapgr", 369, 838, Mode.LENGTH, true);
testScenario("insa.mapgr", 369, 838, Mode.LENGTH, true,false);
System.out.println("== Test en temps ==");
testScenario("insa.mapgr", 369, 838, Mode.TIME, true);
testScenario("insa.mapgr", 369, 838, Mode.TIME, true,false);
// autres scénarios
//System.out.println("== Trajet long (et pénible avec les enfants) ==");
// testScenario("bretagne.mapgr",48233,135047 , Mode.TIME, false);
System.out.println("== Trajet long (et pénible avec les enfants) ==");
testScenario("bretagne.mapgr",48233,135047 , Mode.TIME, false,false);
}
}