Ajout de tests Djikstra qui marchent tous popur l'instant
This commit is contained in:
parent
ced572abaa
commit
2e6b971790
1 changed files with 190 additions and 64 deletions
|
@ -6,6 +6,7 @@ import java.io.FileInputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.insa.graphs.algorithm.ArcInspector;
|
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.BellmanFordAlgorithm;
|
||||||
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
|
||||||
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
|
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.Node;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
import org.insa.graphs.model.io.BinaryGraphReader;
|
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.GraphReader;
|
||||||
|
import org.insa.graphs.model.io.PathReader;
|
||||||
public class TestDijkstra {
|
public class TestDijkstra {
|
||||||
/* Définissez plusieurs scénarios. Chaque scénario est défini par :
|
/* Définissez plusieurs scénarios. Chaque scénario est défini par :
|
||||||
une carte (tester des cartes routières et non routières)
|
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;
|
Graph graph = null;
|
||||||
Path path = null;
|
Path path = null;
|
||||||
|
|
||||||
// create a graph reader
|
// fonction pour recuperer les paths
|
||||||
/* try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
public static Path pathlecture(Graph graph,String pathName) {
|
||||||
new BufferedInputStream(new FileInputStream(mapName))))) {
|
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: read the graph
|
// Scénario 1 : trajet court petite map pieton
|
||||||
graph = reader.read();
|
final static String mapName1= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||||
reader.close();
|
final static String pathName1 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: create a path reader
|
|
||||||
// Je suis là et c'est là 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 2 : trajet long petite map
|
// Scénario 2 : trajet long petite map
|
||||||
final String mapName2= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
final String mapName2= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
|
||||||
Graph graphe2;
|
Graph graphe2;
|
||||||
|
@ -83,8 +81,8 @@ Graph graphe2;
|
||||||
|
|
||||||
|
|
||||||
// Scénario 3: trajet court grande map
|
// Scénario 3: trajet court grande map
|
||||||
final String mapName3= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
|
final static 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 pathName3 ="/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_be_173101_302442.path";
|
||||||
// Scénario 4: trajet long grande map
|
// Scénario 4: trajet long grande map
|
||||||
final String mapName4= "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/belgium.mapgr";
|
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*/
|
/* Tests pour l'ensembles des scénarios*/
|
||||||
|
|
||||||
// Vérifier la validité du chemin : utilisation de .isvalid()
|
// Vérifier la validité du chemin : utilisation de .isvalid()
|
||||||
// fonction verifiant que deux chemins sont les memes
|
// fonction qui extrait le chemin valide
|
||||||
public static boolean bonchemin (Path chemintrouve,Path cheminrecherche){
|
|
||||||
|
|
||||||
|
public static boolean boncheminDijkstra (ShortestPathData Data,Path chemindepart){
|
||||||
// on regarde d'abord si les chemins sont valides
|
// 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");
|
System.out.println(" chemin(s) non valide");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// on verifie si les chemins ont le meme nombre de noeuds
|
// 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");
|
System.out.println(" chemins de taille différente");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// recuperation liste des arcs
|
// recuperation liste des arcs
|
||||||
List<Arc> arcstrouves = chemintrouve.getArcs();
|
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
|
// On verifie si les chemins utilisent les mêmes arcs
|
||||||
for (int i=0; i<chemintrouve.getArcs().size();i++){
|
for (int i=0; i<chemintrouve.getArcs().size();i++){
|
||||||
// on recupere les arcs de chaque iteration
|
// on recupere les arcs de chaque iteration
|
||||||
|
@ -138,51 +139,176 @@ public static boolean bonchemin (Path chemintrouve,Path cheminrecherche){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Pour petites cartes
|
// Pour petites cartes
|
||||||
// Recuperer origine
|
// Recuperation Path de Dijkstra
|
||||||
//public static Node getOrigin
|
public static Path Path_Dijkstra(ShortestPathData Data){
|
||||||
// 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
|
|
||||||
DijkstraAlgorithm Dijkstra = new DijkstraAlgorithm(Data);
|
DijkstraAlgorithm Dijkstra = new DijkstraAlgorithm(Data);
|
||||||
ShortestPathSolution SolutionDijkstra = Dijkstra.run();
|
ShortestPathSolution SolutionDijkstra = Dijkstra.run();
|
||||||
Path PathDijkstra =SolutionDijkstra.getPath();
|
return SolutionDijkstra.getPath();
|
||||||
// Recuperation Path de BellmanFord
|
}
|
||||||
|
|
||||||
|
// Recuperation Path de Dijkstra
|
||||||
|
public static Path Path_BellmanFord(ShortestPathData Data){
|
||||||
|
|
||||||
BellmanFordAlgorithm BellmanFord = new BellmanFordAlgorithm(Data);
|
BellmanFordAlgorithm BellmanFord = new BellmanFordAlgorithm(Data);
|
||||||
ShortestPathSolution SolutionBellmanFord = BellmanFord.run();
|
ShortestPathSolution SolutionBellmanFord = BellmanFord.run();
|
||||||
Path PathBellmanFord =SolutionBellmanFord.getPath();
|
return SolutionBellmanFord.getPath();
|
||||||
// Verifier si un path existe avant de verifier sa validite pour eviter probs de pointeur
|
}
|
||||||
if (PathDijkstra == null || PathBellmanFord == null) {
|
// 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)");
|
System.out.println("Path(s) null(s)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Verification de la validité des Path
|
else{ return true;}
|
||||||
if(!(PathDijkstra.isValid())|| !(PathBellmanFord.isValid()) ){
|
}
|
||||||
|
|
||||||
|
// 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)");
|
System.out.println("Path(s) non valide(s)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
else{ return true;}
|
||||||
//Les deux doivent avoir trouvé un path
|
}
|
||||||
// if (SolutionDijkstra.getStatus() != AbstractSolution.Status.OPTIMAL || SolutionBellman.getStatus() != AbstractSolution.Status.OPTIMAL) {
|
// Recuperer origine d'un path
|
||||||
// return false;
|
//public static Node getOrigin
|
||||||
//}
|
// Vérifier que le coût en temps est similaire entre Dijkstra et Bellman-Ford
|
||||||
// verification de la similarite en temps avec un certain epsilon
|
public static boolean memetemps (ShortestPathData Data){
|
||||||
double CostDijkstra = PathDijkstra.getMinimumTravelTime();
|
// Recuperation Path de Dijkstra
|
||||||
double CostBellmanFord = PathBellmanFord.getMinimumTravelTime();
|
Path PathDijkstra =Path_Dijkstra(Data);
|
||||||
double epsilon = 1e-6;
|
// Recuperation Path de BellmanFord
|
||||||
if(Math.abs(CostDijkstra - CostBellmanFord)>epsilon){
|
Path PathBellmanFord =Path_BellmanFord(Data);
|
||||||
System.out.println("Dijkstra et BellmanFord on des coûts différents");
|
// 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;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
// Vérifier que le coût en distance est similaire entre Dijkstra et Bellman-Ford
|
// 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
|
// Pour grandes cartes
|
||||||
|
// Verifier si le path obtenu est celui voulu
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//graphlecture(graphe2,mapName2);
|
// recuperation des arcInspectors
|
||||||
//Node Origin = Graphe2.get(1333);
|
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)){
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue