AStar fini, tests à faire

这个提交包含在:
Marie Brunetto 2023-04-12 18:16:26 +02:00
父节点 25e27cf5f5
当前提交 b5f89c379e
共有 5 个文件被更改,包括 37 次插入125 次删除

查看文件

@ -1,12 +1,7 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import org.insa.graphs.model.Node; 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 { public class AStarAlgorithm extends DijkstraAlgorithm {
@ -14,121 +9,19 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
super(data); super(data);
} }
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) {
super(data);
}
@Override @Override
protected ShortestPathSolution doRun() { public ArrayList<Label> Init(int tailleGraphe, Node destination)
final ShortestPathData data = getInputData(); {
Label currentLabel;
int tailleGraphe = data.getGraph().size(); ArrayList<Label> labelSommets = new ArrayList<Label>();
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++ ) for(int i = 0; i< tailleGraphe; i++ )
{ {
currentLabel = new LabelStar(data.getGraph().get(i), currentNode); currentLabel = new LabelStar(data.getGraph().get(i), destination);
labelSommets.add(currentLabel); labelSommets.add(currentLabel);
} }
return labelSommets;
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);
} }
}
} }

查看文件

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

查看文件

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

查看文件

@ -83,6 +83,7 @@ public class Launch {
protected final ArcInspector arcInspector; protected final ArcInspector arcInspector;
ShortestPathData theData = new ShortestPathData(graph, path.getOrigin(), path.getDestination(), 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); DijkstraAlgorithm dji = new DijkstraAlgorithm(theData);
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");*/ System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");*/