Ajout de tests Djikstra qui marchent tous popur l'instant

This commit is contained in:
knzrd 2025-05-26 22:35:05 +02:00
parent ced572abaa
commit 2e6b971790

View file

@ -6,6 +6,7 @@ import java.io.FileInputStream;
import java.util.List;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
@ -15,7 +16,9 @@ import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
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;
public class TestDijkstra {
/* Définissez plusieurs scénarios. Chaque scénario est défini par :
une carte (tester des cartes routières et non routières)
@ -45,37 +48,32 @@ Automated: No need for manual launching or visual inspection.*/
Graph graph = null;
Path path = null;
// create a graph reader
/* try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName))))) {
// TODO: read the graph
graph = reader.read();
reader.close();
// fonction pour recuperer les paths
public static Path pathlecture(Graph graph,String pathName) {
try (final PathReader reader = new BinaryPathReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))))) {
return reader.readPath(graph);
} catch (Exception e) {
System.err.println("Erreur lors de la lecture du chemin : " + e.getMessage());
e.printStackTrace();
return null;
}
}
// fonction pour recuperer les graphs
public static Graph graphlecture(String mapName) {
try (final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))))) {
return reader.read();
} catch (Exception e) {
System.err.println("Erreur lors de la lecture du graphe : " + e.getMessage());
e.printStackTrace();
return null;
}
}
// TODO: create a path reader
// Je suis et c'est que je dois faire les changements nécessaires
try (final PathReader pathReader = new BinaryPathReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(pathName))))) {
// TODO: read the path
path = pathReader.readPath(graph);
}*/
// fonction pour recuperer les paths
// create a graph reader
void graphlecture (Graph graph,String mapName) throws Exception {
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
new BufferedInputStream(new FileInputStream(mapName))))) {
// TODO: read the graph
graph = reader.read();
reader.close();
}}
// Scénario 1 : trajet court petite map
final String mapName1= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
final String pathName1 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
// Scénario 1 : trajet court petite map pieton
final static String mapName1= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
final static String pathName1 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
// Scénario 2 : trajet long petite map
final String mapName2= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
Graph graphe2;
@ -83,8 +81,8 @@ Graph graphe2;
// Scénario 3: trajet court grande map
final String mapName3= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
final String pathName3 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path";
final static String mapName3= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
final static String pathName3 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path";
// Scénario 4: trajet long grande map
final String mapName4= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
@ -109,21 +107,24 @@ final String mapName8= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps
/* Tests pour l'ensembles des scénarios*/
// Vérifier la validité du chemin : utilisation de .isvalid()
// fonction verifiant que deux chemins sont les memes
public static boolean bonchemin (Path chemintrouve,Path cheminrecherche){
// fonction qui extrait le chemin valide
public static boolean boncheminDijkstra (ShortestPathData Data,Path chemindepart){
// on regarde d'abord si les chemins sont valides
if(!(chemintrouve.isValid()) || !(cheminrecherche.isValid()) ){
Path chemintrouve = Path_Dijkstra(Data);
if(!(chemintrouve.isValid()) || !(chemindepart.isValid()) ){
System.out.println(" chemin(s) non valide");
return false;
}
// on verifie si les chemins ont le meme nombre de noeuds
if(chemintrouve.size() != cheminrecherche.size()){
if(chemintrouve.size() != chemindepart.size()){
System.out.println(" chemins de taille différente");
return false;
}
// recuperation liste des arcs
List<Arc> arcstrouves = chemintrouve.getArcs();
List<Arc> arcsrecherches = cheminrecherche.getArcs();
List<Arc> arcsrecherches = chemindepart.getArcs();
// On verifie si les chemins utilisent les mêmes arcs
for (int i=0; i<chemintrouve.getArcs().size();i++){
// on recupere les arcs de chaque iteration
@ -138,51 +139,176 @@ public static boolean bonchemin (Path chemintrouve,Path cheminrecherche){
return true;
}
// Pour petites cartes
// Recuperer origine
//public static Node getOrigin
// Vérifier que le coût en temps est similaire entre Dijkstra et Bellman-Ford
public static boolean memetemps (Graph graph,Node Origin,Node Destination,ArcInspector arcInspector){
ShortestPathData Data = new ShortestPathData(graph, Origin, Destination, arcInspector);
// Recuperation Path de Dijkstra
// Recuperation Path de Dijkstra
public static Path Path_Dijkstra(ShortestPathData Data){
DijkstraAlgorithm Dijkstra = new DijkstraAlgorithm(Data);
ShortestPathSolution SolutionDijkstra = Dijkstra.run();
Path PathDijkstra =SolutionDijkstra.getPath();
// Recuperation Path de BellmanFord
return SolutionDijkstra.getPath();
}
// Recuperation Path de Dijkstra
public static Path Path_BellmanFord(ShortestPathData Data){
BellmanFordAlgorithm BellmanFord = new BellmanFordAlgorithm(Data);
ShortestPathSolution SolutionBellmanFord = BellmanFord.run();
Path PathBellmanFord =SolutionBellmanFord.getPath();
// Verifier si un path existe avant de verifier sa validite pour eviter probs de pointeur
if (PathDijkstra == null || PathBellmanFord == null) {
return SolutionBellmanFord.getPath();
}
// Verification existance de deux Paths
public static boolean ExistancePaths(Path PathDijkstra,Path PathBellmanFord){
if (PathDijkstra == null || PathBellmanFord == null) {
System.out.println("Path(s) null(s)");
return false;
}
// Verification de la validité des Path
if(!(PathDijkstra.isValid())|| !(PathBellmanFord.isValid()) ){
else{ return true;}
}
// Verification validite de deux Paths
public static boolean ValiditePaths(Path PathDijkstra,Path PathBellmanFord){
if(!(PathDijkstra.isValid())|| !(PathBellmanFord.isValid()) ){
System.out.println("Path(s) non valide(s)");
return false;
}
//Les deux doivent avoir trouvé un path
// if (SolutionDijkstra.getStatus() != AbstractSolution.Status.OPTIMAL || SolutionBellman.getStatus() != AbstractSolution.Status.OPTIMAL) {
// return false;
//}
// verification de la similarite en temps avec un certain epsilon
double CostDijkstra = PathDijkstra.getMinimumTravelTime();
double CostBellmanFord = PathBellmanFord.getMinimumTravelTime();
double epsilon = 1e-6;
if(Math.abs(CostDijkstra - CostBellmanFord)>epsilon){
System.out.println("Dijkstra et BellmanFord on des coûts différents");
else{ return true;}
}
// Recuperer origine d'un path
//public static Node getOrigin
// Vérifier que le coût en temps est similaire entre Dijkstra et Bellman-Ford
public static boolean memetemps (ShortestPathData Data){
// Recuperation Path de Dijkstra
Path PathDijkstra =Path_Dijkstra(Data);
// Recuperation Path de BellmanFord
Path PathBellmanFord =Path_BellmanFord(Data);
// Verifier si un path existe avant de verifier sa validite pour eviter probs de pointeur
if (!ExistancePaths(PathDijkstra, PathBellmanFord)){
return false;
}
// Verification de la validité des Path
if(!ValiditePaths(PathDijkstra, PathBellmanFord)){
return false;
}
// verification du cout en temps
double CostDijkstra = PathDijkstra.getMinimumTravelTime();
double CostBellmanFord = PathBellmanFord.getMinimumTravelTime();
if(CostDijkstra != CostBellmanFord){
System.out.println("Dijkstra et BellmanFord ont des temps différents de parcours");
return false;
}
System.out.println("Dijkstra et BellmanFord ont les même temps de parcours");
return true;
}
// Vérifier que le coût en distance est similaire entre Dijkstra et Bellman-Ford
public static boolean memedistance (ShortestPathData Data){
// Recuperation Path de Dijkstra
Path PathDijkstra =Path_Dijkstra(Data);
// Recuperation Path de BellmanFord
Path PathBellmanFord =Path_BellmanFord(Data);
// Verifier si un path existe avant de verifier sa validite pour eviter probs de pointeur
if (!ExistancePaths(PathDijkstra, PathBellmanFord)){
return false;
}
// Verification de la validité des Path
if(!ValiditePaths(PathDijkstra, PathBellmanFord)){
return false;
}
// verification de la similarite en distance
double CostDijkstra = PathDijkstra.getLength();
double CostBellmanFord = PathBellmanFord.getLength();
if(CostDijkstra != CostBellmanFord){
System.out.println("Dijkstra et BellmanFord ont des distances différentes de parcours");
return false;
}
System.out.println("Dijkstra et BellmanFord ont les même distances de parcours");
return true;
}
public static boolean memepath (ShortestPathData Data){
// Recuperation Path de Dijkstra
Path PathDijkstra =Path_Dijkstra(Data);
// Recuperation Path de BellmanFord
Path PathBellmanFord =Path_BellmanFord(Data);
// Verifier si un path existe avant de verifier sa validite pour eviter probs de pointeur
if (!ExistancePaths(PathDijkstra, PathBellmanFord)){
return false;
}
// Verification de la validité des Path
if(!ValiditePaths(PathDijkstra, PathBellmanFord)){
return false;
}
// verification de la similarite des paths
// comparer l'ensemble des origines et destination des arcs et leurs couts
List<Arc> ArcsDijkstra = PathDijkstra.getArcs();
List<Arc> ArcsBellmanFord = PathBellmanFord.getArcs();
// Verifier si on a la meme taille d'arc
if(ArcsDijkstra.size() != ArcsBellmanFord.size()){
return false;
}
// Verifier que les arcs ont même origine et même destination
for (int i=0;i<ArcsDijkstra.size();i++){
// Recuperation des Origines et Destinations
Node OrigineDijkstra = ArcsDijkstra.get(i).getOrigin();
Node OrigineBellmanFord = ArcsBellmanFord.get(i).getOrigin();
Node DestinationDijkstra = ArcsDijkstra.get(i).getDestination();
Node DestinationBellmanFord = ArcsBellmanFord.get(i).getDestination();
// Recuperation du cout des arcs
Double tempsArcDijkstra = ArcsDijkstra.get(i).getMinimumTravelTime();
Double tempsArcBellmanFord = ArcsBellmanFord.get(i).getMinimumTravelTime();
float distanceArcDijkstra = ArcsDijkstra.get(i).getLength();
float distanceArcBellmanFord = ArcsBellmanFord.get(i).getLength();
if(OrigineDijkstra.getId()!= OrigineBellmanFord.getId()|| DestinationDijkstra.getId()!= DestinationBellmanFord.getId() ){
System.out.println("A partir de l'arc"+i+"les paths ne sont pas les mêmes");
return false;
}
if(tempsArcDijkstra != tempsArcBellmanFord || distanceArcDijkstra!= distanceArcBellmanFord ){
System.out.println("A partir de l'arc"+i+"les paths n'ont plus la meme taille");
return false;
}
}
System.out.println("Dijkstra et BellmanFord ont les même paths");
return true;
}
// Pour grandes cartes
// Verifier si le path obtenu est celui voulu
public static void main(String[] args) {
//graphlecture(graphe2,mapName2);
//Node Origin = Graphe2.get(1333);
// recuperation des arcInspectors
List<ArcInspector> filters = ArcInspectorFactory.getAllFilters();
ArcInspector Nofilterbylength = filters.get(0);
ArcInspector OnlyCarByLength = filters.get(1);
ArcInspector OnlyCarByTime= filters.get(2);
ArcInspector OnlyPedestrianByTime = filters.get(3);
//final String mapName1= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
//final String pathName1 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
// Recuperation du graph et path de la situation
Graph graph = graphlecture(mapName1);
Path pathrecupere1 = pathlecture(graph, pathName1);
// Recuperation du path realise par Dijkstra
ShortestPathData Data1 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getDestination(),Nofilterbylength);
// verification du bon chemin
if(boncheminDijkstra(Data1, pathrecupere1)){
System.out.println("L'algorithme Dijkstra donne le bon path");
}
else{System.out.println("L'algorithme Dijkstra ne donne pas le bon path");}
// Comparaison avec Bellman Ford
// Comparaison de cout en distance
//if(memedistance(Data1)){
//}
}
}