Etape 4 terminée

This commit is contained in:
Nabzzz 2020-04-24 18:20:50 +02:00
parent 1b6ff22b79
commit a90cae2805
3 changed files with 95 additions and 10 deletions

View file

@ -4,8 +4,10 @@ package org.insa.graphs.algorithm.shortestpath;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
@ -34,42 +36,65 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
tabLabel[data.getOrigin().getId()].setCout(0);
tas.insert(tabLabel[data.getOrigin().getId()]);
this.notifyOriginProcessed(data.getOrigin());
//int nbMarque=0;
int iterations=0;
while(!tabLabel[data.getDestination().getId()].isMarque())
{
Node X=tas.findMin().getSommetCourant();
iterations++;
Node X;
try
{
X=tas.findMin().getSommetCourant();
}
catch(org.insa.graphs.algorithm.utils.EmptyPriorityQueueException e)
{
System.out.println("Tas vide \n");
break;
}
tabLabel[X.getId()].setMarque(true);
this.notifyNodeMarked(X);
System.out.println("Coût: "+tas.findMin().getCout()+"\n");
//nbMarque++;
try
{
tas.remove(tabLabel[X.getId()]);
}
catch(ElementNotFoundException e) {System.out.println("The element was not found in the binary heap \n");}
int nbSuccesseurs=0;
for(Arc a: X.getSuccessors())
{
nbSuccesseurs++;
Node Y=a.getDestination();
if(!tabLabel[Y.getId()].isMarque())
{
//double cout_avant=tabLabel[Y.getId()].getCout();
if(((tabLabel[X.getId()].getCout()+a.getLength())<tabLabel[Y.getId()].getCout()) && data.isAllowed(a))
{ tabLabel[Y.getId()].setCout(tabLabel[X.getId()].getCout()+a.getLength());
if(((tabLabel[X.getId()].getCout()+data.getCost(a))<tabLabel[Y.getId()].getCout()) && data.isAllowed(a))
{ tabLabel[Y.getId()].setCout(tabLabel[X.getId()].getCout()+data.getCost(a));
try
{
tas.remove(tabLabel[Y.getId()]);
tas.insert(tabLabel[Y.getId()]);
tabLabel[Y.getId()].setPere(a);
}
catch(ElementNotFoundException e) {tas.insert(tabLabel[Y.getId()]);
tabLabel[Y.getId()].setPere(a);}
tabLabel[Y.getId()].setPere(a);
this.notifyNodeReached(Y);}
}
}
}
if(nbSuccesseurs==X.getNumberOfSuccessors())
{
System.out.println("Nombre de successeurs OK \n");
}
else
{
System.out.println("PROBLEME: Nombre de successeurs NOT OK ! ! ! ! \n");
}
}
// Destination has no predecessor, the solution is infeasible...
@ -77,16 +102,76 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
this.notifyDestinationReached(data.getDestination());
ArrayList<Arc> arcs = new ArrayList<>();
Arc arc = tabLabel[data.getDestination().getId()].getPere();
while (arc != null) {
arcs.add(arc);
arc = tabLabel[arc.getOrigin().getId()].getPere();
}
System.out.println("Nombre d'arcs: "+arcs.size() + "\n");
System.out.println("Nombre d'itérations: "+iterations + "\n");
// Reverse the path...
Collections.reverse(arcs);
//Vérification grâce à Path.createShortestPathFromNodes
Path p= new Path(graph,arcs);
//List<Node> noeuds=new ArrayList<Node>();
float cout_total=0;
for(Arc a: arcs)
{
cout_total += a.getLength();
}
if(p.getLength()==cout_total)
{
System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à Dijkstra \n");
}
else
{
System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à Dijkstra \n");
System.out.println("Cout Dijktra="+cout_total);
System.out.println("Cout Dijktra="+p.getLength());
}
/*noeuds.add(data.getDestination());
if(data.getMode()==Mode.LENGTH)
{
Path shortestpath=Path.createShortestPathFromNodes(graph, noeuds);
if(shortestpath.getLength()==p.getLength())
{
System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à Dijkstra \n");
}
else
{
System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à Dijkstra \n");
}
}
else
{
Path fastestpath=Path.createFastestPathFromNodes(graph, noeuds);
if(fastestpath.getMinimumTravelTime()==p.getMinimumTravelTime())
{
System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à Dijkstra \n");
}
else
{
System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à Dijkstra \n");
}
}*/
//Vérification de la validation du path
if(p.isValid())
{
System.out.println("Félicitation votre path est valide \n");
}
else
{
System.out.println("Erreur! votre path est invalide \n");
}
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}