rectification des algos importants

This commit is contained in:
knzrd 2025-05-27 21:02:47 +02:00
parent 60e195fd03
commit 70844f91f0
5 changed files with 625 additions and 91 deletions

View file

@ -8,9 +8,7 @@ import java.util.List;
import org.insa.graphs.algorithm.AbstractSolution;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
@ -85,20 +83,20 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
double gNew = cout_realise + costArc;
if (gNew < succLabel.getCost()) {
succLabel.setcoutrealise(gNew);
succLabel.setarcperefils(arc);
notifyNodeReached(succ);
try {
heap.remove(succLabel);
heap.insert(succLabel);
} catch (ElementNotFoundException e) {
heap.insert(succLabel);
if(gNew<succLabel.getCost()) {
LabelStar labelchanger = labels[succ.getId()];
if(succLabel.getCost() != Double.POSITIVE_INFINITY){
heap.remove(labelchanger);
}
// si le coût nv est meilleur on update le cout du sommet au sein du label
labelchanger.setcoutrealise(gNew);
// on indique que l'arc pere est arcscourants
labelchanger.setarcperefils(arc);
// On notifie le fait qu'on a un nouveau node accessible
notifyNodeReached(labelchanger.getsommetcourant());
heap.insert(labelchanger);
}
}
}
}

View file

@ -2,10 +2,10 @@ package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.List;
import org.insa.graphs.algorithm.AbstractSolution;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
@ -46,7 +46,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// on crée une variable permettant d'avoir le dernier label utilisé
Label labelcourant;
// cas trivial: node d'origine et st le meme que le node destination
/* if(origin.equals(destination) ){
/*if(origin.equals(destination) ){
notifyOriginProcessed(origin);
notifyDestinationReached(destination);
Path cheminvide = new Path(graph);

View file

@ -71,18 +71,18 @@ public class Label implements Comparable<Label>{
// on override la méthode de comparaison
@Override
/* @Override
public int compareTo(Label other){
return Double.compare(this.getCost(),other.getCost());
}
} */
//car on va utiliser priority avce les plus petit distances car on a une binary heap
// ces labels sont associé à chaque noeud : les noeuds du graphes sont numérotés de 0 à N-1
/*@Override
@Override
public int compareTo(Label other) {
// Compare via getTotalCost plutôt que directement getCost
return Double.compare(this.getTotalCost(), other.getTotalCost());
} */
}
}

View file

@ -0,0 +1,502 @@
package org.insa.graphs.algorithm.utils;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.List;
import org.insa.graphs.algorithm.AbstractSolution;
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;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
import org.insa.graphs.model.Arc;
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 TestAstar {
Graph graph = null;
Path path = null;
// fonction pour recuperer les paths des scenarios
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 des scenarios
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;
}
}
// Path et Map utilises pour le premier scenario: pour les 4 filtres possibles
// meme Map utilise pour le cas ou l'origine et la destination sont les memes
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";
// Meme map utilise Scénario 5: Chemin inexistant cas 1: origine et destination dans deux composantes connexes différentes
// Scénario 2: trajet long grande map
final static String mapName2 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
// Autre scenarios envisagees mais non presents dans le code car repetitf
//trajet court pour une grande map
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 5: Chemin inexistant cas 1: origine et destination dans deux composantes connexes différentes
final static String mapName5= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/french-polynesia.mapgr";
// Vérifier si le chemin est faisable pour Astar
public static boolean SolutionInfaisableAstar (ShortestPathData Data){
AStarAlgorithm Astar = new AStarAlgorithm(Data);
ShortestPathSolution SolutionAstar = Astar.run();
if (SolutionAstar.getStatus()!=AbstractSolution.Status.INFEASIBLE){
return false;
}
return true;
}
// Vérifier si le chemin est faisable pour Dijkstra
public static boolean SolutionInfaisableDijkstra (ShortestPathData Data){
DijkstraAlgorithm Dijkstra = new DijkstraAlgorithm(Data);
ShortestPathSolution SolutionDijkstra = Dijkstra.run();
if (SolutionDijkstra.getStatus()!=AbstractSolution.Status.INFEASIBLE){
return false;
}
return true;
}
// comparer les paths auxquelles on nous a donne acces et les comparer au Dijkstra obtenu
public static boolean boncheminDijkstra (ShortestPathData Data,Path chemindepart){
// on regarde d'abord si les chemins sont valides
Path chemintrouve = Path_Dijkstra(Data);
// Cas 1 : aucun chemin n'existe
if (chemintrouve == null && chemindepart == null) {
System.out.println("Aucun chemin trouvé (null).");
return true; // les deux algos ont échoué à trouver un chemin, cohérent
}
if (chemintrouve == null || chemindepart == null) {
System.out.println("Un seul chemin est null, l'autre non.");
return false; // incohérence entre les deux
}
// Cas 2 : chemin vide (origine = destination, coût nul)
if (chemintrouve.isEmpty() && chemindepart.isEmpty()) {
System.out.println("Chemins vides mais valides (origine = destination).");
return true;
}
if (chemintrouve.isEmpty() || chemindepart.isEmpty()) {
System.out.println("Un seul chemin vide, l'autre non.");
return false;
}
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() != chemindepart.size()){
System.out.println(" chemins de taille différente");
return false;
}
// recuperation liste des arcs
List<Arc> arcstrouves = chemintrouve.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
Arc arctrouve = arcstrouves.get(i);
Arc arcrecherche = arcsrecherches.get(i);
if(!(arctrouve.getOrigin().equals(arcrecherche.getOrigin())) || !(arctrouve.getDestination().equals(arcrecherche.getDestination()) )){
System.out.println("A partir de l'arc "+i + "les chemins sont differents");
return false;
}
}
return true;
}
// Recuperation Path de Dijkstra
public static Path Path_Dijkstra(ShortestPathData Data){
DijkstraAlgorithm Dijkstra = new DijkstraAlgorithm(Data);
ShortestPathSolution SolutionDijkstra = Dijkstra.run();
return SolutionDijkstra.getPath();
}
// Recuperation Path de BellmanFord pour petites cartes uniquement
public static Path Path_BellmanFord(ShortestPathData Data){
BellmanFordAlgorithm BellmanFord = new BellmanFordAlgorithm(Data);
ShortestPathSolution SolutionBellmanFord = BellmanFord.run();
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;
}
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;
}
else{ return true;}
}
// 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"+CostDijkstra);
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" + CostDijkstra);
return true;
}
public static boolean memepathtemps (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();
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;
}
final double EPSILON = 1e-3;
if(Math.abs(tempsArcDijkstra - tempsArcBellmanFord )> EPSILON){
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;
}
public static boolean memepathdistance (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
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( 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) {
// 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);
// Recuperation du graph et path de la situation
// cas 1 NofilterbyLength : a un path
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
System.out.println("---------------------------------------------------\n ");
System.out.println("Test bonchemin\n ");
if(boncheminDijkstra(Data1, pathrecupere1)){
System.out.println("L'algorithme Dijkstra donne le bon path\n ");
}
else{System.out.println("L'algorithme Dijkstra ne donne pas le bon path\n");}
System.out.println("---------------------------------------------------\n ");
// Comparaison avec Bellman Ford
// Comparaison de cout en distance
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme distance Bellman-Ford et Dijkstra\n ");
if(memedistance(Data1)){
System.out.println("Dijkstra et Bellman Ford donnent le même coût\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même\n ");}
System.out.println("---------------------------------------------------\n ");
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme temps Bellman-Ford et Dijkstra\n ");
if(memetemps(Data1)){
System.out.println("Dijkstra et Bellman Ford donnent le même temps\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même temps\n ");}
System.out.println("---------------------------------------------------\n ");
// comparaison des paths
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme distance Bellman-Ford et Dijkstra\n ");
if(memepathdistance(Data1)){
System.out.println("Dijkstra et Bellman Ford ont le meme path\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même path\n ");}
System.out.println("---------------------------------------------------\n ");
// cas 2 Filter OnlyCarbylength : pas de path
// Recuperation du path realise par Dijkstra
ShortestPathData Data2 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getDestination(),OnlyCarByLength);
// verification du fait qu'on n'a pas de chemin
System.out.println("---------------------------------------------------\n ");
System.out.println("Test Solution Infaisable\n ");
if(SolutionInfaisableDijkstra(Data2)){
System.out.println("L'algorithme Dijkstra donne le bon path : infaisable\n ");
}
else{System.out.println("L'algorithme Dijkstra ne donne pas le bon path\n");}
System.out.println("---------------------------------------------------\n ");
// Comparaison avec Bellman Ford : Voir si meme Bellman Ford ne donne rien dans ce cas
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme distance Bellman-Ford et Dijkstra\n ");
if(SolutionInfaisableDijkstra(Data2)){
System.out.println("BellmanFord donne un path infaisable \n ");
}
else{System.out.println("BellmanFord donne un path faisable\n ");}
System.out.println("---------------------------------------------------\n ");
// cas 3 Filter OnlyCarbytime
// Recuperation du path realise par Dijkstra
ShortestPathData Data3 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getDestination(),OnlyCarByTime);
// Comparaison avec Bellman Ford
// Comparaison de cout en distance
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme distance Bellman-Ford et Dijkstra\n ");
if(memedistance(Data3)){
System.out.println("Dijkstra et Bellman Ford donnent la meme distance\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas la meme distance\n ");}
System.out.println("---------------------------------------------------\n ");
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme temps Bellman-Ford et Dijkstra\n ");
if(memetemps(Data3)){
System.out.println("Dijkstra et Bellman Ford donnent le même temps\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même temps\n ");}
System.out.println("---------------------------------------------------\n ");
// comparaison des paths
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme path Bellman-Ford et Dijkstra\n ");
if(memepathdistance(Data3)){
System.out.println("Dijkstra et Bellman Ford ont le meme path\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même path\n ");}
System.out.println("---------------------------------------------------\n ");
// cas 4 Filter OnlyPedestrianByTime
// Recuperation du path realise par Dijkstra
ShortestPathData Data4 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getDestination(),OnlyPedestrianByTime);
// Comparaison avec Bellman Ford
// Comparaison de cout en distance
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme distance Bellman-Ford et Dijkstra\n ");
if(memedistance(Data4)){
System.out.println("Dijkstra et Bellman Ford donnent la meme distance\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas la meme distance\n ");}
System.out.println("---------------------------------------------------\n ");
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme temps Bellman-Ford et Dijkstra\n ");
if(memetemps(Data4)){
System.out.println("Dijkstra et Bellman Ford donnent le même temps\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même temps\n ");}
System.out.println("---------------------------------------------------\n ");
// comparaison des paths
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme path Bellman-Ford et Dijkstra\n ");
if(memepathdistance(Data4)){
System.out.println("Dijkstra et Bellman Ford ont le meme path\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même path\n ");}
System.out.println("---------------------------------------------------\n ");
// cPath est faisable
System.out.println("---------------------------------------------------\n ");
System.out.println("Test faisabilite path Bellman-Ford et Dijkstra\n ");
if(SolutionInfaisableDijkstra(Data4)){
System.out.println("Dijkstra infaisable :erreur \n ");
}
else{System.out.println("Dijkstra faisable : bon \n ");}
System.out.println("---------------------------------------------------\n ");
// cas 5 : Origine et Destination sont les memes donc path faisable mais cout nul
// Recuperation du path realise par Dijkstra
ShortestPathData Data5 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getOrigin(),OnlyPedestrianByTime);
// verification du fait qu'on n'a pas de chemin
DijkstraAlgorithm algo = new DijkstraAlgorithm(Data5);
ShortestPathSolution solution = algo.run();
// Affichage des résultats
System.out.println("Statut : " + solution.getStatus());
System.out.println("Chemin vide ? " + solution.getPath().isEmpty());
System.out.println("Longueur totale : " + solution.getPath().getLength());
// Cas 6: deux composantes non connexes
Graph graph2 = graphlecture(mapName5);
// Recuperation du path realise par Dijkstra
ShortestPathData Data6 = new ShortestPathData(graph,graph.get(63), graph.get(1253),OnlyPedestrianByTime);
// verification du fait qu'on n'a pas de chemin
System.out.println("---------------------------------------------------\n ");
System.out.println("Test Solution Infaisable\n ");
if(SolutionInfaisableDijkstra(Data6)){
System.out.println("L'algorithme Dijkstra donne le bon path : infaisable\n ");
}
else{System.out.println("L'algorithme Dijkstra ne donne pas le bon path\n");}
System.out.println("---------------------------------------------------\n ");
// Cas 7 : trajet grande map
Graph graph3 = graphlecture(mapName3);
Path path3 = pathlecture(graph3, pathName3);
// Recuperation du path realise par Dijkstra
ShortestPathData Data7 = new ShortestPathData(graph3,path3.getOrigin(), path3.getDestination(),Nofilterbylength);
// verification du fait qu'on n'a pas de chemin
System.out.println("---------------------------------------------------\n ");
System.out.println("Test Solution Faisable\n ");
if(SolutionInfaisableDijkstra(Data7)){
System.out.println("L'algorithme Dijkstra donne le mauvais path : infaisable\n ");
}
else{System.out.println("L'algorithme Dijkstra donne le bon path\n");}
System.out.println("---------------------------------------------------\n ");
DijkstraAlgorithm algo2 = new DijkstraAlgorithm(Data7);
ShortestPathSolution solution2 = algo2.run();
System.out.println("cout chemin:" +solution2.getPath().getLength());
System.out.println("temps chemin:"+solution2.getPath().getMinimumTravelTime());
}
}

View file

@ -21,35 +21,12 @@ 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)
la nature du coût (tester en distance et en temps)
une origine et une destination (tester plusieurs valeurs) */
/* Les scénarios doivent être convaincants et représentatifs des situations
pouvant arriver (chemin inexistant, chemin de longueur nulle, trajet court,
trajet long et pénible avec des enfants, ...).*/
/* Sur ces scénarios, vérifier que le chemin construit par votre algo est valide.
Vérifiez aussi que le coût du chemin calculé par Dijkstra est bien le même que celui calculé par la classe Path. Depuis la 1A vous savez qu'on ne compare pas deux réels avec ==.
Sur les petits scénarios, vérifiez que vous obtenez les mêmes résultats qu'avec Bellman-Ford (que faut-il tester ?)
Sur les scénarios plus grands, Bellman-Ford sera inutilisable. Réfléchissez à quelques tests automatiques que vous pouvez tout de même effectuer pour vous rassurer sur le chemin trouvé.
Concevez vos tests de manière modulaire : vous les -utiliserez pour A-star. */
/* Tests modulaires cad :Reusable: You can use the same test for Dijkstra, Bellman-Ford, and later A*.
Isolated: Each test checks one specific thing (e.g., cost matches, or validity of path).
Automated: No need for manual launching or visual inspection.*/
// fonction pour recuperer les graphs
// visit these directory to see the list of available files on commetud.
Graph graph = null;
Path path = null;
// fonction pour recuperer les paths
// fonction pour recuperer les paths des scenarios
public static Path pathlecture(Graph graph,String pathName) {
try (final PathReader reader = new BinaryPathReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))))) {
@ -60,7 +37,7 @@ Automated: No need for manual launching or visual inspection.*/
return null;
}
}
// fonction pour recuperer les graphs
// fonction pour recuperer les graphs des scenarios
public static Graph graphlecture(String mapName) {
try (final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))))) {
@ -72,44 +49,25 @@ Automated: No need for manual launching or visual inspection.*/
}
}
// Scénario 1 : trajet court petite map pieton
// Path et Map utilises pour le premier scenario: pour les 4 filtres possibles
// meme Map utilise pour le cas ou l'origine et la destination sont les memes
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;
// Scénario 3: trajet court grande map
// Meme map utilise Scénario 5: Chemin inexistant cas 1: origine et destination dans deux composantes connexes différentes
// Scénario 2: trajet long grande map
final static String mapName2 = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
// Autre scenarios envisagees mais non presents dans le code car repetitf
//trajet court pour une grande map
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";
// Scénario 5: Chemin inexistant cas 1: origine et destination dans deux composantes connexes différentes
final String mapName5= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/french-polynesia.mapgr";
// Scénario 6.1: Chemin inexistant cas 2: origine et destination inaccessibles avec la voiture
final String mapName6= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
final String pathName6 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_bikini_motorcar.path";
// Scénario 6.2: Meme chemin test pour pieton
final String mapName7= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
final String pathName7 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
final static String mapName5= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/french-polynesia.mapgr";
// Scénario 8: Chemin de longueur nulle (origine = destination)
final String mapName8= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
////Node Origin = 1333; Node Destination =1103;
// Scénario 9: Test chemin court par rapport temps
final String mapName9 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haute-garonne.mapgr";
final String pathName9 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_aeroport_time.path";
// Scénario 10: Test chemin court par rapport distance
final String mapName10 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haute-garonne.mapgr";
final String pathName10 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_aeroport_length.path";
/* Tests pour l'ensembles des scénarios*/
// Vérifier la validité du chemin : utilisation de .isvalid()
// fonction qui extrait le chemin valide
// Vérifier si le chemin est faisable pour Dijkstra
public static boolean SolutionInfaisableDijkstra (ShortestPathData Data){
DijkstraAlgorithm Dijkstra = new DijkstraAlgorithm(Data);
ShortestPathSolution SolutionDijkstra = Dijkstra.run();
@ -118,7 +76,7 @@ public static boolean SolutionInfaisableDijkstra (ShortestPathData Data){
}
return true;
}
// Verifier si le chemin est faisable pour BellmanFord utilise seulement cas petite carte
public static boolean SolutionInfaisableBellmanFord (ShortestPathData Data){
BellmanFordAlgorithm BellmanFord = new BellmanFordAlgorithm(Data);
ShortestPathSolution SolutionBellmanFord= BellmanFord.run();
@ -127,6 +85,7 @@ public static boolean SolutionInfaisableBellmanFord (ShortestPathData Data){
}
return true;
}
// comparer les paths auxquelles on nous a donne acces et les comparer au Dijkstra obtenu
public static boolean boncheminDijkstra (ShortestPathData Data,Path chemindepart){
// on regarde d'abord si les chemins sont valides
@ -150,10 +109,7 @@ if (chemintrouve.isEmpty() || chemindepart.isEmpty()) {
System.out.println("Un seul chemin vide, l'autre non.");
return false;
}
if(!(chemintrouve.isEmpty()) && chemindepart.isEmpty() ){
System.out.println(" chemin vide");
return true;
}
if(!(chemintrouve.isValid()) || !(chemindepart.isValid()) ){
System.out.println(" chemin(s) non valide");
return false;
@ -179,7 +135,7 @@ if (chemintrouve.isEmpty() || chemindepart.isEmpty()) {
return true;
}
// Pour petites cartes
// Recuperation Path de Dijkstra
public static Path Path_Dijkstra(ShortestPathData Data){
@ -188,7 +144,7 @@ public static Path Path_Dijkstra(ShortestPathData Data){
return SolutionDijkstra.getPath();
}
// Recuperation Path de Dijkstra
// Recuperation Path de BellmanFord pour petites cartes uniquement
public static Path Path_BellmanFord(ShortestPathData Data){
BellmanFordAlgorithm BellmanFord = new BellmanFordAlgorithm(Data);
@ -212,8 +168,6 @@ if(!(PathDijkstra.isValid())|| !(PathBellmanFord.isValid()) ){
}
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
@ -372,9 +326,6 @@ public static boolean memepathdistance (ShortestPathData Data){
ArcInspector OnlyCarByLength = filters.get(1);
ArcInspector OnlyCarByTime= filters.get(2);
ArcInspector OnlyPedestrianByTime = filters.get(3);
// petite carte chemin existant pour tous filtres
//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
// cas 1 NofilterbyLength : a un path
Graph graph = graphlecture(mapName1);
@ -434,7 +385,7 @@ System.out.println("---------------------------------------------------\n ");
System.out.println("---------------------------------------------------\n ");
// cas 3 Filter OnlyCarbytime: pas de path
// cas 3 Filter OnlyCarbytime
// Recuperation du path realise par Dijkstra
ShortestPathData Data3 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getDestination(),OnlyCarByTime);
// Comparaison avec Bellman Ford
@ -463,12 +414,95 @@ System.out.println("---------------------------------------------------\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même path\n ");}
System.out.println("---------------------------------------------------\n ");
// cas 4 Filter OnlyPedestrianByTime
// Recuperation du path realise par Dijkstra
ShortestPathData Data4 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getDestination(),OnlyPedestrianByTime);
// Comparaison avec Bellman Ford
// Comparaison de cout en distance
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme distance Bellman-Ford et Dijkstra\n ");
if(memedistance(Data4)){
System.out.println("Dijkstra et Bellman Ford donnent la meme distance\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas la meme distance\n ");}
System.out.println("---------------------------------------------------\n ");
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme temps Bellman-Ford et Dijkstra\n ");
if(memetemps(Data4)){
System.out.println("Dijkstra et Bellman Ford donnent le même temps\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même temps\n ");}
System.out.println("---------------------------------------------------\n ");
// comparaison des paths
System.out.println("---------------------------------------------------\n ");
System.out.println("Test meme path Bellman-Ford et Dijkstra\n ");
if(memepathdistance(Data4)){
System.out.println("Dijkstra et Bellman Ford ont le meme path\n ");
}
else{System.out.println("Dijkstra et Bellman Ford ne donnent pas le même path\n ");}
System.out.println("---------------------------------------------------\n ");
// cPath est faisable
System.out.println("---------------------------------------------------\n ");
System.out.println("Test faisabilite path Bellman-Ford et Dijkstra\n ");
if(SolutionInfaisableDijkstra(Data4)){
System.out.println("Dijkstra infaisable :erreur \n ");
}
else{System.out.println("Dijkstra faisable : bon \n ");}
System.out.println("---------------------------------------------------\n ");
// cas 5 : Origine et Destination sont les memes donc path faisable mais cout nul
// Recuperation du path realise par Dijkstra
ShortestPathData Data5 = new ShortestPathData(graph,pathrecupere1.getOrigin(), pathrecupere1.getOrigin(),OnlyPedestrianByTime);
// verification du fait qu'on n'a pas de chemin
DijkstraAlgorithm algo = new DijkstraAlgorithm(Data5);
ShortestPathSolution solution = algo.run();
// Affichage des résultats
System.out.println("Statut : " + solution.getStatus());
System.out.println("Chemin vide ? " + solution.getPath().isEmpty());
System.out.println("Longueur totale : " + solution.getPath().getLength());
// Cas 6: deux composantes non connexes
Graph graph2 = graphlecture(mapName5);
// Recuperation du path realise par Dijkstra
ShortestPathData Data6 = new ShortestPathData(graph,graph.get(63), graph.get(1253),OnlyPedestrianByTime);
// verification du fait qu'on n'a pas de chemin
System.out.println("---------------------------------------------------\n ");
System.out.println("Test Solution Infaisable\n ");
if(SolutionInfaisableDijkstra(Data6)){
System.out.println("L'algorithme Dijkstra donne le bon path : infaisable\n ");
}
else{System.out.println("L'algorithme Dijkstra ne donne pas le bon path\n");}
System.out.println("---------------------------------------------------\n ");
// Cas 7 : trajet grande map
Graph graph3 = graphlecture(mapName3);
Path path3 = pathlecture(graph3, pathName3);
// Recuperation du path realise par Dijkstra
ShortestPathData Data7 = new ShortestPathData(graph3,path3.getOrigin(), path3.getDestination(),Nofilterbylength);
// verification du fait qu'on n'a pas de chemin
System.out.println("---------------------------------------------------\n ");
System.out.println("Test Solution Faisable\n ");
if(SolutionInfaisableDijkstra(Data7)){
System.out.println("L'algorithme Dijkstra donne le mauvais path : infaisable\n ");
}
else{System.out.println("L'algorithme Dijkstra donne le bon path\n");}
System.out.println("---------------------------------------------------\n ");
DijkstraAlgorithm algo2 = new DijkstraAlgorithm(Data7);
ShortestPathSolution solution2 = algo2.run();
System.out.println("cout chemin:" +solution2.getPath().getLength());
System.out.println("temps chemin:"+solution2.getPath().getMinimumTravelTime());
}
}
}