Mon répertoire pour le bureau d'étude graphes de 3MIC à l'INSA de Toulouse
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.

PccAlgorithmTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package org.insa.graphs.algorithm.utils;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import org.junit.Test;
  5. import java.io.BufferedInputStream;
  6. import java.io.DataInputStream;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.util.List;
  10. import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
  11. import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
  12. import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
  13. import org.insa.graphs.algorithm.ArcInspector;
  14. import org.insa.graphs.algorithm.ArcInspectorFactory;
  15. import org.insa.graphs.algorithm.AbstractInputData.Mode;
  16. import org.insa.graphs.algorithm.AbstractSolution.*;
  17. import org.insa.graphs.model.Graph;
  18. import org.insa.graphs.model.Arc;
  19. import org.insa.graphs.model.Node;
  20. import org.insa.graphs.model.Path;
  21. import org.insa.graphs.model.io.BinaryGraphReader;
  22. import org.insa.graphs.model.io.BinaryPathReader;
  23. import org.insa.graphs.model.io.GraphReader;
  24. import org.insa.graphs.model.io.PathReader;
  25. public abstract class PccAlgorithmTest {
  26. // Oracle (généré à partir du Bellman Ford)
  27. String hauteGaronneFileName = "./Maps/haute-garonne.mapgr";
  28. String lfboInsaPathName = "./Paths/LFBO_INSA_correct.path";
  29. Graph hauteGaronne;
  30. Path LfboInsa;
  31. // Outils
  32. PathReader pathReader;
  33. ArcInspector inspector;
  34. // Solution trouvée par l'algorithme donné pour le chemin LFBO - INSA
  35. ShortestPathSolution solutionTrouveeLfboInsa;
  36. class Scenario {
  37. Graph graph;
  38. Mode nc;
  39. Node origine;
  40. Node destination;
  41. public Scenario(Graph graph, Mode nc, Node origine, Node destination) {
  42. this.graph = graph;
  43. this.origine = origine;
  44. this.destination = destination;
  45. this.nc = nc;
  46. }
  47. }
  48. protected abstract ShortestPathAlgorithm createAlgorithm(ShortestPathData data);
  49. /**
  50. * Fonction utilitaire, test un scenario donné
  51. * @param scenario
  52. * @return
  53. */
  54. public ShortestPathSolution testScenario(Scenario scenario) {
  55. // Préparation des données
  56. ShortestPathData data = new ShortestPathData(scenario.graph, scenario.origine, scenario.destination, inspector);
  57. // Préparation de l'algorithme
  58. ShortestPathAlgorithm algo = createAlgorithm(data);
  59. // Lancement de l'algorithme
  60. return algo.run();
  61. }
  62. /**
  63. * Fonction utilitaire, fait calculer le chemin LFBO - INSA à l'algorithme donné
  64. * @return
  65. */
  66. public ShortestPathSolution doLFBO_INSA() {
  67. // Récupération du chemin
  68. Path correctPath = LfboInsa;
  69. // Création du scénario
  70. Scenario ScnLfboInsa = new Scenario(hauteGaronne, Mode.LENGTH, correctPath.getOrigin(), correctPath.getDestination());
  71. return testScenario(ScnLfboInsa);
  72. }
  73. /**
  74. * Constructeur, lit les fichiers liés à l'oracle et calcul LFBO - INSA
  75. * @throws IOException
  76. */
  77. public PccAlgorithmTest() throws IOException {
  78. inspector = ArcInspectorFactory.getAllFilters().get(0);
  79. GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(hauteGaronneFileName))));
  80. hauteGaronne = reader.read();
  81. pathReader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(lfboInsaPathName))));
  82. LfboInsa = pathReader.readPath(hauteGaronne);
  83. solutionTrouveeLfboInsa = doLFBO_INSA();
  84. }
  85. @Test
  86. public void testLFBO_INSA() {
  87. Path pathTrouve = solutionTrouveeLfboInsa.getPath();
  88. // Test de la solution trouvée
  89. assertTrue(solutionTrouveeLfboInsa.isFeasible());
  90. assertEquals(solutionTrouveeLfboInsa.getStatus(), Status.OPTIMAL);
  91. assertEquals(pathTrouve.getOrigin(), LfboInsa.getOrigin());
  92. assertEquals(pathTrouve.getDestination(), LfboInsa.getDestination());
  93. assertEquals(pathTrouve.getLength(), LfboInsa.getLength(), 0.01);
  94. }
  95. @Test
  96. public void testOneNodePath() {
  97. Node onlyOne = LfboInsa.getOrigin();
  98. // Création du scénario
  99. Scenario ScnOneNode = new Scenario(hauteGaronne, Mode.LENGTH, onlyOne, onlyOne);
  100. // Test du scénario
  101. ShortestPathSolution solutionTrouvee = testScenario(ScnOneNode);
  102. // Test de la solution trouvée
  103. assertTrue(solutionTrouvee.isFeasible());
  104. assertEquals(solutionTrouvee.getStatus(), Status.OPTIMAL);
  105. assertEquals(solutionTrouvee.getPath().getLength(), 0.0, 0.01);
  106. }
  107. /**
  108. * Il n'y a pas beaucoup de solution pour vérifier l'optimalité d'un chemin sans oracle.
  109. * On peut cependant vérifier que les sous-chemin sont également optimaux.
  110. * Note : La meilleure solution reste de démontrer l'optimalité du A* et de prendre
  111. * une heurestique admissible.
  112. */
  113. @Test
  114. public void testSousChemin() {
  115. Path pathTrouve = solutionTrouveeLfboInsa.getPath();
  116. Node n = pathTrouve.getOrigin();
  117. Node m = pathTrouve.getArcs().get(30).getDestination();
  118. Scenario ScnSousChemin = new Scenario(pathTrouve.getGraph(), Mode.LENGTH, n, m);
  119. ShortestPathSolution sol = testScenario(ScnSousChemin);
  120. List<Arc> arcsCheminOriginal = pathTrouve.getArcs();
  121. List<Arc> arcsSousChemin = sol.getPath().getArcs();
  122. for (int i = 0; i < 30; ++i) {
  123. assertTrue(arcsCheminOriginal.get(i) == arcsSousChemin.get(i));
  124. }
  125. }
  126. /**
  127. * Ce test permet de vérifier que la distance à vol d'oiseau est toujours plus faible
  128. * que la longueur du chemin retourné par l'algorithme
  129. */
  130. @Test
  131. public void testVolDOiseau() {
  132. Path pathTrouve = solutionTrouveeLfboInsa.getPath();
  133. double dx = LfboInsa.getOrigin().getPoint().getLongitude() - LfboInsa.getDestination().getPoint().getLongitude();
  134. double dy = LfboInsa.getOrigin().getPoint().getLatitude() - LfboInsa.getDestination().getPoint().getLatitude();
  135. assertTrue(pathTrouve.getLength() >= Math.sqrt(dx * dx + dy * dy));
  136. }
  137. @Test
  138. public void testInegaliteTriangulaire() {
  139. // Récupération d'un point légèrement dé-axé
  140. Node pointDePassage = LfboInsa.getOrigin().getSuccessors().get(0).getDestination().getSuccessors().get(0).getDestination().getSuccessors().get(0).getDestination();
  141. // Récupération de la solution directe
  142. Path pathTrouve = solutionTrouveeLfboInsa.getPath();
  143. Scenario Scn1 = new Scenario(LfboInsa.getGraph(), Mode.LENGTH, pathTrouve.getOrigin(), pointDePassage);
  144. Scenario Scn2 = new Scenario(LfboInsa.getGraph(), Mode.LENGTH, pointDePassage, pathTrouve.getDestination());
  145. ShortestPathSolution sol1 = testScenario(Scn1);
  146. ShortestPathSolution sol2 = testScenario(Scn2);
  147. // Vérification de l'inégalité triangulaire
  148. assertTrue(sol1.getPath().getLength() + sol2.getPath().getLength() >= pathTrouve.getLength());
  149. }
  150. }