Test junit prblm dijkstra

This commit is contained in:
El Haji Fofana 2023-05-19 18:11:20 +02:00
parent 9b1b344d29
commit bc6e91b21d
12 changed files with 213 additions and 130 deletions

35
.vscode/launch.json vendored
View file

@ -1,35 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "BinaryHeapFormatter",
"request": "launch",
"mainClass": "org.insa.graphs.algorithm.utils.BinaryHeapFormatter",
"projectName": "be-graphes-algos"
},
{
"type": "java",
"name": "MainWindow",
"request": "launch",
"mainClass": "org.insa.graphs.gui.MainWindow",
"projectName": "be-graphes-gui"
},
{
"type": "java",
"name": "Launch",
"request": "launch",
"mainClass": "org.insa.graphs.gui.simple.Launch",
"projectName": "be-graphes-gui"
}
]
}

View file

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View file

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false

View file

@ -4,5 +4,6 @@ org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.compiler.source=1.8

View file

@ -1,8 +1,6 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import java.util.ArrayList;
import org.insa.graphs.model.Point; import org.insa.graphs.model.Point;
public class AStarAlgorithm extends DijkstraAlgorithm { public class AStarAlgorithm extends DijkstraAlgorithm {
@ -17,24 +15,5 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
return new LabelStar(x,cout,parent,Point.distance(data.getGraph().get(x.getId()).getPoint(), data.getDestination().getPoint())); return new LabelStar(x,cout,parent,Point.distance(data.getGraph().get(x.getId()).getPoint(), data.getDestination().getPoint()));
} }
@Override
public void ShortestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes , ShortestPathData data)
{
Path p = Path.createShortestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("shortest path : " + p.getLength());
if (p.getLength()-solution.getPath().getLength() < 1.00){
System.out.println("le chemin Astar bien est le shortest");
}
}
@Override
public void fastestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes,ShortestPathData data){
Path p= Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("fastest path : " + p.getMinimumTravelTime());
if (p.getMinimumTravelTime()-solution.getPath().getMinimumTravelTime() < 1.00){
System.out.println("le chemin Astar est bien le fastest");
}
}
} }

View file

@ -8,7 +8,6 @@ import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
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.Point;
public class DijkstraAlgorithm extends ShortestPathAlgorithm { public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -30,7 +29,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
BinaryHeap<Label> Tas = new BinaryHeap<Label>(); BinaryHeap<Label> Tas = new BinaryHeap<Label>();
ArrayList<Arc> Arcs = new ArrayList<Arc>(); ArrayList<Arc> Arcs = new ArrayList<Arc>();
/* Initialise nos label */ /* Initialise nos label */
for (Node x: data.getGraph().getNodes()) for (Node x: data.getGraph().getNodes())
{ {
@ -90,58 +88,28 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
//On récupère le chemin (liste d'arcs) de la destination vers la source //On récupère le chemin (liste d'arcs) de la destination vers la source
Label dest =List_Label.get(data.getDestination().getId()); Label dest =List_Label.get(data.getDestination().getId());
while (dest.getParent() != null){
Arcs.add(dest.getParent()); if (dest.getParent() == null || data.getOrigin()==data.getDestination()) {
dest = List_Label.get(dest.getParent().getOrigin().getId()); return solution = new ShortestPathSolution(data, Status.INFEASIBLE);
} }
else {
// The destination has been found, notify the observers.
notifyDestinationReached(data.getDestination());
while (dest.getParent() != null){
Arcs.add(dest.getParent());
dest = List_Label.get(dest.getParent().getOrigin().getId());
}
}
//On inverse la liste d'arcs //On inverse la liste d'arcs
Collections.reverse(Arcs); Collections.reverse(Arcs);
//On construit la solution //On construit la solution
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs)); solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
ArrayList<Node> solutionNodes = new ArrayList<Node>();
//On récupère la liste de nodes du Path solution
for (Arc a : Arcs){
solutionNodes.add(a.getOrigin());
}
solutionNodes.add(data.getDestination());
//Vérifications de la validité de la solution
ShortestVerif(solution, solutionNodes, data);
fastestVerif(solution, solutionNodes, data);
volDoiseauVerif(data, solution);
return solution; return solution;
} }
//Permet de vérifier si le chemin obtenu par la solution est bien le plus court
public void ShortestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes , ShortestPathData data)
{
Path p = Path.createShortestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("shortest path : " + p.getLength());
if (p.getLength()-solution.getPath().getLength() < 1.00){
System.out.println("le chemin Dijkstra est bien le shortest");
}
}
// Permet de vérifier si le temps de parcours de la solution est bien le plus rapide
public void fastestVerif(ShortestPathSolution solution,ArrayList<Node> solutionNodes,ShortestPathData data){
Path p= Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("fastest path : " + p.getMinimumTravelTime());
if (p.getMinimumTravelTime()-solution.getPath().getMinimumTravelTime() < 1.00){
System.out.println("le chemin Dijkstra est bien le fastest");
}
}
// Permet de vérifier si la distance de la solution est plus long que la distance à vol d'oiseau
public void volDoiseauVerif(ShortestPathData data, ShortestPathSolution solution){
double distanceV = Point.distance(data.getGraph().get(data.getOrigin().getId()).getPoint(), data.getDestination().getPoint());
if (distanceV > solution.getPath().getLength()){
System.out.println("La solution n'est pas réalisable");
} else {
System.out.println("La solution est réalisable");
}
}
} }

View file

@ -0,0 +1,14 @@
package org.insa.graphs.algorithm.utils;
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
public class AStarAlgorithmTest extends DijkstraAlgorithmTest{
@Override
protected ShortestPathAlgorithm doAlgo(ShortestPathData data)
{
return new AStarAlgorithm(data);
}
}

View file

@ -0,0 +1,179 @@
package org.insa.graphs.algorithm.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import org.insa.graphs.model.*;
import org.insa.graphs.model.RoadInformation.RoadType;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.shortestpath.*;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
import org.junit.BeforeClass;
import org.junit.Test;
public abstract class DijkstraAlgorithmTest {
// Small graph use for tests
private static Graph graph;
// List of nodes
private static Node[] nodes;
// List of arcs in the graph, a2b is the arc from node A (0) to B (1).
@SuppressWarnings("unused")
private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
@BeforeClass
public static void initAll() throws IOException {
final String map = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre-dense.mapgr";
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(map))));
graph = reader.read();
// 10 and 20 meters per seconds paramètres des chemins emprunter
RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, null, true, 36, ""),
speed20 = new RoadInformation(RoadType.MOTORWAY, null, true, 72, "");
// Create nodes
nodes = new Node[6];
// for (int i = 0; i < nodes.length; ++i) {
// nodes[i] = new Node(i, null);
// }
nodes[0] = new Node(0, new Point(0, 20));
nodes[1] = new Node(0, new Point(20, 0));
nodes[2] = new Node(0, new Point(15, 43));
nodes[3] = new Node(0, new Point(12, 16));
nodes[4] = new Node(0, new Point(17, 89));
nodes[5] = new Node(0, new Point(43, 45));
// Add arcs...
a2b = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
a2c = Node.linkNodes(nodes[0], nodes[2], 15, speed10, null);
a2e = Node.linkNodes(nodes[0], nodes[4], 15, speed20, null);
b2c = Node.linkNodes(nodes[1], nodes[2], 10, speed10, null);
c2d_1 = Node.linkNodes(nodes[2], nodes[3], 20, speed10, null);
c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null);
c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null);
d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null);
d2e = Node.linkNodes(nodes[3], nodes[4], 22.8f, speed20, null);
e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null);
graph = new Graph("ID", "", Arrays.asList(nodes), new GraphStatistics (null,9,1,72,1));
}
protected ShortestPathAlgorithm doAlgo(ShortestPathData data) {
return new DijkstraAlgorithm(data);
}
@Test
public void Chemin_Valide() throws Exception{
final String map_c = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre-dense.mapgr";
final GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(map_c))));
Graph graph_c = reader.read();
ShortestPathData data = new ShortestPathData(graph_c,graph_c.get(120084),graph_c.get(191086),ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra = doAlgo(data);
ShortestPathSolution solution = Dijkstra.run();
assertTrue(solution.getPath().isValid());
}
@Test
public void Chemin_Inexistant(){
ShortestPathData data = new ShortestPathData(graph,nodes[0],nodes[5],ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Bellman_ford= new BellmanFordAlgorithm(data);
ShortestPathAlgorithm Dijkstra = doAlgo(data);
ShortestPathSolution solution = Dijkstra.run();
ShortestPathSolution soluce_Bellma= Bellman_ford.run();
assertEquals(soluce_Bellma.getStatus(),solution.getStatus());
}
@Test
public void Comparaison_Bell() throws IOException{
final String map_c = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre.mapgr";
final GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(map_c))));
Graph graph_c = reader.read();
ShortestPathData data = new ShortestPathData(graph_c,graph_c.get(1),graph_c.get(23),ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra = doAlgo(data);
ShortestPathAlgorithm Bellman_ford= new BellmanFordAlgorithm(data);
ShortestPathSolution soluce_Dijkstra= Dijkstra.run();
ShortestPathSolution soluce_Bellma= Bellman_ford.run();
//Verifie la difference entre Bellman et djkstra avec une difference max de 1e-6
assertEquals(soluce_Dijkstra.getPath().getLength(),soluce_Bellma.getPath().getLength(),1e-6);
}
@Test
public void Chemin_Null(){
ShortestPathData data = new ShortestPathData(graph,nodes[0],nodes[0],ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra = new DijkstraAlgorithm(data);
ShortestPathAlgorithm Bellman_ford= new BellmanFordAlgorithm(data);
ShortestPathSolution solution = Dijkstra.run();
ShortestPathSolution soluce_Bellma= Bellman_ford.run();
assertEquals(solution.getStatus(),soluce_Bellma.getStatus());
}
@Test
public void Carte_Length() throws IOException{
//Nous allons cherche lee chemin et la carte a analyser
final String map_c = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre-dense.mapgr";
final GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(map_c))));
Graph graph_c = reader.read();
ShortestPathData data = new ShortestPathData(graph_c,graph_c.get(120084),graph_c.get(191086),ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra = doAlgo(data);
ShortestPathSolution solution = Dijkstra.run();
ArrayList<Node> solutionNodes = new ArrayList<Node>();
//On récupère la liste de nodes du Path solution
for (Arc a : solution.getPath().getArcs()){
solutionNodes.add(a.getOrigin());
}
solutionNodes.add(data.getDestination());
Path solution_final = Path.createShortestPathFromNodes(graph_c, solutionNodes);
assertEquals(solution.getPath().getLength(), solution_final.getLength(),1e-6);
}
@Test
public void Carte_Time() throws Exception{
//Nous allons cherche lee chemin et la carte a analyser
final String map_c = "/mnt/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/carre-dense.mapgr";
final GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(map_c))));
Graph graph_c = reader.read();
ShortestPathData data = new ShortestPathData(graph_c,graph_c.get(120084),graph_c.get(191086),ArcInspectorFactory.getAllFilters().get(2));
ShortestPathAlgorithm Dijkstra = doAlgo(data);
ShortestPathSolution solution = Dijkstra.run();
ArrayList<Node> solutionNodes = new ArrayList<Node>();
//On récupère la liste de nodes du Path solution
for (Arc a : solution.getPath().getArcs()){
solutionNodes.add(a.getOrigin());
}
solutionNodes.add(data.getDestination());
Path solution_final = Path.createFastestPathFromNodes(graph_c, solutionNodes);
assertEquals(solution.getPath().getMinimumTravelTime(), solution_final.getMinimumTravelTime(),1e-6);
}
}

View file

@ -76,6 +76,8 @@ public class Launch {
// TODO: Draw the path. // TODO: Draw the path.
drawing.drawPath(path,Color.green); drawing.drawPath(path,Color.green);
} }
} }