Projet_Graphe/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarFullTest.java
Gasson-Betuing Danyl d79ea53283 AstarTEst
2025-05-26 14:24:04 +02:00

117 строки
Без EOL
4,8 КиБ
Java
Исходный Авторство История

Этот файл содержит неоднозначные символы Юникода

Этот файл содержит символы Юникода, которые легко спутать с похожими. Если так и должно быть, можете спокойно игнорировать это предупреждение. Отобразить символы можно кнопкой Экранирования.

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 : 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); // 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());
}
}