Compare commits

..

3 commits

7 changed files with 136 additions and 15 deletions

1
.gitignore vendored
View file

@ -16,5 +16,4 @@ doc
# Project specific files and folders
*.mapfg
*.mapgr
*.path
*.tgz

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -12,6 +12,8 @@ import org.insa.graphs.model.Path;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
private float pathCost;
public DijkstraAlgorithm(ShortestPathData data) {
super(data);
}
@ -32,6 +34,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// List of sommets, but with type Label
ArrayList<Label> labels = new ArrayList<Label>(nbNodes);
// Heap of sommets
BinaryHeap<Label> tas = new BinaryHeap<Label>();
@ -71,7 +74,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// we know its origin and destination
for (Arc arc : x.getNode().getSuccessors()) {
if (successor.getNode().equals(arc.getDestination())) {
arc_cost = arc.getLength();
arc_cost = (float) data.getCost(arc);
}
}
final float possible_path_cost = x.getCost() + arc_cost;
@ -97,6 +100,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
if (labels.get(data.getDestination().getId()).getParentNode() == null) {
this.pathCost = 0;
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
@ -128,15 +132,18 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
notifyDestinationReached(data.getDestination());
// Reverse the path...
Collections.reverse(arcs_path);
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs_path));
}
this.pathCost = labels.get(data.getDestination().getId()).getCost();
}
return solution;
}
public float getCostPath() {
return this.pathCost;
}
}

View file

@ -71,20 +71,22 @@ public class DijkstraAlgorithmTest {
}
}
@Test
/* 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() {
public void chemin_court_CARRE_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(0);
Node origin = myGraph.get(19);
@ -110,7 +112,7 @@ public class DijkstraAlgorithmTest {
* Tous chemins permis
* PATH UTILISE : ../Paths/custom_paths/long_path_carre.path
*/
public void chemin_long_CARRE() {
public void chemin_long_CARRE_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(0);
Node origin = myGraph.get(15);
@ -130,6 +132,7 @@ public class DijkstraAlgorithmTest {
assert(Math.abs(dijk_path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
}
@Test
/*
* Chemin nul sur carte carrée.
@ -207,9 +210,10 @@ public class DijkstraAlgorithmTest {
* ê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() {
public void chemin_long_TLS_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(2);
Node origin = myGraph.get(16644);
@ -225,7 +229,39 @@ public class DijkstraAlgorithmTest {
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()) < (bell_path.getPath().getLength() ));
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
@ -271,6 +307,50 @@ public class DijkstraAlgorithmTest {
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.
@ -280,9 +360,10 @@ public class DijkstraAlgorithmTest {
* -etc.
* Origin: 279654
* Destination: 481936
* Mode: LENGTH
* PATH UTILISE : ../Paths/custom_paths/long_chemin_midi_pyrenees.path
*/
public void chemin_long_Midi_pyrenees() {
public void chemin_long_Midi_pyrenees_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(3);
Node origin = myGraph.get(279654);
@ -292,9 +373,10 @@ public class DijkstraAlgorithmTest {
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.
@ -302,4 +384,37 @@ public class DijkstraAlgorithmTest {
// 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);
}
}