Minor fixes and tests

This commit is contained in:
Yohan Simard 2020-05-19 18:35:58 +02:00
parent db095f9ea9
commit 460456581e
4 changed files with 12 additions and 5 deletions

View file

@ -56,7 +56,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Node destination = data.getDestination(); Node destination = data.getDestination();
Node currentNode = null; Node currentNode = null;
// Debug / Tests
double previousCost = 0; double previousCost = 0;
int iterationCounter = 0;
while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) { while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
// Get the node with the minimum cost // Get the node with the minimum cost
@ -65,11 +68,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
currentNode = currentLabel.getNode(); currentNode = currentLabel.getNode();
/* /*
// Debug // Debug / Tests
assert(((BinaryHeap<Label>) heap).isValid()); assert(((BinaryHeap<Label>) heap).isValid());
assert(currentLabel.getTotalCost() >= previousCost); assert(currentLabel.getTotalCost() >= previousCost);
previousCost = currentLabel.getCost(); previousCost = currentLabel.getCost();
*/ */
iterationCounter++;
for (Arc arc : currentNode.getSuccessors()) { for (Arc arc : currentNode.getSuccessors()) {
Node dest = arc.getDestination(); Node dest = arc.getDestination();
@ -111,6 +115,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
currentLabel = labels.get(arc.getOrigin().getId()); currentLabel = labels.get(arc.getOrigin().getId());
} }
System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
Collections.reverse(path); Collections.reverse(path);
return new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(data.getGraph(), path)); return new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(data.getGraph(), path));

View file

@ -4,7 +4,7 @@ package org.insa.graphs.algorithm.shortestpath;
public class AStarAlgorithmTest extends ShortestPathTest { public class AStarAlgorithmTest extends ShortestPathTest {
@Override @Override
protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) { protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) {
return new AStarAlgorithm(data).doRun(); return new AStarAlgorithm(data).run();
} }
} }

View file

@ -3,6 +3,6 @@ package org.insa.graphs.algorithm.shortestpath;
public class DijkstraAlgorithmTest extends ShortestPathTest { public class DijkstraAlgorithmTest extends ShortestPathTest {
@Override @Override
protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) { protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) {
return new DijkstraAlgorithm(data).doRun(); return new DijkstraAlgorithm(data).run();
} }
} }

View file

@ -62,7 +62,7 @@ public abstract class ShortestPathTest {
for (ArcInspector arcInspector : arcInspectors) { for (ArcInspector arcInspector : arcInspectors) {
ShortestPathData data = createData(maps[i], arcInspector, origIndexes[i], destIndexes[i]); ShortestPathData data = createData(maps[i], arcInspector, origIndexes[i], destIndexes[i]);
ShortestPathSolution sol = runShortestPathAlgo(data); ShortestPathSolution sol = runShortestPathAlgo(data);
ShortestPathSolution oracle = new BellmanFordAlgorithm(data).doRun(); ShortestPathSolution oracle = new BellmanFordAlgorithm(data).run();
assertTrue(sol.isFeasible()); assertTrue(sol.isFeasible());
Path path = sol.getPath(); Path path = sol.getPath();
assertTrue(path.isValid()); assertTrue(path.isValid());
@ -71,7 +71,8 @@ public abstract class ShortestPathTest {
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes())); assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
if (i == 1) if (i == 1)
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes())); assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
// Check result against Bellman Ford algorithm (except for the square map or there may be several paths with the same cost) // Check result against Bellman Ford algorithm (except for the square map
// where there may be several paths with the same cost)
if (i != 0) if (i != 0)
assertEquals(sol.getPath(), oracle.getPath()); assertEquals(sol.getPath(), oracle.getPath());
System.out.println(); System.out.println();