420 Zeilen
18 KiB
Java
420 Zeilen
18 KiB
Java
package org.insa.graphs.algorithm.shortestpath;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.DataInputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
|
|
import org.insa.graphs.algorithm.ArcInspector;
|
|
import org.insa.graphs.algorithm.ArcInspectorFactory;
|
|
import org.insa.graphs.model.Graph;
|
|
import org.insa.graphs.model.Node;
|
|
import org.insa.graphs.model.Path;
|
|
import org.insa.graphs.model.io.BinaryGraphReader;
|
|
import org.insa.graphs.model.io.GraphReader;
|
|
import org.insa.graphs.model.io.PathReader;
|
|
import org.junit.BeforeClass;
|
|
import org.junit.Test;
|
|
import org.junit.runners.Parameterized.Parameter;
|
|
|
|
public class DijkstraAlgorithmTest {
|
|
|
|
// TODO finish this
|
|
public static GraphReader reader;
|
|
public static ArrayList<Graph> graph = new ArrayList<Graph>();
|
|
public static PathReader pathReader;
|
|
public static ArrayList<Path> path = new ArrayList<Path>();
|
|
|
|
@Parameter
|
|
// Liste de cartes utilisées
|
|
static ArrayList<String> Maps = new ArrayList<String>(Arrays.asList("../Maps/carre.mapgr",
|
|
"../Maps/insa.mapgr",
|
|
"../Maps/toulouse.mapgr",
|
|
"../Maps/midi-pyrenees.mapgr"));
|
|
|
|
@BeforeClass
|
|
/*
|
|
* Ajoute toute les cartes dans l'attribut graph.
|
|
* Ensuite, on testera des chemins dont qui sont disponibles dans custom_path.
|
|
* Ces chemins ont été tracés avec l'algo BellmanFord et sont donc considérés corrects.
|
|
* On ne crée pas les chemins dans un attribut, on va les "recréer" virtuellement à
|
|
* partir de leur origine/destination et en appliquant BellmanFord.
|
|
* Cela permet une meilleure adaptabilité du code.
|
|
*/
|
|
public static void init() {
|
|
try {
|
|
ArrayList<String> actual_path_list = new ArrayList<String>();
|
|
// Create the map
|
|
for (int j = 0 ; j < Maps.size() ; j++) {
|
|
final String mapName = Maps.get(j);
|
|
// Create a graph reader
|
|
final GraphReader reader = new BinaryGraphReader(
|
|
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
|
|
// Read the graph. X
|
|
graph.add(reader.read());
|
|
// free resources
|
|
reader.close();
|
|
}
|
|
}
|
|
catch (FileNotFoundException e) {
|
|
System.err.println("File not found: " + e.getMessage());
|
|
fail("File not found: " + e.getMessage());
|
|
}
|
|
catch (IOException e ) {
|
|
System.err.println("Error reading file: " + e.getMessage());
|
|
fail("Error reading file: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/* Stratégie:
|
|
* Chemins courts testés par comparaison avec Bellman.
|
|
* Chemin longs testés par comparaison avec chemins déjà construits. Bellman trop long.
|
|
*/
|
|
// TODO
|
|
|
|
@Test
|
|
/*
|
|
* Map: carre.mapgr
|
|
* Chemin: 19 --> 4
|
|
* Tous chemins permis
|
|
* Mode: LENGTH
|
|
* PATH UTILISE : ../Paths/custom_paths/short_path_carre.path
|
|
*/
|
|
public void chemin_court_CARRE_length() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(0);
|
|
Node origin = myGraph.get(19);
|
|
Node destination = myGraph.get(4);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
|
ShortestPathSolution bell_path = bellman.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
|
|
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Chemin long relativement à la carte carrée.
|
|
* Chemin: 15 --> 9
|
|
* Tous chemins permis
|
|
* PATH UTILISE : ../Paths/custom_paths/long_path_carre.path
|
|
*/
|
|
public void chemin_long_CARRE_length() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(0);
|
|
Node origin = myGraph.get(15);
|
|
Node destination = myGraph.get(9);
|
|
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
|
ShortestPathSolution bell_path = bellman.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
|
|
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
|
|
}
|
|
|
|
|
|
@Test
|
|
/*
|
|
* Chemin nul sur carte carrée.
|
|
* L'origine et la destination sont les mêmes (noeud 3).
|
|
* Tous chemins permis
|
|
*/
|
|
public void chemin_nul_CARRE() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(0);
|
|
Node origin = myGraph.get(3);
|
|
Node destination = myGraph.get(3);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
assert(!dijk_path.isFeasible());
|
|
assert(dijk_path.getPath() == null);
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Chemin inexistant sur la carte INSA.
|
|
* Origine: 224
|
|
* Destination: 814.
|
|
* Les 2 noeuds font partie de deux composantes non connexes.
|
|
* Tous chemins permis.
|
|
*/
|
|
public void chemin_inexistant_INSA() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(1);
|
|
Node origin = myGraph.get(224);
|
|
Node destination = myGraph.get(814);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
assert(!dijk_path.isFeasible());
|
|
assert(dijk_path.getPath() == null);
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Chemin court sur la carte de Toulouse.
|
|
* Origine : 8423
|
|
* Destination: 8435
|
|
* Tous chemins permis.
|
|
* PATH UTILISE : ../Paths/custom_paths/short_path_tls.path
|
|
*/
|
|
public void chemin_court_TLS() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(2);
|
|
Node origin = myGraph.get(8423);
|
|
Node destination = myGraph.get(8435);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
|
ShortestPathSolution bell_path = bellman.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
assert(Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0);
|
|
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Chemin long sur la carte de Toulouse.
|
|
* Même si Bellman est de plus long à faire long à faire, ce test prend moins
|
|
* de 3s, on estime que ce n'est pas trop et on va utiliser Bellman.
|
|
* Par contre, dans le test sur la région midi_pyrenees qui arrive après, on va
|
|
* être obligé de trouver une autre solution.
|
|
* Origine: 16644
|
|
* Destination: 39229
|
|
* Mode: LENGTH
|
|
* PATH UTILISE : ../Paths/custom_paths/long_path_tls.path
|
|
*/
|
|
public void chemin_long_TLS_length() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(2);
|
|
Node origin = myGraph.get(16644);
|
|
Node destination = myGraph.get(39229);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
|
ShortestPathSolution bell_path = bellman.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
assert((Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength()) < 1.0));
|
|
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 10.0);
|
|
}
|
|
|
|
|
|
@Test
|
|
/*
|
|
* Chemin long sur la carte de Toulouse.
|
|
* Même si Bellman est de plus long à faire long à faire, ce test prend moins
|
|
* de 3s, on estime que ce n'est pas trop et on va utiliser Bellman.
|
|
* Par contre, dans le test sur la région midi_pyrenees qui arrive après, on va
|
|
* être obligé de trouver une autre solution.
|
|
* Origine: 16644
|
|
* Destination: 39229
|
|
* Mode: TIME
|
|
* PATH UTILISE : ../Paths/custom_paths/long_path_tls.path
|
|
*/
|
|
public void chemin_long_TLS_time() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(2);
|
|
Graph myGraph = graph.get(2);
|
|
Node origin = myGraph.get(16644);
|
|
Node destination = myGraph.get(39229);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
|
|
ShortestPathSolution bell_path = bellman.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
assert((Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getMinimumTravelTime()) < 1.0));
|
|
assert(Math.abs(dijk_path.getPath().getMinimumTravelTime() - bell_path.getPath().getMinimumTravelTime()) < 10.0 );
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Test du mode à vélo facultatif.
|
|
* Nous prenons une origine sur l'autoroute et une destination en dehors.
|
|
* Ce chemin est donc censé être utilisable en vélo mais pas en voiture.
|
|
* Avec le filtre vélos, on obtient pas de chemin.
|
|
* Avec le filtre voitures on obtient un chemin.
|
|
* Origine: 19135
|
|
* Destination: 1980
|
|
* PATH UTILISE : ../Paths/custom_paths/path_cyclist.path
|
|
*/
|
|
public void chemin_velo_uniquement() {
|
|
// Filter: forBicyclesCustomT
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(4);
|
|
|
|
Graph myGraph = graph.get(2);
|
|
Node origin = myGraph.get(19135);
|
|
Node destination = myGraph.get(1980);
|
|
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra_bicycle = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path_bicycle = dijkstra_bicycle.doRun();
|
|
|
|
BellmanFordAlgorithm bellman_bicycle = new BellmanFordAlgorithm(data);
|
|
ShortestPathSolution bell_path_bicycle = bellman_bicycle.doRun();
|
|
|
|
// Filter: forCarsL
|
|
ArcInspector new_Inspector = ArcInspectorFactory.getAllFilters().get(1);
|
|
data = new ShortestPathData(myGraph, origin, destination, new_Inspector);
|
|
|
|
DijkstraAlgorithm dijkstra_car = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path_car = dijkstra_car.doRun();
|
|
|
|
assertEquals(dijk_path_bicycle.getPath(), null);
|
|
assert(!dijk_path_bicycle.isFeasible());
|
|
assertEquals(bell_path_bicycle.getPath(), null);
|
|
assert(!bell_path_bicycle.isFeasible());
|
|
|
|
assert(dijk_path_car.getPath() != null);
|
|
assert(dijk_path_car.isFeasible());
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* On veut vérifier le bon fonctionnement des modes TIME et LENGTH.
|
|
* Pour cela, on va obtenir deux chemins avec l'algo de Dijkstra et vérifier
|
|
* que:
|
|
* -le chemin TIME est plus rapide en durée que le chemin LENGTH
|
|
* -le chemin LENGTH est plus court en distance que le chemin LENGTH.
|
|
* On prend un grand chemin pour être sur d'avoir une différence :
|
|
* Origine: 16644
|
|
* Destination: 39229
|
|
* Mode: TIME/LENGTH
|
|
* PATH UTILISE : ../Paths/custom_paths/long_path_tls.path
|
|
*/
|
|
public void chemin_time_length_comparison() {
|
|
Graph myGraph = graph.get(2);
|
|
Node origin = myGraph.get(19135);
|
|
Node destination = myGraph.get(1980);
|
|
|
|
// Filter: forCarsL
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(1);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra_L = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path_L = dijkstra_L.doRun();
|
|
|
|
// Filter: forCarsT
|
|
ArcInspector new_Inspector = ArcInspectorFactory.getAllFilters().get(2);
|
|
data = new ShortestPathData(myGraph, origin, destination, new_Inspector);
|
|
|
|
DijkstraAlgorithm dijkstra_T = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path_T = dijkstra_T.doRun();
|
|
|
|
assert(dijk_path_L.getPath().isValid());
|
|
assert(dijk_path_L.isFeasible());
|
|
assert(dijk_path_T.getPath().isValid());
|
|
assert(dijk_path_T.isFeasible());
|
|
|
|
assert((Math.abs(dijkstra_L.getCostPath() - dijk_path_L.getPath().getLength()) < 1.0));
|
|
assert((Math.abs(dijkstra_T.getCostPath() - dijk_path_T.getPath().getMinimumTravelTime()) < 1.0));
|
|
|
|
assert(dijk_path_L.getPath().getLength() < dijk_path_T.getPath().getLength());
|
|
assert(dijk_path_L.getPath().getMinimumTravelTime() > dijk_path_T.getPath().getMinimumTravelTime());
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Cette fois-ci, Bellman est trop long à utiliser même une seule fois.
|
|
* On va donc utiliser quelques techniques pour se rassurer.
|
|
* -vérifier certains noeuds comme départ, destination, noeud pivot.
|
|
* -vérifier le cout avec une estimation gentille.
|
|
* -etc.
|
|
* Origin: 279654
|
|
* Destination: 481936
|
|
* Mode: LENGTH
|
|
* PATH UTILISE : ../Paths/custom_paths/long_chemin_midi_pyrenees.path
|
|
*/
|
|
public void chemin_long_Midi_pyrenees_length() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
|
|
Graph myGraph = graph.get(3);
|
|
Node origin = myGraph.get(279654);
|
|
Node destination = myGraph.get(481936);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
// On a des erreurs d'arrondi assez grande avec la distance, mais elles sont mineures
|
|
// relativement aux distance de 300000 ici.
|
|
assert((Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getLength())) < 1000.0);
|
|
// Selon le chemin sélectionné on peut avoir une estimation de la longueur qu'on est censée avoir.
|
|
// Avec notre long chemin: entre 250 et 260 kilomètres.
|
|
assert(dijkstra.getCostPath() > 250000 && dijkstra.getCostPath() < 260000);
|
|
// On peut aussi supposer que le nombre d'arcs empruntés est très grand.
|
|
assert(dijk_path.getPath().getArcs().size() > 1000);
|
|
}
|
|
|
|
@Test
|
|
/*
|
|
* Cette fois-ci, Bellman est trop long à utiliser même une seule fois.
|
|
* On va donc utiliser quelques techniques pour se rassurer.
|
|
* -vérifier certains noeuds comme départ, destination, noeud pivot.
|
|
* -vérifier le cout avec une estimation gentille.
|
|
* -etc.
|
|
* Origin: 279654
|
|
* Destination: 481936
|
|
* Mode: LENGTH
|
|
* PATH UTILISE : ../Paths/custom_paths/long_chemin_midi_pyrenees.path
|
|
*/
|
|
public void chemin_long_Midi_pyrenees_time() {
|
|
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(2);
|
|
Graph myGraph = graph.get(3);
|
|
Node origin = myGraph.get(279654);
|
|
Node destination = myGraph.get(481936);
|
|
ShortestPathData data = new ShortestPathData(myGraph, origin, destination, arcInspector);
|
|
|
|
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(data);
|
|
ShortestPathSolution dijk_path = dijkstra.doRun();
|
|
|
|
assert(dijk_path.getPath().isValid());
|
|
assert(dijk_path.isFeasible());
|
|
// On a des erreurs d'arrondi assez grandes avec la distance
|
|
assert((Math.abs(dijkstra.getCostPath() - dijk_path.getPath().getMinimumTravelTime())) < 100.0);
|
|
// Selon le chemin sélectionné on peut avoir une estimation de la durée qu'on est censée avoir.
|
|
// Avec notre long chemin: entre 12000 et 13000 secondes.
|
|
assert(dijkstra.getCostPath() > 12000 && dijkstra.getCostPath() < 13000);
|
|
// On peut aussi supposer que le nombre d'arcs empruntés est très grand.
|
|
assert(dijk_path.getPath().getArcs().size() > 1000);
|
|
}
|
|
}
|