added testings for Dijkstra
This commit is contained in:
parent
7fa1212e70
commit
53efa75497
3 changed files with 121 additions and 8 deletions
|
@ -17,7 +17,7 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
public ShortestPathSolution doRun() {
|
||||||
|
|
||||||
// Retrieve the graph.
|
// Retrieve the graph.
|
||||||
ShortestPathData data = getInputData();
|
ShortestPathData data = getInputData();
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
public ShortestPathSolution doRun() {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
Graph gr = data.getGraph();
|
Graph gr = data.getGraph();
|
||||||
|
|
||||||
|
@ -40,25 +40,30 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
pQueue.insert(labels[data.getOrigin().getId()]);
|
pQueue.insert(labels[data.getOrigin().getId()]);
|
||||||
|
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
|
double prev = 0;
|
||||||
// Pathfinding
|
// Pathfinding
|
||||||
while(!pQueue.isEmpty() && !found){
|
while(!pQueue.isEmpty() && !found){
|
||||||
|
|
||||||
Label labelX = pQueue.findMin();
|
Label labelX = pQueue.deleteMin();
|
||||||
pQueue.deleteMin();
|
|
||||||
|
if (labelX.getCost()<prev){System.out.println(labelX.getCost()-prev);}
|
||||||
|
prev = labelX.getCost();
|
||||||
|
|
||||||
labelX.setMarked(true);
|
labelX.setMarked(true);
|
||||||
|
notifyNodeReached(labelX.getCurrNode());
|
||||||
found = (data.getDestination() == labelX.getCurrNode());
|
found = (data.getDestination() == labelX.getCurrNode());
|
||||||
|
|
||||||
for (Arc y : labelX.getCurrNode().getSuccessors()){
|
for (Arc y : labelX.getCurrNode().getSuccessors()){
|
||||||
Label labelY = labels[y.getDestination().getId()];
|
Label labelY = labels[y.getDestination().getId()];
|
||||||
if (!labelY.isMarked()){
|
if (!labelY.isMarked() && data.isAllowed(y)){
|
||||||
|
|
||||||
if (labelY.getCost() > labelX.getCost()+data.getCost(y)){
|
if (labelY.getCost() > labelX.getCost()+data.getCost(y)){
|
||||||
|
if (labels[labelY.getCurrNode().getId()].getCost() != Double.POSITIVE_INFINITY) {
|
||||||
|
pQueue.remove(labelY);
|
||||||
|
}
|
||||||
labelY.setCost(
|
labelY.setCost(
|
||||||
labelX.getCost()+data.getCost(y)
|
labelX.getCost()+data.getCost(y)
|
||||||
);
|
);
|
||||||
|
|
||||||
pQueue.insert(labelY);
|
pQueue.insert(labelY);
|
||||||
labelY.setFather(y); // we give the arc from x to y as the "father arc"
|
labelY.setFather(y); // we give the arc from x to y as the "father arc"
|
||||||
}
|
}
|
||||||
|
@ -68,7 +73,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
ShortestPathSolution solution = null;
|
ShortestPathSolution solution = null;
|
||||||
|
|
||||||
// Destination has no predecessor, the solution is infeasible...
|
// Destination has no predecessor, the solution is infeasible...
|
||||||
if (labels[data.getDestination().getId()] == null) {
|
if (!found || labels[data.getDestination().getId()] == null) {
|
||||||
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -9,9 +9,16 @@ import java.io.FileInputStream;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.*;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
||||||
|
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
|
||||||
import org.insa.graphs.gui.drawing.Drawing;
|
import org.insa.graphs.gui.drawing.Drawing;
|
||||||
import org.insa.graphs.gui.drawing.components.BasicDrawing;
|
import org.insa.graphs.gui.drawing.components.BasicDrawing;
|
||||||
import org.insa.graphs.model.Graph;
|
import org.insa.graphs.model.Graph;
|
||||||
|
import org.insa.graphs.model.Node;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
import org.insa.graphs.model.io.BinaryGraphReader;
|
import org.insa.graphs.model.io.BinaryGraphReader;
|
||||||
import org.insa.graphs.model.io.BinaryPathReader;
|
import org.insa.graphs.model.io.BinaryPathReader;
|
||||||
|
@ -44,6 +51,94 @@ public class Launch {
|
||||||
return basicDrawing;
|
return basicDrawing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Path createTestPath(Graph graph,int idOrigin,int idDest,String algo){
|
||||||
|
Node A = graph.get(idOrigin);
|
||||||
|
Node B = graph.get(idDest);
|
||||||
|
Path resu = null;
|
||||||
|
ArcInspector ins = ArcInspectorFactory.getAllFilters().get(0);
|
||||||
|
ShortestPathSolution sol;
|
||||||
|
ShortestPathData data= new ShortestPathData(graph,A,B,ins);
|
||||||
|
|
||||||
|
switch (algo){
|
||||||
|
case "A*":
|
||||||
|
System.out.println("not yet implemented");
|
||||||
|
break;
|
||||||
|
case "D":
|
||||||
|
DijkstraAlgorithm algorithm = new DijkstraAlgorithm(data);
|
||||||
|
sol = algorithm.doRun();
|
||||||
|
resu = sol.getPath();
|
||||||
|
break;
|
||||||
|
case "BF":
|
||||||
|
BellmanFordAlgorithm BF = new BellmanFordAlgorithm(data);
|
||||||
|
sol = BF.doRun();
|
||||||
|
resu = sol.getPath();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("no known algorithm");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resu;
|
||||||
|
|
||||||
|
}
|
||||||
|
public static int comparePath(Path p1, Path p2,boolean time){
|
||||||
|
if (p1 == null){
|
||||||
|
if (p2 == null){
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (p2 == null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(time)
|
||||||
|
if (Double.compare(p1.getMinimumTravelTime(),p2.getMinimumTravelTime())==0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (Float.compare(p1.getLength(),p2.getLength())==0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static int test(Graph graph){
|
||||||
|
int Id1 = (int) Math.floor(Math.random()*graph.size());
|
||||||
|
int Id2 = (int) Math.floor(Math.random()*graph.size());
|
||||||
|
int resu = 0;
|
||||||
|
System.out.println("testing with nodes " + Id1 + " and " + Id2);
|
||||||
|
if (comparePath(createTestPath(graph, Id1, Id2, "D"),createTestPath(graph, Id1, Id2, "BF"), true) == 0) {
|
||||||
|
resu++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
resu++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comparePath(createTestPath(graph, Id1, Id2, "D"),createTestPath(graph, Id1, Id2, "BF"), false) == 0) {
|
||||||
|
resu++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
resu++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resu;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
// Visit these directory to see the list of available files on Commetud.
|
// Visit these directory to see the list of available files on Commetud.
|
||||||
|
@ -72,5 +167,18 @@ public class Launch {
|
||||||
|
|
||||||
// TODO: Draw the path.
|
// TODO: Draw the path.
|
||||||
drawing.drawPath(path);
|
drawing.drawPath(path);
|
||||||
|
|
||||||
|
System.out.println("==TESTS==");
|
||||||
|
|
||||||
|
|
||||||
|
int nbTest = 1000;
|
||||||
|
int juste = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < nbTest; i++){
|
||||||
|
juste += test(graph);
|
||||||
|
}
|
||||||
|
System.out.println(
|
||||||
|
"nmber of good tests : " + Integer.toString(juste) + "/" + Integer.toString(nbTest*2)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue