AStar fini, tests à faire

This commit is contained in:
Marie Brunetto 2023-04-12 18:16:26 +02:00
parent 25e27cf5f5
commit b5f89c379e
5 changed files with 37 additions and 125 deletions

View file

@ -1,12 +1,7 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Collections;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Path;
import org.insa.graphs.algorithm.AbstractSolution;
import org.insa.graphs.algorithm.utils.BinaryHeap;
public class AStarAlgorithm extends DijkstraAlgorithm {
@ -14,121 +9,19 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
super(data);
}
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) {
super(data);
}
@Override
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
int tailleGraphe = data.getGraph().size();
int index = 0;
ArrayList<LabelStar> labelSommets = new ArrayList<LabelStar>();
LabelStar currentLabel;
BinaryHeap<LabelStar> leTas = new BinaryHeap<LabelStar>();
//Initialisation ----------------------------------------------------
Node currentNode = data.getDestination() ;
for(int i = 0; i< tailleGraphe; i++ )
{
currentLabel = new LabelStar(data.getGraph().get(i), currentNode);
labelSommets.add(currentLabel);
}
currentLabel = labelSommets.get(data.getOrigin().getId());
currentLabel.setNouveauChemin(null, 0);
currentLabel.setMarqueTrue();
leTas.insert(currentLabel);
currentNode = data.getOrigin() ;
double currentCost;
double newCost;
LabelStar newLabel;
notifyOriginProcessed(currentNode);
//Boucle principale ----------------------------------------------------
while (!leTas.isEmpty() && !currentNode.equals(data.getDestination()))
{
currentLabel = leTas.deleteMin();
currentNode = currentLabel.getSommetCourant();
index = currentNode.getId();
currentLabel.setMarqueTrue();
notifyNodeMarked(currentNode);
for(Arc arc : currentNode.getSuccessors())
{
if(data.isAllowed(arc))
{
newLabel = labelSommets.get(arc.getDestination().getId());
if(!newLabel.getMarque())
{
currentCost = newLabel.getTotalCost();
newCost = data.getCost(arc) + currentLabel.getTotalCost();
if(currentCost == -1.0)
{
newLabel.setNouveauChemin(arc, newCost);
leTas.insert(newLabel);
notifyNodeReached(newLabel.getSommetCourant());
}
else if(newCost < currentCost)
{
leTas.remove(newLabel);
newLabel.setNouveauChemin(arc, newCost);
leTas.insert(newLabel);
}
}
}
}
}
//Retour chemin obtenu ----------------------------------------------------
Node finalNode = data.getDestination();
index = 0;
ArrayList<Arc> arcListe = new ArrayList<Arc>() ;
if(labelSommets.get(data.getDestination().getId()).getCost() == -1)
{
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}
while(!finalNode.equals(data.getOrigin()) && index < tailleGraphe)
{
arcListe.add(labelSommets.get(finalNode.getId()).pere);
finalNode = arcListe.get(arcListe.size()-1).getOrigin();
index++;
}
if(index != tailleGraphe)
{
Collections.reverse(arcListe);
notifyDestinationReached(data.getDestination());
return new ShortestPathSolution(data, AbstractSolution.Status.FEASIBLE, new Path(data.getGraph(),arcListe ));
}
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
@Override
public ArrayList<Label> Init(int tailleGraphe, Node destination)
{
Label currentLabel;
ArrayList<Label> labelSommets = new ArrayList<Label>();
for(int i = 0; i< tailleGraphe; i++ )
{
currentLabel = new LabelStar(data.getGraph().get(i), destination);
labelSommets.add(currentLabel);
}
return labelSommets;
}
}

View file

@ -10,6 +10,23 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public ArrayList<Label> Init(int tailleGraphe, Node destination)
{
Label currentLabel;
ArrayList<Label> labelSommets = new ArrayList<Label>();
for(int i = 0; i< tailleGraphe; i++ )
{
currentLabel = new Label(data.getGraph().get(i));
labelSommets.add(currentLabel);
}
return labelSommets;
}
public DijkstraAlgorithm(ShortestPathData data) {
super(data);
}
@ -23,17 +40,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ArrayList<Label> labelSommets = new ArrayList<Label>();
Label currentLabel;
BinaryHeap<Label> leTas = new BinaryHeap<Label>();
//Initialisation ----------------------------------------------------
for(int i = 0; i< tailleGraphe; i++ )
{
currentLabel = new Label(data.getGraph().get(i));
labelSommets.add(currentLabel);
}
labelSommets = Init(tailleGraphe, data.getDestination());
Label currentLabel;
currentLabel = labelSommets.get(data.getOrigin().getId());

View file

@ -50,6 +50,11 @@ public class Label implements Comparable<Label>{
return this.coutRealise ;
}
public double getEstimation() {
return 0 ;
}
/* Getter pour pere */
public Arc getPere() {
return this.pere ;

View file

@ -5,7 +5,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
//import org.insa.graphs.model.Point;
public class LabelStar extends Label {
public class LabelStar extends Label{
/* ATTRIBUT */

View file

@ -83,6 +83,7 @@ public class Launch {
protected final ArcInspector arcInspector;
ShortestPathData theData = new ShortestPathData(graph, path.getOrigin(), path.getDestination(), arcInspector);
//ArcInspectorFactory liste d'ArcInspectors avec permissions etc existants en static
DijkstraAlgorithm dji = new DijkstraAlgorithm(theData);
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");*/