ajout d un fichier de test complet pour djikstra a partir d un graphe crée a la main : logique a revoir car certains tests ne consuisent pas aux resultats qu on attend de djikstra
This commit is contained in:
parent
640f2140ba
commit
3cd3eeec3f
1 changed files with 117 additions and 0 deletions
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue