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 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;
@ -16,74 +14,11 @@ 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;
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()));
}
}

View file

@ -2,9 +2,7 @@ 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;
@ -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
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()){
Label a= new Label(x,Double.MAX_VALUE,null);
Label a= CreationLabel(x,Double.MAX_VALUE,null);
List_Label.add(x.getId(), a);
}
else{
Label a = new Label(data.getOrigin(), 0, null);
Label a = CreationLabel(data.getOrigin(), 0.0, null);
List_Label.add(data.getOrigin().getId(), a);
}
@ -54,21 +57,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
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()){
if (x.getRealCost()+data.getCost(suivant) < l.getRealCost()){
changé = true;
}
if(changé){
if (l.getTotalCost() != Double.MAX_VALUE){
if (l.getRealCost() != Double.MAX_VALUE){
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);
l.setParent(suivant);
}
notifyNodeReached(suivant.getDestination());
}
@ -83,7 +91,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
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

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

View file

@ -7,23 +7,20 @@ import org.insa.graphs.model.Node;
public class LabelStar extends Label{
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);
this.cost_Origin = cost_Origin;
super(sommet, cost, parent);
this.cost=cost;
this.cost_Destination=cost_Destination;
}
@Override
public int compareTo(Label a) {
return super.compareTo(a);
}
@Override
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;
}
}