Astar Fini
This commit is contained in:
parent
4c09642f93
commit
7624518c7e
6 changed files with 38 additions and 23 deletions
|
@ -1,7 +1,8 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
||||
import org.insa.graphs.model.Path;
|
||||
import java.util.ArrayList;
|
||||
import org.insa.graphs.model.Point;
|
||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||
|
||||
|
@ -12,7 +13,17 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
@Override
|
||||
public Label CreationLabel(Node x, Double cout, Arc parent){
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -97,13 +97,19 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
solutionNodes.add(a.getOrigin());
|
||||
}
|
||||
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 p2 = Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
|
||||
System.out.println("shortest path : " + p.getLength());
|
||||
if (p.getLength()-solution.getPath().getLength() < 1.00){
|
||||
System.out.println("le chemin Dijkstra est le shortest");
|
||||
}
|
||||
|
||||
return solution;
|
||||
|
||||
}
|
||||
}
|
|
@ -22,20 +22,27 @@ public class Label implements Comparable<Label> {
|
|||
@Override
|
||||
public int compareTo(Label a) {
|
||||
int result;
|
||||
if (this.getTotalCost() < a.cost){
|
||||
if (this.getTotalCost() < a.getTotalCost()){
|
||||
result = -1;
|
||||
}else if (this.getTotalCost()>a.cost){
|
||||
}else if (this.getTotalCost()>a.getTotalCost()){
|
||||
result = 1;
|
||||
}else {
|
||||
if(this.getTotalCost() == this.cost){
|
||||
result=1;
|
||||
}
|
||||
else if (this.cost <a.cost){
|
||||
result = -1;
|
||||
if(this.getRealCost() == a.getRealCost()){
|
||||
result=0;
|
||||
}
|
||||
else{
|
||||
result = 0;
|
||||
if (this.getRealCost() <a.getRealCost()){
|
||||
result = -1;
|
||||
}
|
||||
else if(this.getRealCost() > a.getRealCost()){
|
||||
result = 1;
|
||||
}
|
||||
else{
|
||||
result = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -7,28 +7,19 @@ import org.insa.graphs.model.Node;
|
|||
|
||||
public class LabelStar extends Label{
|
||||
private double cost_Destination;
|
||||
private double cost;
|
||||
|
||||
|
||||
public LabelStar(Node sommet, double cost, Arc parent,double cost_Destination)
|
||||
{
|
||||
super(sommet, cost, parent);
|
||||
this.cost=cost;
|
||||
this.cost_Destination=cost_Destination;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getTotalCost() {
|
||||
return this.cost_Destination+this.cost;
|
||||
return this.cost_Destination+super.getRealCost();
|
||||
}
|
||||
|
||||
|
||||
public double getCost_Destination() {
|
||||
return cost_Destination;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue