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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.util.Arrays;
  10. import org.insa.graphs.algorithm.ArcInspector;
  11. import org.insa.graphs.algorithm.ArcInspectorFactory;
  12. import org.insa.graphs.model.*;
  13. import org.insa.graphs.model.AccessRestrictions.AccessMode;
  14. import org.insa.graphs.model.RoadInformation.RoadType;
  15. import org.insa.graphs.model.io.BinaryGraphReader;
  16. import org.insa.graphs.model.io.BinaryPathReader;
  17. import org.insa.graphs.model.io.GraphReader;
  18. import org.insa.graphs.model.io.PathReader;
  19. import org.junit.BeforeClass;
  20. import org.junit.Test;
  21. public class DijkstraAlgorithmTest{
  22. //copié sur PathTest.java
  23. // Small graph use for tests
  24. private static Graph graph;
  25. // List of nodes
  26. private static Node[] nodes;
  27. // List of arcs in the graph, a2b is the arc from node A (0) to B (1).
  28. @SuppressWarnings("unused")
  29. private static Arc ab, ac, ad, bf, bd, cd, cg, df, de, de2, dh, ef, eg, eh, fg, gh;
  30. private static ShortestPathData dataal, datacl, dataat, datact, datapt, onenodata, emptydata, invalidata;
  31. // permet de savoir quels arcs autoriser(cf l.187 excel ggdoc)
  32. private static ArcInspector alllen = ArcInspectorFactory.getAllFilters().get(0);
  33. private static ArcInspector carlen = ArcInspectorFactory.getAllFilters().get(1);
  34. private static ArcInspector alltime = ArcInspectorFactory.getAllFilters().get(2);
  35. private static ArcInspector cartime = ArcInspectorFactory.getAllFilters().get(3);
  36. private static ArcInspector pietime = ArcInspectorFactory.getAllFilters().get(4);
  37. private static ShortestPathSolution dijkal, dijkcl, dijkat, dijkct, dijkpt, onenodijk, emptydijk, invalidijk;
  38. private static ShortestPathSolution bfaal, bfacl, bfaat, bfact, bfapt; //les BF et Dijkstra sur deux lignes différentes par souci de lisibilité
  39. @BeforeClass
  40. public static void initAll() throws IOException {
  41. RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, null, false, 36, ""),
  42. speed20 = new RoadInformation(RoadType.MOTORWAY, null, false, 72, ""),
  43. pietonable = new RoadInformation(RoadType.PEDESTRIAN, null, false, 5, "");
  44. //cyclable = new RoadInformation(RoadType.CYCLEWAY, null, false, 20, ""),
  45. //toutes les routes ici sont à double sens
  46. //visiblement un problème ave le deuxième argument null, comment gérer les restrictions?
  47. // Create nodes
  48. nodes = new Node[8];
  49. for (int i = 0; i < nodes.length; ++i) {
  50. nodes[i] = new Node(i, null);
  51. }
  52. // définition des arcs
  53. ab = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
  54. ac = Node.linkNodes(nodes[0], nodes[2], 50, speed20, null);
  55. ad = Node.linkNodes(nodes[0], nodes[3], 15, speed10, null);
  56. bd = Node.linkNodes(nodes[1], nodes[3], 15, speed20, null);
  57. bf = Node.linkNodes(nodes[1], nodes[5], 20, speed10, null);
  58. cd = Node.linkNodes(nodes[2], nodes[3], 10, speed20, null);
  59. cg = Node.linkNodes(nodes[2], nodes[6], 15, speed10, null);
  60. de = Node.linkNodes(nodes[3], nodes[4], 15, speed20, null);
  61. de2= Node.linkNodes(nodes[3], nodes[4], 25, pietonable, null);
  62. df = Node.linkNodes(nodes[3], nodes[5], 8, pietonable, null);
  63. dh = Node.linkNodes(nodes[3], nodes[7], 12, pietonable, null);
  64. ef = Node.linkNodes(nodes[4], nodes[5], 10, speed10, null);
  65. eg = Node.linkNodes(nodes[4], nodes[6], 10, speed20, null);
  66. eh = Node.linkNodes(nodes[4], nodes[7], 5, pietonable, null);
  67. fg = Node.linkNodes(nodes[5], nodes[6], 10, pietonable, null);
  68. gh = Node.linkNodes(nodes[6], nodes[7], 12, pietonable, null);
  69. graph = new Graph("ID", "", Arrays.asList(nodes), null);
  70. //initialisation des datas
  71. dataal= new ShortestPathData(graph, nodes[0], nodes[4], alllen);//a->e
  72. datacl= new ShortestPathData(graph, nodes[0], nodes[0], carlen);//b->g
  73. dataat= new ShortestPathData(graph, nodes[0], nodes[0], alltime);//h->b
  74. datact= new ShortestPathData(graph, nodes[0], nodes[0], cartime);//c->b
  75. datapt= new ShortestPathData(graph, nodes[5], nodes[0], pietime);//f->e
  76. onenodata=new ShortestPathData(graph, nodes[2], nodes[2], pietime);
  77. emptydata=new ShortestPathData(graph, null, null, pietime);
  78. invalidata=new ShortestPathData(graph, nodes[0], nodes[7], carlen);//h accessible uniquement aux piétons
  79. //initialisation des Dijkstras
  80. dijkal = (new DijkstraAlgorithm(dataal)).run();
  81. dijkcl = (new DijkstraAlgorithm(datacl)).run();
  82. dijkat = (new DijkstraAlgorithm(dataat)).run();
  83. dijkct = (new DijkstraAlgorithm(datact)).run();
  84. //dijkpt = (new DijkstraAlgorithm(datapt)).run();//erreur ici
  85. onenodijk = (new DijkstraAlgorithm(onenodata)).run();
  86. emptydijk = (new DijkstraAlgorithm(emptydata)).run();//erreur ici
  87. //invalidijk = (new DijkstraAlgorithm(invalidata)).run();//et erreur là
  88. //initialisation des Bellman-Ford pour comparaison
  89. bfaal = (new BellmanFordAlgorithm(dataal)).run();
  90. bfacl = (new BellmanFordAlgorithm(datacl)).run();
  91. /*bfaat = (new BellmanFordAlgorithm(dataat)).run();
  92. bfact = (new BellmanFordAlgorithm(datact)).run();
  93. bfapt = (new BellmanFordAlgorithm(datapt)).run();*/ //erreurs partout ici...
  94. }
  95. //créer plus et avec des maps différentes
  96. //faire des test spécifiques pour longs trajets
  97. /*(Test faits directement via la console et DijkstraAlgorithm.java, actuellement commentés:
  98. -coûts croissants
  99. -nbr successeurs cohérents
  100. -tas valide.)*/
  101. @Test
  102. public void cheminValide() {
  103. assertTrue(dijkal.getPath().isValid());
  104. assertTrue(dijkcl.getPath().isValid());
  105. assertTrue(dijkat.getPath().isValid());
  106. assertTrue(dijkct.getPath().isValid());
  107. //assertTrue(dijkpt.getPath().isValid());//pas normal
  108. assertTrue(onenodijk.getPath().isValid());
  109. assertTrue(emptydijk.getPath().isValid());
  110. //assertFalse(invalidijk.getPath().isValid());
  111. }
  112. @Test
  113. public void faisable() {
  114. assertTrue(dijkal.isFeasible());
  115. assertTrue(dijkcl.isFeasible());
  116. assertTrue(dijkat.isFeasible());
  117. assertTrue(dijkct.isFeasible());
  118. //assertTrue(dijkpt.isFeasible());
  119. assertTrue(onenodijk.isFeasible());
  120. assertFalse(emptydijk.isFeasible());
  121. //assertFalse(invalidijk.isFeasible());
  122. }
  123. //résultat identique à Bellman-Ford (sur les petits scénarios)
  124. @Test
  125. public void sameasBF() {
  126. assertTrue(Float.compare(dijkal.getPath().getLength(),bfaal.getPath().getLength())==0);
  127. //assertTrue(Float.compare(dijkcl.getPath().getLength(),bfacl.getPath().getLength())==0);
  128. //assertTrue(Double.compare(dijkat.getPath().getMinimumTravelTime(),bfaat.getPath().getMinimumTravelTime())==0);
  129. //assertTrue(Double.compare(dijkct.getPath().getMinimumTravelTime(),bfact.getPath().getMinimumTravelTime())==0);
  130. //assertTrue(Double.compare(dijkpt.getPath().getMinimumTravelTime(),bfapt.getPath().getMinimumTravelTime())==0);
  131. }
  132. //grands scénarios:
  133. @Test
  134. public void testmap() throws FileNotFoundException, IOException{//A FAIRE
  135. String mapaddr = "/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haute-garonne.mapgr";
  136. String pathaddr ="/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_aeroport_length.path";
  137. GraphReader graphread = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapaddr))));
  138. PathReader pathread = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathaddr))));
  139. Graph graphmap = graphread.read();
  140. Path pathmap = pathread.readPath(graphmap);//erreur ici mais pas mapmismatch, donc pourquoi? Path impossible? (hem)
  141. //quel inspector prendre? Le path est en longueur mais voitures seulement ou tout autorisé?
  142. DijkstraAlgorithm dijkmap = new DijkstraAlgorithm(new ShortestPathData(graphmap, pathmap.getOrigin(), pathmap.getDestination(), alllen));
  143. assertTrue(dijkmap.run().getPath().getLength()==pathmap.getLength());
  144. //comparaison de la longueur
  145. }
  146. }