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.

DijkstraAlgorithm_et_AStarTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 java.util.EnumMap;
  11. import org.insa.graphs.algorithm.ArcInspector;
  12. import org.insa.graphs.algorithm.ArcInspectorFactory;
  13. import org.insa.graphs.model.*;
  14. import org.insa.graphs.model.AccessRestrictions.AccessMode;
  15. import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
  16. import org.insa.graphs.model.RoadInformation.RoadType;
  17. import org.insa.graphs.model.io.BinaryGraphReader;
  18. import org.insa.graphs.model.io.BinaryPathReader;
  19. import org.insa.graphs.model.io.GraphReader;
  20. import org.insa.graphs.model.io.PathReader;
  21. import org.junit.BeforeClass;
  22. import org.junit.Test;
  23. public class DijkstraAlgorithm_et_AStarTest{
  24. //copié sur PathTest.java
  25. // Small graph use for tests
  26. private static Graph graph;
  27. // List of nodes
  28. private static Node[] nodes;
  29. // List of arcs in the graph, a2b is the arc from node A (0) to B (1).
  30. @SuppressWarnings("unused")
  31. private static Arc ab, ac, ad, bf, bd, cd, cg, df, de, de2, dh, ef, eg, eh, fg, gh;
  32. private static ShortestPathData dataal, datacl, dataat, datact, datapt, onenodata, emptydata, invalidata;
  33. // permet de savoir quels arcs autoriser(cf l.187 excel ggdoc)
  34. private static ArcInspector alllen = ArcInspectorFactory.getAllFilters().get(0);
  35. private static ArcInspector carlen = ArcInspectorFactory.getAllFilters().get(1);
  36. private static ArcInspector alltime = ArcInspectorFactory.getAllFilters().get(2);
  37. private static ArcInspector cartime = ArcInspectorFactory.getAllFilters().get(3);
  38. private static ArcInspector pietime = ArcInspectorFactory.getAllFilters().get(4);
  39. private static ShortestPathSolution dijkal, dijkcl, dijkat, dijkct, dijkpt, onenodijk, emptydijk, invalidijk;
  40. private static ShortestPathSolution bfaal, bfacl, bfaat, bfact, bfapt; //les BF, Dijkstra et A* sur trois lignes différentes par souci de lisibilité
  41. private static ShortestPathSolution asal, ascl, asat, asct, aspt, asonenod, asempty, asinvalid;
  42. @BeforeClass
  43. public static void initAll() throws IOException {
  44. //TODO: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHHH
  45. //rabbithole, mission annulée
  46. //sinon, utilisation de toAccessInformation(), à condition de savoir quel bit correspond à quoi...
  47. //gestion des restrictions pour pouvoir créer les Bellman-Ford
  48. AccessRestrictions voiture, pedestre;
  49. /*AccessRestrictions.AccessRestriction authorise = AccessRestrictions.AccessRestriction.ALLOWED;
  50. EnumMap<AccessMode, AccessRestrictions.AccessRestriction> pmap, vmap;
  51. pmap = null;//dkgfvbwslidfue djeflmmqff^dfomqf^sf652s894fd5swrtwedf+e
  52. vmap = null;//cette initalisation ne marche pas, COMMENT faire? ->toAccessInformation de voiture et pedestre sans passer par des EnumMap
  53. pmap.put(AccessMode.FOOT, authorise);
  54. //vmap.put(AccessMode.MOTORCAR, authorise);*/
  55. //bloc actuellement inutile
  56. voiture = new AccessRestrictions();//vmap en paramètre si possible
  57. pedestre = new AccessRestrictions();//pmap en paramètre si possible
  58. RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, voiture, false, 36, ""),
  59. speed20 = new RoadInformation(RoadType.MOTORWAY, voiture, false, 72, ""),
  60. pietonable = new RoadInformation(RoadType.PEDESTRIAN, pedestre, false, 5, "");
  61. //cyclable = new RoadInformation(RoadType.CYCLEWAY, null, false, 20, ""),
  62. //toutes les routes ici sont à double sens
  63. //attention, les piétons ont une maxspeed définie à 5
  64. // Create nodes
  65. nodes = new Node[9];
  66. for (int i = 0; i < nodes.length; ++i) {
  67. nodes[i] = new Node(i, null);
  68. }
  69. // définition des arcs (graphe custom)
  70. ab = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
  71. ac = Node.linkNodes(nodes[0], nodes[2], 50, speed20, null);
  72. ad = Node.linkNodes(nodes[0], nodes[3], 15, speed10, null);
  73. bd = Node.linkNodes(nodes[1], nodes[3], 15, speed20, null);
  74. bf = Node.linkNodes(nodes[1], nodes[5], 20, speed10, null);
  75. cd = Node.linkNodes(nodes[2], nodes[3], 10, speed20, null);
  76. cg = Node.linkNodes(nodes[2], nodes[6], 15, speed10, null);
  77. de = Node.linkNodes(nodes[3], nodes[4], 15, speed20, null);
  78. de2= Node.linkNodes(nodes[3], nodes[4], 25, pietonable, null);
  79. df = Node.linkNodes(nodes[3], nodes[5], 8, pietonable, null);
  80. dh = Node.linkNodes(nodes[3], nodes[7], 12, pietonable, null);
  81. ef = Node.linkNodes(nodes[4], nodes[5], 10, speed10, null);
  82. eg = Node.linkNodes(nodes[4], nodes[6], 10, speed20, null);
  83. eh = Node.linkNodes(nodes[4], nodes[7], 5, pietonable, null);
  84. fg = Node.linkNodes(nodes[5], nodes[6], 10, pietonable, null);
  85. gh = Node.linkNodes(nodes[6], nodes[7], 12, pietonable, null);
  86. graph = new Graph("ID", "", Arrays.asList(nodes), null);
  87. //initialisation des datas
  88. dataal= new ShortestPathData(graph, nodes[0], nodes[4], alllen);//a->e
  89. datacl= new ShortestPathData(graph, nodes[1], nodes[6], carlen);//b->g
  90. dataat= new ShortestPathData(graph, nodes[7], nodes[1], alltime);//h->b
  91. datact= new ShortestPathData(graph, nodes[2], nodes[1], cartime);//c->b
  92. datapt= new ShortestPathData(graph, nodes[5], nodes[4], pietime);//f->e
  93. onenodata=new ShortestPathData(graph, nodes[2], nodes[2], pietime);
  94. emptydata=new ShortestPathData(graph, null, null, pietime);
  95. invalidata=new ShortestPathData(graph, nodes[0], nodes[8], carlen);//h aurait dû être inaccessible aux voitures
  96. //pour compenser, ajout du node i non relié
  97. //initialisation des Dijkstras
  98. dijkal = (new DijkstraAlgorithm(dataal)).run();
  99. dijkcl = (new DijkstraAlgorithm(datacl)).run();
  100. dijkat = (new DijkstraAlgorithm(dataat)).run();
  101. dijkct = (new DijkstraAlgorithm(datact)).run();
  102. dijkpt = (new DijkstraAlgorithm(datapt)).run();
  103. onenodijk = (new DijkstraAlgorithm(onenodata)).run();
  104. emptydijk = (new DijkstraAlgorithm(emptydata)).run();
  105. invalidijk = (new DijkstraAlgorithm(invalidata)).run();
  106. //initialisation des A*
  107. asal = (new DijkstraAlgorithm(dataal)).run();
  108. ascl = (new DijkstraAlgorithm(datacl)).run();
  109. asat = (new DijkstraAlgorithm(dataat)).run();
  110. asct = (new DijkstraAlgorithm(datact)).run();
  111. aspt = (new DijkstraAlgorithm(datapt)).run();
  112. asonenod = (new DijkstraAlgorithm(onenodata)).run();
  113. asempty = (new DijkstraAlgorithm(emptydata)).run();
  114. asinvalid = (new DijkstraAlgorithm(invalidata)).run();
  115. //initialisation des Bellman-Ford pour comparaison
  116. bfaal = (new BellmanFordAlgorithm(dataal)).run();
  117. bfacl = (new BellmanFordAlgorithm(datacl)).run();
  118. bfaat = (new BellmanFordAlgorithm(dataat)).run();
  119. bfact = (new BellmanFordAlgorithm(datact)).run();
  120. bfapt = (new BellmanFordAlgorithm(datapt)).run();
  121. }
  122. /* ( Test faits directement via la console et DijkstraAlgorithm.java, actuellement commentés:
  123. - coûts croissants
  124. - nbr successeurs cohérents
  125. - tas valide. ) */
  126. @Test
  127. public void cheminValideD() {
  128. assertTrue(dijkal.getPath().isValid());
  129. assertTrue(dijkcl.getPath().isValid());
  130. assertTrue(dijkat.getPath().isValid());
  131. assertTrue(dijkct.getPath().isValid());
  132. assertTrue(dijkpt.getPath().isValid());
  133. assertTrue(onenodijk.getPath().isValid());
  134. assertTrue(emptydijk.getPath().isValid());
  135. assertTrue(invalidijk.getPath().isValid());
  136. }//note: invalidata ne peut pas trouver de chemin et contient ainsi un path null, donc valide
  137. @Test
  138. public void cheminValideA() {
  139. assertTrue(asal.getPath().isValid());
  140. assertTrue(ascl.getPath().isValid());
  141. assertTrue(asat.getPath().isValid());
  142. assertTrue(asct.getPath().isValid());
  143. assertTrue(aspt.getPath().isValid());
  144. assertTrue(asonenod.getPath().isValid());
  145. assertTrue(asempty.getPath().isValid());
  146. assertTrue(asinvalid.getPath().isValid());
  147. }
  148. @Test
  149. public void faisableD() {
  150. assertTrue(dijkal.isFeasible());
  151. assertTrue(dijkcl.isFeasible());
  152. assertTrue(dijkat.isFeasible());
  153. assertTrue(dijkct.isFeasible());
  154. assertTrue(dijkpt.isFeasible());
  155. assertTrue(onenodijk.isFeasible());
  156. assertFalse(emptydijk.isFeasible());
  157. assertFalse(invalidijk.isFeasible());
  158. }
  159. @Test
  160. public void faisableA() {
  161. assertTrue(asal.isFeasible());
  162. assertTrue(ascl.isFeasible());
  163. assertTrue(asat.isFeasible());
  164. assertTrue(asct.isFeasible());
  165. assertTrue(aspt.isFeasible());
  166. assertTrue(asonenod.isFeasible());
  167. assertFalse(asempty.isFeasible());
  168. assertFalse(asinvalid.isFeasible());
  169. }
  170. //résultat identique à Bellman-Ford (sur les petits scénarios)
  171. @Test
  172. public void DsameasBF() {
  173. assertTrue(Float.compare(dijkal.getPath().getLength(),bfaal.getPath().getLength())==0);
  174. assertTrue(Float.compare(dijkcl.getPath().getLength(),bfacl.getPath().getLength())==0);
  175. assertTrue(Double.compare(dijkat.getPath().getMinimumTravelTime(),bfaat.getPath().getMinimumTravelTime())==0);
  176. assertTrue(Double.compare(dijkct.getPath().getMinimumTravelTime(),bfact.getPath().getMinimumTravelTime())==0);
  177. assertTrue(Double.compare(dijkpt.getPath().getMinimumTravelTime(),bfapt.getPath().getMinimumTravelTime())==0);
  178. }
  179. @Test
  180. public void AsameasBF() {
  181. assertTrue(Float.compare(asal.getPath().getLength(),bfaal.getPath().getLength())==0);
  182. assertTrue(Float.compare(ascl.getPath().getLength(),bfacl.getPath().getLength())==0);
  183. assertTrue(Double.compare(asat.getPath().getMinimumTravelTime(),bfaat.getPath().getMinimumTravelTime())==0);
  184. assertTrue(Double.compare(asct.getPath().getMinimumTravelTime(),bfact.getPath().getMinimumTravelTime())==0);
  185. assertTrue(Double.compare(aspt.getPath().getMinimumTravelTime(),bfapt.getPath().getMinimumTravelTime())==0);
  186. }
  187. //grand scénario avec oracle:
  188. @Test
  189. public void testMap() throws FileNotFoundException, IOException{//A FAIRE
  190. String mapaddr = "/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haute-garonne.mapgr";
  191. String pathaddr ="/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_aeroport_length.path";
  192. GraphReader graphread = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapaddr))));
  193. PathReader pathread = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathaddr))));
  194. Graph graphmap = graphread.read();
  195. Path pathmap = pathread.readPath(graphmap);
  196. //après test, il faut prendre carlen et non alllen en ArcInspector (sinon A* et Dijkstra sont plus courts)
  197. DijkstraAlgorithm dijkmap = new DijkstraAlgorithm(new ShortestPathData(graphmap, pathmap.getOrigin(), pathmap.getDestination(), carlen));
  198. AStarAlgorithm asmap = new AStarAlgorithm(new ShortestPathData(graphmap, pathmap.getOrigin(), pathmap.getDestination(), carlen));
  199. assertTrue(dijkmap.run().getPath().getLength()==pathmap.getLength());
  200. assertTrue(asmap.run().getPath().getLength()==pathmap.getLength());
  201. //comparaison de la longueur
  202. }
  203. //long car la map bretagne (sans oracle possible) est assez importante
  204. @Test
  205. public void testMapNoOracle() throws FileNotFoundException, IOException{//A FAIRE
  206. String mapaddr = "/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/bretagne.mapgr";
  207. GraphReader graphread = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapaddr))));
  208. Graph graphmap = graphread.read();
  209. Node Origine = graphmap.getNodes().get(100000);//Brest
  210. Node Destination = graphmap.getNodes().get(6000);//Rennes
  211. //sans possibilité d'utiliser un pathmap, on fait une estimation arbitrairement basée sur la distance réelle
  212. float estimation=1.2f*(float) (Origine.getPoint().distanceTo(Destination.getPoint()));
  213. //1.2 très arbitraire, choisi car entre 1 (absurde) et sqrt(2)
  214. //ne peut pas être adapté à tous les trajets, mais ici le graphe est assez couvrant et le trajet assez long
  215. DijkstraAlgorithm dijkmap = new DijkstraAlgorithm(new ShortestPathData(graphmap, Origine, Destination, carlen));
  216. AStarAlgorithm asmap = new AStarAlgorithm(new ShortestPathData(graphmap, Origine, Destination, carlen));
  217. assertTrue(dijkmap.run().getPath().getLength()<estimation);
  218. assertTrue(asmap.run().getPath().getLength()<estimation);//<= car pas optimal sans oracle
  219. //comparaison de la longueur
  220. }
  221. }