Début AStar
This commit is contained in:
parent
734fa38388
commit
70c2dfee46
2 changed files with 148 additions and 7 deletions
|
@ -1,9 +1,134 @@
|
|||
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 {
|
||||
|
||||
public AStarAlgorithm(ShortestPathData data) {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,13 @@ import org.insa.graphs.model.io.BinaryGraphReader;
|
|||
import org.insa.graphs.model.io.BinaryPathReader;
|
||||
import org.insa.graphs.model.io.GraphReader;
|
||||
import org.insa.graphs.model.io.PathReader;
|
||||
import org.insa.graphs.algorithm.ArcInspector;
|
||||
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
||||
/*
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;*/
|
||||
|
||||
public class Launch {
|
||||
|
||||
|
@ -47,31 +54,40 @@ public class Launch {
|
|||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// Visit these directory to see the list of available files on Commetud.
|
||||
final String mapName = "/home/norgeux/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||
final String pathName = "/home/norgeux/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
|
||||
final String mapName = "/home/brunetto/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||
final String pathName = "/home/brunetto/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
|
||||
|
||||
// Create a graph reader.
|
||||
final GraphReader reader = new BinaryGraphReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
|
||||
|
||||
// TODO: Read the graph.
|
||||
// Read the graph.
|
||||
final Graph graph = reader.read();
|
||||
|
||||
// Create the drawing:
|
||||
final Drawing drawing = createDrawing();
|
||||
|
||||
// TODO: Draw the graph on the drawing.
|
||||
// Draw the graph on the drawing.
|
||||
drawing.drawGraph(graph);
|
||||
|
||||
// TODO: Create a PathReader.
|
||||
// Create a PathReader.
|
||||
final PathReader pathReader = new BinaryPathReader(
|
||||
new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))));
|
||||
|
||||
// TODO: Read the path.
|
||||
// Read the path.
|
||||
final Path path = pathReader.readPath(graph);
|
||||
|
||||
// TODO: Draw the path.
|
||||
// Draw the path.
|
||||
drawing.drawPath(path);
|
||||
/*
|
||||
protected final ArcInspector arcInspector;
|
||||
|
||||
ShortestPathData theData = new ShortestPathData(graph, path.getOrigin(), path.getDestination(), arcInspector);
|
||||
DijkstraAlgorithm dji = new DijkstraAlgorithm(theData);
|
||||
|
||||
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue