No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DijkstraAlgorithmTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package org.insa.graphs.algorithm.shortestpath;
  2. import static org.junit.Assert.*;
  3. import org.insa.graphs.*;//voir si on ne peut pas importer launch autrement
  4. import java.io.BufferedInputStream;
  5. import java.io.DataInputStream;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import org.insa.graphs.algorithm.ArcInspector;
  11. import org.insa.graphs.algorithm.ArcInspectorFactory;
  12. import org.insa.graphs.algorithm.shortestpath.*;
  13. import org.insa.graphs.model.*;
  14. import org.insa.graphs.model.RoadInformation.RoadType;
  15. import org.junit.BeforeClass;
  16. import org.junit.Test;
  17. public class DijkstraAlgorithmTest{
  18. //copié sur PathTest.java
  19. // Small graph use for tests
  20. private static Graph graph;
  21. // List of nodes
  22. private static Node[] nodes;
  23. // List of arcs in the graph, a2b is the arc from node A (0) to B (1).
  24. @SuppressWarnings("unused")
  25. private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
  26. private static ShortestPathData dataal, datacl, dataat, datact, datapt, onenodata, emptydata, invalidata;
  27. // permet de savoir quels arcs autoriser(cf l.187 excel ggdoc)
  28. private static ArcInspector alllen = ArcInspectorFactory.getAllFilters().get(0);
  29. private static ArcInspector carlen = ArcInspectorFactory.getAllFilters().get(1);
  30. private static ArcInspector alltime = ArcInspectorFactory.getAllFilters().get(2);
  31. private static ArcInspector cartime = ArcInspectorFactory.getAllFilters().get(3);
  32. private static ArcInspector pietime = ArcInspectorFactory.getAllFilters().get(4);
  33. private static DijkstraAlgorithm dijkal, dijkcl, dijkat, dijkct, dijkpt, onenodijk, emptydijk, invalidijk;
  34. private static BellmanFordAlgorithm bfaal, bfacl, bfaat, bfact, bfapt;
  35. @BeforeClass
  36. public static void initAll() throws IOException {
  37. // 10 and 20 meters per seconds
  38. RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, null, true, 36, ""),
  39. speed20 = new RoadInformation(RoadType.MOTORWAY, null, true, 72, "");
  40. // Create nodes
  41. nodes = new Node[5];
  42. for (int i = 0; i < nodes.length; ++i) {
  43. nodes[i] = new Node(i, null);
  44. }
  45. // Add arcs...
  46. a2b = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
  47. a2c = Node.linkNodes(nodes[0], nodes[2], 15, speed10, null);
  48. a2e = Node.linkNodes(nodes[0], nodes[4], 15, speed20, null);
  49. b2c = Node.linkNodes(nodes[1], nodes[2], 10, speed10, null);
  50. c2d_1 = Node.linkNodes(nodes[2], nodes[3], 20, speed10, null);
  51. c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null);
  52. c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null);
  53. d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null);
  54. d2e = Node.linkNodes(nodes[3], nodes[4], 22.8f, speed20, null);
  55. e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null);
  56. graph = new Graph("ID", "", Arrays.asList(nodes), null);
  57. //initialisation des datas
  58. dataal= new ShortestPathData(graph, null, null, alllen);
  59. datacl= new ShortestPathData(graph, null, null, carlen);
  60. dataat= new ShortestPathData(graph, null, null, alltime);
  61. datact= new ShortestPathData(graph, null, null, cartime);
  62. datapt= new ShortestPathData(graph, null, null, pietime);
  63. onenodata=new ShortestPathData(graph, nodes[2], nodes[2], pietime);
  64. emptydata=new ShortestPathData(graph, null, null, pietime);
  65. invalidata=new ShortestPathData(graph, null, null, pietime);
  66. //initialisation des Dijkstras
  67. dijkal=new DijkstraAlgorithm(dataal);
  68. dijkcl=new DijkstraAlgorithm(datacl);
  69. dijkat=new DijkstraAlgorithm(dataat);
  70. dijkct=new DijkstraAlgorithm(datact);
  71. dijkpt=new DijkstraAlgorithm(datapt);
  72. onenodijk=new DijkstraAlgorithm(onenodata);
  73. emptydijk=new DijkstraAlgorithm(emptydata);
  74. invalidijk=new DijkstraAlgorithm(invalidata);
  75. //initialisation des Bellman-Ford pour comparaison
  76. bfaal=new BellmanFordAlgorithm(dataal);
  77. bfacl=new BellmanFordAlgorithm(datacl);
  78. bfaat=new BellmanFordAlgorithm(dataat);
  79. bfact=new BellmanFordAlgorithm(datact);
  80. bfapt=new BellmanFordAlgorithm(datapt);
  81. }
  82. //créer plus et avec des maps différentes
  83. //faire des test spécifiques pour longs trajets
  84. /*(Test faits directement via la console et DijkstraAlgorithm.java, actuellement commentés:
  85. -coûts croissants
  86. -nbr successeurs cohérents
  87. -tas valide.)*/
  88. @Test
  89. public void cheminValide() {
  90. assertTrue(dijkal.doRun().getPath().isValid());
  91. }
  92. @Test
  93. public void faisable() {
  94. assertTrue(dijkal.doRun().isFeasible());
  95. }
  96. //résultat identique à Bellman-Ford (sur les petits scénarios)
  97. @Test
  98. public void sameasBF() {
  99. assertTrue(Float.compare(dijkal.doRun().getPath().getLength(),bfaal.doRun().getPath().getLength())==0);
  100. }
  101. //tests applicables aussi pour des grands scénarios:
  102. //...
  103. //graph = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream("a")))).read;
  104. }