Astar probleme

This commit is contained in:
El Haji Fofana 2023-05-18 18:09:55 +02:00
parent 0d6cf379f4
commit c5072b4729
6 changed files with 46 additions and 106 deletions

View file

@ -2,9 +2,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.AbstractSolution.Status;
import java.lang.invoke.LambdaConversionException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Collections; import java.util.Collections;
import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
@ -16,74 +14,11 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) { public AStarAlgorithm(ShortestPathData data) {
super(data); super(data);
} }
@Override @Override
protected ShortestPathSolution doRun() { public Label CreationLabel(Node x, Double cout, Arc parent){
final ShortestPathData data = getInputData(); ShortestPathData data = getInputData();
ShortestPathSolution solution = null; return new LabelStar(x,cout,parent,Point.distance(x.getPoint(), data.getDestination().getPoint()));
// 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

@ -2,9 +2,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.algorithm.AbstractSolution.Status; import org.insa.graphs.algorithm.AbstractSolution.Status;
import java.lang.invoke.LambdaConversionException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Collections; import java.util.Collections;
import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
@ -18,25 +16,30 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
public Label CreationLabel(Node x, Double cout, Arc parent){
return new Label(x,cout,parent);
}
@Override @Override
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData(); final ShortestPathData data = getInputData();
ShortestPathSolution solution = null; ShortestPathSolution solution = null;
// TODO:
ArrayList<Label> List_Label = new ArrayList<Label>(data.getGraph().size()); //Liste de labels ArrayList<Label> List_Label = new ArrayList<Label>(data.getGraph().size()); //Liste de labels
BinaryHeap<Label> Tas = new BinaryHeap<Label>(); BinaryHeap<Label> Tas = new BinaryHeap<Label>();
ArrayList<Arc> Arcs = new ArrayList<Arc>(); ArrayList<Arc> Arcs = new ArrayList<Arc>();
/* Initialise nos label */ /* Initialise nos label */
for (Node x: data.getGraph().getNodes()) for (Node x: data.getGraph().getNodes())
{ {
if(x != data.getOrigin()){ if(x != data.getOrigin()){
Label a= new Label(x,Double.MAX_VALUE,null); Label a= CreationLabel(x,Double.MAX_VALUE,null);
List_Label.add(x.getId(), a); List_Label.add(x.getId(), a);
} }
else{ else{
Label a = new Label(data.getOrigin(), 0, null); Label a = CreationLabel(data.getOrigin(), 0.0, null);
List_Label.add(data.getOrigin().getId(), a); List_Label.add(data.getOrigin().getId(), a);
} }
@ -54,21 +57,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
if (!data.isAllowed(suivant)) { if (!data.isAllowed(suivant)) {
continue; continue;
} }
Label l=List_Label.get(suivant.getDestination().getId()); Label l=List_Label.get(suivant.getDestination().getId());
if(!l.isMarque()){ if(!l.isMarque()){
Boolean changé = false; Boolean changé = false;
if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){ if (x.getRealCost()+data.getCost(suivant) < l.getRealCost()){
changé = true; changé = true;
} }
if(changé){ if(changé){
if (l.getTotalCost() != Double.MAX_VALUE){ if (l.getRealCost() != Double.MAX_VALUE){
Tas.remove(l); Tas.remove(l);
l.setCost(x.getRealCost()+data.getCost(suivant));
Tas.insert(l);
l.setParent(suivant);
} }
l.setCost(x.getTotalCost()+data.getCost(suivant)); else{
l.setCost(x.getRealCost()+data.getCost(suivant));
Tas.insert(l); Tas.insert(l);
l.setParent(suivant); l.setParent(suivant);
}
notifyNodeReached(suivant.getDestination()); notifyNodeReached(suivant.getDestination());
} }
@ -83,7 +91,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Arcs.add(dest.getParent()); Arcs.add(dest.getParent());
dest = List_Label.get(dest.getParent().getOrigin().getId()); dest = List_Label.get(dest.getParent().getOrigin().getId());
} }
Collections.reverse(Arcs); Collections.reverse(Arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs)); solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
return solution; return solution;

View file

@ -22,12 +22,20 @@ public class Label implements Comparable<Label> {
@Override @Override
public int compareTo(Label a) { public int compareTo(Label a) {
int result; int result;
if (a.getTotalCost() < cost){ if (this.getTotalCost() < a.cost){
result = 1;
}else if (a.getTotalCost()>cost){
result = -1; result = -1;
}else if (this.getTotalCost()>a.cost){
result = 1;
}else { }else {
result = 0; if(this.getTotalCost() == this.cost){
result=1;
}
else if (this.cost <a.cost){
result = -1;
}
else{
result = 0;
}
} }
return result; return result;
} }
@ -41,6 +49,11 @@ public class Label implements Comparable<Label> {
public double getTotalCost() { public double getTotalCost() {
return cost; return cost;
} }
public double getRealCost()
{
return cost;
}
public Arc getParent() { public Arc getParent() {
return parent; return parent;
} }

View file

@ -7,23 +7,20 @@ 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_Origin; private double cost;
public LabelStar(Node sommet, double cost_Origin, Arc parent,double cost_Destination)
public LabelStar(Node sommet, double cost, Arc parent,double cost_Destination)
{ {
super(sommet, cost_Origin, parent); super(sommet, cost, parent);
this.cost_Origin = cost_Origin; this.cost=cost;
this.cost_Destination=cost_Destination; this.cost_Destination=cost_Destination;
} }
@Override
public int compareTo(Label a) {
return super.compareTo(a);
}
@Override @Override
public double getTotalCost() { public double getTotalCost() {
return cost_Destination+cost_Origin; return this.cost_Destination+this.cost;
} }
@ -32,18 +29,6 @@ public class LabelStar extends Label{
} }
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;
}
} }