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;
|
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 class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(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.AbstractSolution;
|
||||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
import org.insa.graphs.algorithm.utils.Label;
|
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.Arc;
|
||||||
import org.insa.graphs.model.Graph;
|
import org.insa.graphs.model.Graph;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
@ -17,22 +18,29 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
public DijkstraAlgorithm(ShortestPathData data) {
|
public DijkstraAlgorithm(ShortestPathData data) {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ShortestPathSolution doRun() {
|
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
Graph gr = data.getGraph();
|
Graph gr = data.getGraph();
|
||||||
|
|
||||||
// array containing all the labels
|
// array containing all the labels
|
||||||
Label[] labels = new Label[gr.size()];
|
Label[] labels = new Label[gr.size()];
|
||||||
|
|
||||||
|
protected void initLabel(){
|
||||||
//initialization of the labels
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Node l : gr.getNodes()){
|
for (Node l : gr.getNodes()){
|
||||||
labels[i] = new Label(l, false, null);
|
labels[i] = new Label(l, false, null);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ShortestPathData getInputData() {
|
||||||
|
return super.getInputData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShortestPathSolution doRun() {
|
||||||
|
|
||||||
|
//initialization of the labels
|
||||||
|
initLabel();
|
||||||
|
|
||||||
// initialization of the heap & the first node
|
// initialization of the heap & the first node
|
||||||
BinaryHeap<Label> pQueue = new BinaryHeap<Label>();
|
BinaryHeap<Label> pQueue = new BinaryHeap<Label>();
|
||||||
|
@ -40,14 +48,14 @@ 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;
|
/*double prev = 0;*/
|
||||||
// Pathfinding
|
// Pathfinding
|
||||||
while(!pQueue.isEmpty() && !found){
|
while(!pQueue.isEmpty() && !found){
|
||||||
|
|
||||||
Label labelX = pQueue.deleteMin();
|
Label labelX = pQueue.deleteMin();
|
||||||
|
|
||||||
if (labelX.getCost()<prev){System.out.println(labelX.getCost()-prev);}
|
/*if (labelX.getCost()<prev){System.out.println(labelX.getCost()-prev);}
|
||||||
prev = labelX.getCost();
|
prev = labelX.getCost();*/
|
||||||
|
|
||||||
labelX.setMarked(true);
|
labelX.setMarked(true);
|
||||||
notifyNodeReached(labelX.getCurrNode());
|
notifyNodeReached(labelX.getCurrNode());
|
||||||
|
|
|
@ -19,6 +19,10 @@ public class Label implements Comparable<Label> {
|
||||||
return(this.cost);
|
return(this.cost);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getTotalCost(){
|
||||||
|
return(this.cost);
|
||||||
|
}
|
||||||
|
|
||||||
public Node getCurrNode() {
|
public Node getCurrNode() {
|
||||||
return currNode;
|
return currNode;
|
||||||
}
|
}
|
||||||
|
@ -44,14 +48,25 @@ public class Label implements Comparable<Label> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compareTo(Label other) {
|
public int compareTo(Label other) {
|
||||||
if (this.cost < other.getCost()) {
|
if (this.getTotalCost() < other.getTotalCost()) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
if (this.cost > other.getCost()) {
|
if (this.getTotalCost() > other.getTotalCost()) {
|
||||||
|
return 1;
|
||||||
|
} 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;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
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 javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import org.insa.graphs.algorithm.*;
|
import org.insa.graphs.algorithm.*;
|
||||||
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
import org.insa.graphs.algorithm.shortestpath.*;
|
||||||
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;
|
||||||
|
@ -61,11 +57,13 @@ public class Launch {
|
||||||
|
|
||||||
switch (algo){
|
switch (algo){
|
||||||
case "A*":
|
case "A*":
|
||||||
System.out.println("not yet implemented");
|
AStarAlgorithm AAlgo = new AStarAlgorithm(data);
|
||||||
|
sol = AAlgo.doRun();
|
||||||
|
resu = sol.getPath();
|
||||||
break;
|
break;
|
||||||
case "D":
|
case "D":
|
||||||
DijkstraAlgorithm algorithm = new DijkstraAlgorithm(data);
|
DijkstraAlgorithm DAlgo = new DijkstraAlgorithm(data);
|
||||||
sol = algorithm.doRun();
|
sol = DAlgo.doRun();
|
||||||
resu = sol.getPath();
|
resu = sol.getPath();
|
||||||
break;
|
break;
|
||||||
case "BF":
|
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 Id1 = (int) Math.floor(Math.random()*graph.size());
|
||||||
int Id2 = (int) Math.floor(Math.random()*graph.size());
|
int Id2 = (int) Math.floor(Math.random()*graph.size());
|
||||||
int resu = 0;
|
int resu = 0;
|
||||||
System.out.println("testing with nodes " + Id1 + " and " + Id2);
|
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++;
|
resu++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -135,7 +133,12 @@ public class Launch {
|
||||||
{
|
{
|
||||||
resu++;
|
resu++;
|
||||||
}
|
}
|
||||||
|
Path drawPath = createTestPath(graph, Id1, Id2, "BF");
|
||||||
|
if (drawPath != null){
|
||||||
|
if (!drawPath.isEmpty()){
|
||||||
|
drawing.drawPath(drawPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
return resu;
|
return resu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,10 +178,13 @@ public class Launch {
|
||||||
int juste = 0;
|
int juste = 0;
|
||||||
|
|
||||||
for (int i = 0; i < nbTest; i++){
|
for (int i = 0; i < nbTest; i++){
|
||||||
juste += test(graph);
|
if (i%10 == 0){
|
||||||
|
drawing.clearOverlays();
|
||||||
|
}
|
||||||
|
juste += test(graph,"A*",drawing);
|
||||||
}
|
}
|
||||||
System.out.println(
|
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