Dijkstra tests

This commit is contained in:
Yohan Simard 2020-04-22 18:32:33 +02:00
parent f79d9148c0
commit 7776e34f92
4 changed files with 144 additions and 0 deletions

View file

@ -168,6 +168,35 @@ public class ArcInspectorFactory {
}
});
// No filter (all arcs allowed), time:
filters.add(new ArcInspector() {
@Override
public boolean isAllowed(Arc arc) {
return true;
}
@Override
public double getCost(Arc arc) {
return arc.getMinimumTravelTime();
}
@Override
public int getMaximumSpeed() {
return GraphStatistics.NO_MAXIMUM_SPEED;
}
@Override
public Mode getMode() {
return Mode.TIME;
}
@Override
public String toString() {
return "Fastest path, all roads allowed";
}
});
// Add your own filters here (do not forget to implement toString()
// to get an understandable output!):

View file

@ -42,12 +42,20 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
heap.insert(labels.get(originId));
notifyOriginProcessed(data.getOrigin());
// int iterationCounter = 0;
while (!heap.isEmpty()) {
// iterationCounter++;
// Get the node with the minimum cost
Label currentLabel = heap.deleteMin();
currentLabel.mark();
Node currentNode = currentLabel.getNode();
// System.out.printf("Label marked : cost = %f; %d succesors; heap valid = %b \n",
// currentLabel.getCost(),
// currentNode.getSuccessors().size(),
// ((BinaryHeap<Label>)heap).isValid());
for (Arc arc : currentNode.getSuccessors()) {
Node dest = arc.getDestination();
Label destLabel = labels.get(dest.getId());
@ -89,6 +97,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
currentLabel = labels.get(arc.getOrigin().getId());
}
// System.out.printf("Nombre d'arcs du plus court chemin : %d\n", path.size());
// System.out.printf("Nombre d'itérations : %d\n", iterationCounter);
Collections.reverse(path);
return new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(graph, path));

View file

@ -37,6 +37,29 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
this.array = new ArrayList<E>(heap.array);
}
/**
* Check if each element is lower than its children
* @return true if valid, false otherwise
*/
public boolean isValid() {
for (int i = 0; i <= indexParent(currentSize - 1); i++) {
E element = array.get(i);
// Check left child
if (indexLeft(i) < currentSize) {
E leftChild = array.get(indexLeft(i));
if (leftChild.compareTo(element) < 0)
return false;
}
// Check right child
if (indexRight(i) < currentSize) {
E rightChild = array.get(indexRight(i));
if (rightChild.compareTo(element) < 0)
return false;
}
}
return true;
}
/**
* Set an element at the given index.
*
@ -66,6 +89,13 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
return index * 2 + 1;
}
/**
* @return Index of the right child of the given index.
*/
protected int indexRight(int index) {
return (index + 1) * 2;
}
/**
* Internal method to percolate up in the heap.
*

View file

@ -0,0 +1,74 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.model.*;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
import org.junit.Test;
import java.io.*;
import java.util.List;
import java.util.Random;
import static org.junit.Assert.assertTrue;
public class DijkstraAlgorithmTest {
String[] maps = {
"/home/yohan/Documents/etudes/be-graphes/maps/carre.mapgr",
"/home/yohan/Documents/etudes/be-graphes/maps/insa.mapgr",
"/home/yohan/Documents/etudes/be-graphes/maps/toulouse.mapgr",
};
ArcInspector[] arcInspectors = {
ArcInspectorFactory.getAllFilters().get(0),
ArcInspectorFactory.getAllFilters().get(2),
};
@Test
public void testDoRun() {
Random random = new Random();
for (String map : maps) {
try (GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(
new FileInputStream(map))))) {
Graph graph = reader.read();
List<Node> nodes = graph.getNodes();
Node orig;
Node dest;
// Test random paths
for (int i = 0; i < 3; i++) {
orig = nodes.get(random.nextInt(nodes.size()));
do {
dest = nodes.get(random.nextInt(nodes.size()));
} while (orig == dest);
for (ArcInspector arcInspector : arcInspectors) {
ShortestPathData data = new ShortestPathData(graph, orig, dest, arcInspector);
ShortestPathSolution sol = new DijkstraAlgorithm(data).doRun();
if (sol.isFeasible())
assertTrue(sol.getPath().isValid());
}
}
// Test zero-length path
orig = nodes.get(random.nextInt(nodes.size()));
dest = orig;
for (ArcInspector arcInspector : arcInspectors) {
ShortestPathData data = new ShortestPathData(graph, orig, dest, arcInspector);
ShortestPathSolution sol = new DijkstraAlgorithm(data).doRun();
if (sol.isFeasible())
assertTrue(sol.getPath().isValid());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}