finished tests and A*
This commit is contained in:
parent
53efa75497
commit
8168b86d09
5 changed files with 96 additions and 29 deletions
|
@ -1,9 +1,29 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractSolution;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.algorithm.utils.Label;
|
||||
import org.insa.graphs.algorithm.utils.LabelStar;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||
|
||||
public AStarAlgorithm(ShortestPathData data) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initLabel(){
|
||||
int i = 0;
|
||||
for (Node l : gr.getNodes()){
|
||||
labels[i] = new LabelStar(l, false, null,data.getDestination());
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
|
|||
import org.insa.graphs.algorithm.AbstractSolution;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.algorithm.utils.Label;
|
||||
import org.insa.graphs.algorithm.utils.LabelStar;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
@ -17,22 +18,29 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
public DijkstraAlgorithm(ShortestPathData data) {
|
||||
super(data);
|
||||
}
|
||||
final ShortestPathData data = getInputData();
|
||||
Graph gr = data.getGraph();
|
||||
|
||||
@Override
|
||||
public ShortestPathSolution doRun() {
|
||||
final ShortestPathData data = getInputData();
|
||||
Graph gr = data.getGraph();
|
||||
// array containing all the labels
|
||||
Label[] labels = new Label[gr.size()];
|
||||
|
||||
// array containing all the labels
|
||||
Label[] labels = new Label[gr.size()];
|
||||
|
||||
|
||||
//initialization of the labels
|
||||
protected void initLabel(){
|
||||
int i = 0;
|
||||
for (Node l : gr.getNodes()){
|
||||
labels[i] = new Label(l, false, null);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShortestPathData getInputData() {
|
||||
return super.getInputData();
|
||||
}
|
||||
|
||||
public ShortestPathSolution doRun() {
|
||||
|
||||
//initialization of the labels
|
||||
initLabel();
|
||||
|
||||
// initialization of the heap & the first node
|
||||
BinaryHeap<Label> pQueue = new BinaryHeap<Label>();
|
||||
|
@ -40,14 +48,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
pQueue.insert(labels[data.getOrigin().getId()]);
|
||||
|
||||
boolean found = false;
|
||||
double prev = 0;
|
||||
/*double prev = 0;*/
|
||||
// Pathfinding
|
||||
while(!pQueue.isEmpty() && !found){
|
||||
|
||||
Label labelX = pQueue.deleteMin();
|
||||
|
||||
if (labelX.getCost()<prev){System.out.println(labelX.getCost()-prev);}
|
||||
prev = labelX.getCost();
|
||||
/*if (labelX.getCost()<prev){System.out.println(labelX.getCost()-prev);}
|
||||
prev = labelX.getCost();*/
|
||||
|
||||
labelX.setMarked(true);
|
||||
notifyNodeReached(labelX.getCurrNode());
|
||||
|
|
|
@ -19,6 +19,10 @@ public class Label implements Comparable<Label> {
|
|||
return(this.cost);
|
||||
}
|
||||
|
||||
public double getTotalCost(){
|
||||
return(this.cost);
|
||||
}
|
||||
|
||||
public Node getCurrNode() {
|
||||
return currNode;
|
||||
}
|
||||
|
@ -44,13 +48,24 @@ public class Label implements Comparable<Label> {
|
|||
}
|
||||
|
||||
public int compareTo(Label other) {
|
||||
if (this.cost < other.getCost()) {
|
||||
if (this.getTotalCost() < other.getTotalCost()) {
|
||||
return -1;
|
||||
} else {
|
||||
if (this.cost > other.getCost()) {
|
||||
if (this.getTotalCost() > other.getTotalCost()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
} else { // same total cost
|
||||
|
||||
double thisCrowCost = this.getTotalCost()-this.getCost();
|
||||
double otherCrowCost = other.getTotalCost()-other.getCost();
|
||||
if (thisCrowCost < otherCrowCost) {
|
||||
return -1;
|
||||
} else {
|
||||
if (thisCrowCost > otherCrowCost) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package org.insa.graphs.algorithm.utils;
|
||||
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
||||
public class LabelStar extends Label{
|
||||
|
||||
private double crowCost;
|
||||
|
||||
public LabelStar(Node currNode, boolean marked, Arc father, Node dest){
|
||||
super(currNode,marked,father);
|
||||
this.crowCost = currNode.getPoint().distanceTo(dest.getPoint());
|
||||
}
|
||||
|
||||
public double getTotalCost(){
|
||||
return(this.getCost() + this.crowCost);
|
||||
}
|
||||
}
|
|
@ -10,11 +10,7 @@ import javax.swing.JFrame;
|
|||
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.algorithm.shortestpath.*;
|
||||
import org.insa.graphs.gui.drawing.Drawing;
|
||||
import org.insa.graphs.gui.drawing.components.BasicDrawing;
|
||||
import org.insa.graphs.model.Graph;
|
||||
|
@ -61,11 +57,13 @@ public class Launch {
|
|||
|
||||
switch (algo){
|
||||
case "A*":
|
||||
System.out.println("not yet implemented");
|
||||
AStarAlgorithm AAlgo = new AStarAlgorithm(data);
|
||||
sol = AAlgo.doRun();
|
||||
resu = sol.getPath();
|
||||
break;
|
||||
case "D":
|
||||
DijkstraAlgorithm algorithm = new DijkstraAlgorithm(data);
|
||||
sol = algorithm.doRun();
|
||||
DijkstraAlgorithm DAlgo = new DijkstraAlgorithm(data);
|
||||
sol = DAlgo.doRun();
|
||||
resu = sol.getPath();
|
||||
break;
|
||||
case "BF":
|
||||
|
@ -115,12 +113,12 @@ public class Launch {
|
|||
|
||||
|
||||
|
||||
private static int test(Graph graph){
|
||||
private static int test(Graph graph, String testType,Drawing drawing){
|
||||
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) {
|
||||
if (comparePath(createTestPath(graph, Id1, Id2, testType),createTestPath(graph, Id1, Id2, "BF"), true) == 0) {
|
||||
resu++;
|
||||
}
|
||||
else
|
||||
|
@ -135,7 +133,12 @@ public class Launch {
|
|||
{
|
||||
resu++;
|
||||
}
|
||||
|
||||
Path drawPath = createTestPath(graph, Id1, Id2, "BF");
|
||||
if (drawPath != null){
|
||||
if (!drawPath.isEmpty()){
|
||||
drawing.drawPath(drawPath);
|
||||
}
|
||||
}
|
||||
return resu;
|
||||
}
|
||||
|
||||
|
@ -175,10 +178,13 @@ public class Launch {
|
|||
int juste = 0;
|
||||
|
||||
for (int i = 0; i < nbTest; i++){
|
||||
juste += test(graph);
|
||||
if (i%10 == 0){
|
||||
drawing.clearOverlays();
|
||||
}
|
||||
juste += test(graph,"A*",drawing);
|
||||
}
|
||||
System.out.println(
|
||||
"nmber of good tests : " + Integer.toString(juste) + "/" + Integer.toString(nbTest*2)
|
||||
"number of good tests : " + Integer.toString(juste) + "/" + Integer.toString(nbTest*2)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue