Astar bientot

This commit is contained in:
El Haji Fofana 2023-05-18 14:27:54 +02:00
parent 65f19a9876
commit 0d6cf379f4
6 changed files with 112 additions and 9 deletions

View file

@ -1,9 +1,89 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import java.lang.invoke.LambdaConversionException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Point;
public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) {
super(data);
}
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO:
ArrayList<Label> List_Label = new ArrayList<Label>(data.getGraph().size()); //Liste de labels
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
ArrayList<Arc> Arcs = new ArrayList<Arc>();
/* Initialise nos label */
for (Node x: data.getGraph().getNodes())
{
if(x != data.getOrigin()){
LabelStar a= new LabelStar(x,Double.MAX_VALUE,null,Point.distance(x.getPoint(),data.getDestination().getPoint()));
List_Label.add(x.getId(), a);
}
else{
LabelStar a = new LabelStar(data.getOrigin(), 0, null,Point.distance(x.getPoint(),data.getDestination().getPoint()));
List_Label.add(data.getOrigin().getId(), a);
}
}
Tas.insert(List_Label.get(data.getOrigin().getId()));
Label x;
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
x = Tas.findMin();
x.setMarque(true);
Tas.deleteMin();
for(Arc suivant : x.getSommet().getSuccessors()){
// Small test to check allowed roads...
if (!data.isAllowed(suivant)) {
continue;
}
Label l=List_Label.get(suivant.getDestination().getId());
if(!l.isMarque()){
Boolean changé = false;
if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){
changé = true;
}
if(changé){
if (l.getTotalCost() != Double.MAX_VALUE){
Tas.remove(l);
}
l.setCost(x.getTotalCost()+data.getCost(suivant));
Tas.insert(l);
l.setParent(suivant);
notifyNodeReached(suivant.getDestination());
}
}
}
}
Label dest =List_Label.get(data.getDestination().getId());
while (dest.getParent() != null){
Arcs.add(dest.getParent());
dest = List_Label.get(dest.getParent().getOrigin().getId());
}
Collections.reverse(Arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
return solution;
}
}

View file

@ -58,15 +58,15 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Label l=List_Label.get(suivant.getDestination().getId());
if(!l.isMarque()){
Boolean changé = false;
if (x.getCost()+data.getCost(suivant) < l.getCost()){
if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){
changé = true;
}
if(changé){
if (l.getCost() != Double.MAX_VALUE){
if (l.getTotalCost() != Double.MAX_VALUE){
Tas.remove(l);
}
l.setCost(x.getCost()+data.getCost(suivant));
l.setCost(x.getTotalCost()+data.getCost(suivant));
Tas.insert(l);
l.setParent(suivant);

View file

@ -22,9 +22,9 @@ public class Label implements Comparable<Label> {
@Override
public int compareTo(Label a) {
int result;
if (a.getCost() < cost){
if (a.getTotalCost() < cost){
result = 1;
}else if (a.getCost()>cost){
}else if (a.getTotalCost()>cost){
result = -1;
}else {
result = 0;
@ -38,7 +38,7 @@ public class Label implements Comparable<Label> {
public boolean isMarque() {
return marque;
}
public double getCost() {
public double getTotalCost() {
return cost;
}
public Arc getParent() {

View file

@ -9,7 +9,7 @@ public class LabelStar extends Label {
private double cost_Destination;
private double cost_Origin;
public LabelStar(Node sommet, double cost_Origin,double cost_Destination, Arc parent)
public LabelStar(Node sommet, double cost_Origin, Arc parent,double cost_Destination)
{
super(sommet, cost_Origin, parent);
this.cost_Origin = cost_Origin;
@ -17,10 +17,33 @@ public class LabelStar extends Label {
}
@Override
public int compareTo(Label a) {
return super.compareTo(a);
}
@Override
public double getTotalCost() {
return cost_Destination+cost_Origin;
}
public double getCost_Destination() {
return cost_Destination;
}
public void setCost_Destination(double cost_Destination) {
this.cost_Destination = cost_Destination;
}
public double getCost_Origin() {
return cost_Origin;
}
public void setCost_Origin(double cost_Origin) {
this.cost_Origin = cost_Origin;
}
}