fin de Dijkstra et A *, avancé sur la classe de tests Launch
This commit is contained in:
parent
c6814121a4
commit
07e85edc3c
9 changed files with 114 additions and 62 deletions
|
|
@ -16,10 +16,13 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public LabelStar newLabelStar(Node s, ShortestPathData data){
|
||||
return new LabelStar(s, false, Integer.MAX_VALUE, null, s.getPoint().distanceTo(data.getDestination().getPoint()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShortestPathSolution doRun() {
|
||||
|
||||
System.out.println("on est dans l'algo");
|
||||
// retrieve data from the input problem (getInputData() is inherited from the
|
||||
// parent class ShortestPathAlgorithm)
|
||||
final ShortestPathData data = getInputData();
|
||||
|
|
@ -30,20 +33,19 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
BinaryHeap<Label> tas = new BinaryHeap<Label>();
|
||||
|
||||
// variable that will contain the solution of the shortest path problem
|
||||
ShortestPathSolution solution = null;
|
||||
ShortestPathSolution solution;
|
||||
|
||||
// initialisation
|
||||
for(Node nod : graph.getNodes()){
|
||||
LabelStar l = new LabelStar(nod, false, Integer.MAX_VALUE, null, nod.getPoint().distanceTo(data.getDestination().getPoint()));
|
||||
listLabel[nod.getId()] = l ;
|
||||
listLabel[nod.getId()] = null ;
|
||||
if (nod.equals(data.getOrigin())){
|
||||
tas.insert(l);
|
||||
l.cout = 0 ;
|
||||
listLabel[nod.getId()] = new LabelStar(nod, false, 0, null, nod.getPoint().distanceTo(data.getDestination().getPoint()));
|
||||
tas.insert(listLabel[nod.getId()]);
|
||||
notifyOriginProcessed(nod);
|
||||
}
|
||||
}
|
||||
|
||||
LabelStar xl = tas.findMin();
|
||||
Label xl = tas.findMin();
|
||||
// iterations
|
||||
while (!tas.isEmpty() && xl.sommetCourant!= data.getDestination()){
|
||||
xl = tas.findMin();
|
||||
|
|
@ -53,14 +55,14 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
xl.marque = true ;
|
||||
for(Arc a : x.getSuccessors()){
|
||||
if(data.isAllowed(a)){
|
||||
if(listLabel[a.getDestination().getId()] == null){
|
||||
listLabel[a.getDestination().getId()] = newLabelStar(a.getDestination(), data);
|
||||
}
|
||||
Node n = a.getDestination();
|
||||
if (!listLabel[n.getId()].getMarque()){
|
||||
final var c = listLabel[n.getId()].cout ;
|
||||
final var w = listLabel[x.getId()].cout + data.getCost(a) ;
|
||||
if (c<w){
|
||||
listLabel[n.getId()].cout = c;
|
||||
}
|
||||
else{
|
||||
final var c = listLabel[n.getId()].getCost() ;
|
||||
final var w = listLabel[x.getId()].getCost() + data.getCost(a) ;
|
||||
if (c>w){
|
||||
if(listLabel[n.getId()].cout!=Integer.MAX_VALUE){
|
||||
tas.remove(listLabel[n.getId()]);
|
||||
} else {
|
||||
|
|
@ -79,7 +81,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
|
|||
// when the algorithm terminates, return the solution that has been found
|
||||
|
||||
|
||||
if (listLabel[data.getDestination().getId()].cout == Integer.MAX_VALUE) {
|
||||
if (listLabel[data.getDestination().getId()].getCost() == Integer.MAX_VALUE) {
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
for(Node nod : graph.getNodes()){
|
||||
listLabel[nod.getId()] = null ;
|
||||
if (nod.equals(data.getOrigin())){
|
||||
tas.insert(new Label(nod, false, 0, null));
|
||||
listLabel[nod.getId()] = new Label(nod, false, 0, null);
|
||||
tas.insert(listLabel[nod.getId()]);
|
||||
notifyOriginProcessed(nod);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
|
|
@ -11,6 +11,7 @@ import javax.swing.SwingUtilities;
|
|||
|
||||
import org.insa.graphs.algorithm.ArcInspector;
|
||||
import org.insa.graphs.algorithm.ArcInspectorFactory;
|
||||
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
|
||||
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
|
||||
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
||||
|
|
@ -51,60 +52,110 @@ public class Launch {
|
|||
|
||||
private static ShortestPathSolution paths ;
|
||||
private static ShortestPathSolution paths2 ;
|
||||
private static ShortestPathSolution paths3 ;
|
||||
private static DijkstraAlgorithm dijkstra ;
|
||||
private static BellmanFordAlgorithm bellman ;
|
||||
private static AStarAlgorithm AEtoile ;
|
||||
private static ShortestPathData data ;
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// visit these directory to see the list of available files on commetud.
|
||||
final String mapName =
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||
final String pathName =
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
|
||||
final String[] mapName ={
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haiti-and-domrep.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/french-polynesia.mapgr",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/fractal.mapgr"
|
||||
};
|
||||
final String[] pathName = {
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path",
|
||||
"/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path"
|
||||
};
|
||||
|
||||
final Graph graph;
|
||||
final Path path;
|
||||
final Graph[] graph = new Graph[5];
|
||||
final Path[] path = new Path[5];
|
||||
|
||||
// create a graph reader
|
||||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(mapName))))) {
|
||||
for (int i = 0 ; i<5 ; i++) {
|
||||
// create a graph reader
|
||||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(mapName[i]))))) {
|
||||
|
||||
// TODO: read the graph
|
||||
graph = reader.read();
|
||||
// TODO: read the graph
|
||||
graph[i] = reader.read();
|
||||
System.out.println("pas d'erreur au niveau du reader");
|
||||
}
|
||||
|
||||
// create the drawing
|
||||
final Drawing drawing = createDrawing();
|
||||
|
||||
// TODO: draw the graph on the drawing
|
||||
drawing.drawGraph(graph[i]);
|
||||
|
||||
// TODO: create a path reader
|
||||
try (final PathReader pathReader = new BinaryPathReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(pathName[i]))))) {
|
||||
|
||||
// TODO: read the path
|
||||
path[i] = pathReader.readPath(graph[i]);
|
||||
System.out.println("pas d'erreur au niveau du pathreader");
|
||||
}
|
||||
|
||||
// TODO: draw the path on the drawing
|
||||
drawing.drawPath(path[i]);
|
||||
|
||||
data = new ShortestPathData(graph[i], path[i].getOrigin(),path[i].getDestination(), ArcInspectorFactory.getAllFilters().get(i)) ;
|
||||
dijkstra = new DijkstraAlgorithm(data) ;
|
||||
bellman = new BellmanFordAlgorithm(data);
|
||||
AEtoile = new AStarAlgorithm(data) ;
|
||||
paths = dijkstra.doRun() ;
|
||||
paths2 = bellman.doRun() ;
|
||||
paths3 = AEtoile.doRun() ;
|
||||
|
||||
if (!paths.getPath().isValid()){
|
||||
System.out.println("Pour la carte " + i + ", c'est pas valide");
|
||||
|
||||
if (!paths.isFeasible()) {
|
||||
System.out.println("Chemin impossible");
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Pour la carte " + i + ", c'est valide");
|
||||
if (paths.isFeasible()) {
|
||||
System.out.println("Chemin possible");
|
||||
}
|
||||
//tester si l'origine = destination
|
||||
if (paths.getInputData().getOrigin() == paths.getInputData().getDestination()) {
|
||||
System.out.println("Origine = destination");
|
||||
}
|
||||
|
||||
if (Math.abs(paths.getPath().getLength()- paths2.getPath().getLength()) < 1e-6) {
|
||||
System.out.println("Même résultat entre dijkstra et bellman");
|
||||
}
|
||||
else {
|
||||
System.out.println("Résultat différent entre dijkstra et A*");
|
||||
}
|
||||
|
||||
if (Math.abs(paths.getPath().getLength()- paths3.getPath().getLength()) < 1e-6) {
|
||||
System.out.println("Même résultat entre dijkstra et A*");
|
||||
}else {
|
||||
System.out.println("Résultat différent entre dijkstra et A*");
|
||||
}
|
||||
|
||||
|
||||
if (i==2) { //on teste au niveau du temps --> demande un ArcInspectorFactory spécial
|
||||
if (Math.abs(paths.getPath().getMinimumTravelTime()- paths2.getPath().getMinimumTravelTime()) < 1e-6) {
|
||||
System.out.println("Même résultat");
|
||||
}
|
||||
if (paths.getPath().getMinimumTravelTime()==paths2.getPath().getMinimumTravelTime()) {
|
||||
System.out.println("Dijkstra donne le même résultat que Bellman");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// create the drawing
|
||||
final Drawing drawing = createDrawing();
|
||||
|
||||
// TODO: draw the graph on the drawing
|
||||
drawing.drawGraph(graph);
|
||||
|
||||
// TODO: create a path reader
|
||||
try (final PathReader pathReader = new BinaryPathReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(pathName))))) {
|
||||
|
||||
// TODO: read the path
|
||||
path = pathReader.readPath(graph);
|
||||
}
|
||||
|
||||
// TODO: draw the path on the drawing
|
||||
drawing.drawPath(path);
|
||||
|
||||
data = new ShortestPathData(graph, path.getOrigin(),path.getDestination(), ArcInspectorFactory.getAllFilters().get(0)) ;
|
||||
dijkstra = new DijkstraAlgorithm(data) ;
|
||||
bellman = new BellmanFordAlgorithm(data);
|
||||
paths = dijkstra.doRun() ;
|
||||
paths2 = bellman.doRun() ;
|
||||
if (paths.getPath().isValid()){
|
||||
System.out.println("c'est valide");
|
||||
}
|
||||
if (paths.getPath().getLength()==path.getLength()) {
|
||||
System.out.println("même longueur ");
|
||||
}
|
||||
if (paths.getPath().getLength()==paths2.getPath().getLength()) {
|
||||
System.out.println("Dijkstra donne le même résultat que Bellman");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -285,7 +285,6 @@ public class Path {
|
|||
* @param speed Speed to compute the travel time.
|
||||
* @return Time (in seconds) required to travel this path at the given speed (in
|
||||
* kilometers-per-hour).
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public double getTravelTime(double speed) {
|
||||
double l = 0;
|
||||
|
|
@ -301,7 +300,6 @@ public class Path {
|
|||
* every arc.
|
||||
*
|
||||
* @return Minimum travel time to travel this path (in seconds).
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public double getMinimumTravelTime() {
|
||||
// TODO:
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in a new issue