AstarTEst

This commit is contained in:
Gasson-Betuing Danyl 2025-05-26 14:24:04 +02:00
parent 0e8f305f9a
commit d79ea53283
3 changed files with 121 additions and 82 deletions

View file

@ -47,12 +47,12 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
for(Node node : graph.getNodes()){ // Mode DISTANCE for(Node node : graph.getNodes()){ // Mode DISTANCE
nodeId = node.getId(); nodeId = node.getId();
Heuristicdistance = Point.distance(node.getPoint(), data.getDestination().getPoint()); // la distance estimé est calculé par la fonction distance (ligne droite entre les deux noeuds) Heuristicdistance = node.getPoint() != null ? Point.distance(node.getPoint(), data.getDestination().getPoint()):0; // 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); labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Heuristicdistance);
} }
} }
else if(graph.getGraphInformation().hasMaximumSpeed()){ // Mode TEMPS else if(graph.getGraphInformation() != null && graph.getGraphInformation().hasMaximumSpeed()){ // Mode TEMPS
maxSpeed = graph.getGraphInformation().getMaximumSpeed()/3.6; // en m/s maxSpeed = graph.getGraphInformation().getMaximumSpeed()/3.6; // en m/s
@ -60,7 +60,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
for(Node node : graph.getNodes()){ for(Node node : graph.getNodes()){
nodeId = node.getId(); nodeId = node.getId();
Heuristicdistance = Point.distance(node.getPoint(), data.getDestination().getPoint()); Heuristicdistance = node.getPoint() != null ? Point.distance(node.getPoint(), data.getDestination().getPoint()):0;
labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Heuristicdistance/maxSpeed); labels[nodeId] = new LabelStar(node, false, Double.POSITIVE_INFINITY, Heuristicdistance/maxSpeed);
} }
} }

View file

@ -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 AStarFullTest {
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] na 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);
AStarAlgorithm algo = new AStarAlgorithm(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(2); // "Fastest path"
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[3], inspector);
AStarAlgorithm algo = new AStarAlgorithm(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);
AStarAlgorithm algo = new AStarAlgorithm(data);
ShortestPathSolution sol = algo.run();
// Deux chemins possibles de 20m : ACD et ABD
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); // F D
AStarAlgorithm algo = new AStarAlgorithm(data);
ShortestPathSolution sol = algo.run();
assertFalse(sol.isFeasible());
assertEquals(Status.INFEASIBLE, sol.getStatus());
}
@Test
@SuppressWarnings("deprecation")
public void testSameOriginDestination() {
ArcInspector inspector = ArcInspectorFactory.getAllFilters().get(1);
ShortestPathData data = new ShortestPathData(graph, nodes[0], nodes[0], inspector); // A A
AStarAlgorithm algo = new AStarAlgorithm(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());
}
}

View file

@ -115,81 +115,3 @@ public class DijkstraFulltest {
//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.");
}
}
}
}
*/