Astar Fini

This commit is contained in:
El Haji Fofana 2023-05-18 19:09:10 +02:00
parent 4c09642f93
commit 7624518c7e
6 changed files with 38 additions and 23 deletions

View file

@ -1,7 +1,8 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import java.util.ArrayList;
import org.insa.graphs.model.Point; import org.insa.graphs.model.Point;
public class AStarAlgorithm extends DijkstraAlgorithm { public class AStarAlgorithm extends DijkstraAlgorithm {
@ -12,7 +13,17 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
@Override @Override
public Label CreationLabel(Node x, Double cout, Arc parent){ public Label CreationLabel(Node x, Double cout, Arc parent){
ShortestPathData data = getInputData(); ShortestPathData data = getInputData();
return new LabelStar(x,cout,parent,Point.distance(x.getPoint(), data.getDestination().getPoint())); return new LabelStar(x,cout,parent,Point.distance(data.getGraph().get(x.getId()).getPoint(), data.getDestination().getPoint()));
} }
@Override
public void ShortestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes , ShortestPathData data)
{
Path p = Path.createShortestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("shortest path : " + p.getLength());
if (p.getLength()-solution.getPath().getLength() < 1.00){
System.out.println("le chemin Astar est le shortest");
}
}
} }

View file

@ -97,13 +97,19 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
solutionNodes.add(a.getOrigin()); solutionNodes.add(a.getOrigin());
} }
solutionNodes.add(data.getDestination()); solutionNodes.add(data.getDestination());
ShortestVerif(solution, solutionNodes, data);
return solution;
}
public void ShortestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes , ShortestPathData data)
{
Path p = Path.createShortestPathFromNodes(data.getGraph(), solutionNodes); Path p = Path.createShortestPathFromNodes(data.getGraph(), solutionNodes);
Path p2 = Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("shortest path : " + p.getLength()); System.out.println("shortest path : " + p.getLength());
if (p.getLength()-solution.getPath().getLength() < 1.00){ if (p.getLength()-solution.getPath().getLength() < 1.00){
System.out.println("le chemin Dijkstra est le shortest"); System.out.println("le chemin Dijkstra est le shortest");
} }
return solution;
} }
} }

View file

@ -22,20 +22,27 @@ public class Label implements Comparable<Label> {
@Override @Override
public int compareTo(Label a) { public int compareTo(Label a) {
int result; int result;
if (this.getTotalCost() < a.cost){ if (this.getTotalCost() < a.getTotalCost()){
result = -1; result = -1;
}else if (this.getTotalCost()>a.cost){ }else if (this.getTotalCost()>a.getTotalCost()){
result = 1; result = 1;
}else { }else {
if(this.getTotalCost() == this.cost){ if(this.getRealCost() == a.getRealCost()){
result=1; result=0;
}
else if (this.cost <a.cost){
result = -1;
} }
else{ else{
result = 0; if (this.getRealCost() <a.getRealCost()){
result = -1;
}
else if(this.getRealCost() > a.getRealCost()){
result = 1;
}
else{
result = 0;
}
} }
} }
return result; return result;
} }

View file

@ -7,28 +7,19 @@ import org.insa.graphs.model.Node;
public class LabelStar extends Label{ public class LabelStar extends Label{
private double cost_Destination; private double cost_Destination;
private double cost;
public LabelStar(Node sommet, double cost, Arc parent,double cost_Destination) public LabelStar(Node sommet, double cost, Arc parent,double cost_Destination)
{ {
super(sommet, cost, parent); super(sommet, cost, parent);
this.cost=cost;
this.cost_Destination=cost_Destination; this.cost_Destination=cost_Destination;
} }
@Override @Override
public double getTotalCost() { public double getTotalCost() {
return this.cost_Destination+this.cost; return this.cost_Destination+super.getRealCost();
} }
public double getCost_Destination() {
return cost_Destination;
}
} }