JUnits avec Oracle

This commit is contained in:
Auriane Lartigue 2020-05-17 16:28:54 +02:00
parent b428e17ce8
commit 0484234692
3 changed files with 190 additions and 35 deletions

View file

@ -27,9 +27,10 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
vitesse = vitesse / 3.6 ; vitesse = vitesse / 3.6 ;
} }
for (Node node: graph.getNodes()) { // on crée un Label pour chaque node for (Node node: graph.getNodes()) { // on crée un Label pour chaque node
double distance = Point.distance( node.getPoint(), data.getDestination().getPoint() ); double distance = Point.distance( node.getPoint(), data.getDestination().getPoint() );
TabLabel[node.getId()]= new LabelStar(node,distance/vitesse); TabLabel[node.getId()]= new LabelStar(node,distance/vitesse);
}
} }
}
} }

View file

@ -38,7 +38,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0 TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0
Tas.insert(TabLabel[data.getOrigin().getId()]); // on insert le label du sommet dans le tas Tas.insert(TabLabel[data.getOrigin().getId()]); // on insert le label du sommet dans le tas
notifyOriginProcessed(data.getOrigin()); this.notifyOriginProcessed(data.getOrigin());
double old_cost = 0 ; // on utilisera cette variable pour vérifier si le coût est croissant double old_cost = 0 ; // on utilisera cette variable pour vérifier si le coût est croissant
@ -53,32 +53,36 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
TabLabel[X.getId()].setMarque(true); // on le marque TabLabel[X.getId()].setMarque(true); // on le marque
this.notifyNodeMarked(X); this.notifyNodeMarked(X);
if( X == data.getDestination()) {
if(old_cost > TabLabel[X.getId()].getTotalCost()) { //Vérification que le coût des labels marqués est croissant au cours des itérations. this.notifyDestinationReached(data.getDestination());
System.out.println("Le coût des Labels marqués n'est pas croissant");
} }
old_cost = TabLabel[X.getId()].getTotalCost(); else {
if(old_cost > TabLabel[X.getId()].getTotalCost()) { //Vérification que le coût des labels marqués est croissant au cours des itérations.
Tas.remove(TabLabel[X.getId()]); System.out.println("Le coût des Labels marqués n'est pas croissant");
nbMarque++; }
for(Arc a : X.getSuccessors() ){ // Le nombre de successeurs explorés à chaque itération = au nombre de successeurs d'un node old_cost = TabLabel[X.getId()].getTotalCost();
if (data.isAllowed(a)) {
Node Y = a.getDestination(); Tas.remove(TabLabel[X.getId()]);
if(!(TabLabel[Y.getId()].isMarque() ) ) { nbMarque++;
if(TabLabel[Y.getId()].getCost()==Double.POSITIVE_INFINITY ) { //Y n'est pas dans le tas for(Arc a : X.getSuccessors() ){ // Le nombre de successeurs explorés à chaque itération = au nombre de successeurs d'un node
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) ); if (data.isAllowed(a)) {
Tas.insert(TabLabel[Y.getId()]); Node Y = a.getDestination();
TabLabel[Y.getId()].setFather(a); if(!(TabLabel[Y.getId()].isMarque() ) ) {
this.notifyNodeReached(Y); if(TabLabel[Y.getId()].getCost()==Double.POSITIVE_INFINITY ) { //Y n'est pas dans le tas
} TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) );
else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ data.getCost(a)) { Tas.insert(TabLabel[Y.getId()]);
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) ); TabLabel[Y.getId()].setFather(a);
Tas.remove(TabLabel[Y.getId()]); this.notifyNodeReached(Y);
Tas.insert(TabLabel[Y.getId()]); }
TabLabel[Y.getId()].setFather(a); else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ data.getCost(a)) {
} TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) );
Tas.remove(TabLabel[Y.getId()]);
Tas.insert(TabLabel[Y.getId()]);
TabLabel[Y.getId()].setFather(a);
}
}
} }
} }
} }
} }
@ -93,11 +97,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
solution = new ShortestPathSolution(data, Status.INFEASIBLE); solution = new ShortestPathSolution(data, Status.INFEASIBLE);
} }
else { else {
float length = 0; double length = Labeldest.getTotalCost();
notifyDestinationReached(dest);
while (a != null ) { while (a != null ) {
arcs.add(a); arcs.add(a);
length += a.getLength();
a = TabLabel[a.getOrigin().getId()].getFather(); a = TabLabel[a.getOrigin().getId()].getFather();
//System.out.println(a); //System.out.println(a);
} }
@ -108,13 +110,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme // Vérifier que la longueur du chemin (avec methode de la class Path) est identique à la longueur du PCC de l'algorithme
System.out.println("TEST : Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme"); System.out.println("TEST : Vérifier que la longueur du chemin (avec methode de la class Path) est identique à celle du PCC de l'algorithme");
if( (int) path.getLength() == (int)length) { if( (int) path.getLength() == (int)length) {
System.out.println("La longueur du chemin (avec methode de la class Path) est identique au PCC de l'algorithme"); System.out.println("La longueur du chemin (avec methode de la class Path) est identique à celle du PCC de l'algorithme");
} }
else { else {
System.out.println( "Erreur : la longueur du chemin (avec methode de la class Path) n'est pas identique au PCC de l'algorithme, " + "Avec methode Path "+ path.getLength() + ", algorithme " + length) ; System.out.println( "Erreur : la longueur du chemin (avec methode de la class Path) n'est pas identique à celle du PCC de l'algorithme, " + "Avec methode Path "+ path.getLength() + ", algorithme " + length) ;
} }
//Vérifier que le chemin solution obtenu par Dijsktra est valide //Vérifier que le chemin solution obtenu par Dijsktra est valide

View file

@ -0,0 +1,152 @@
package org.insa.graphs.algorithm.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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.ShortestPathAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import org.insa.graphs.algorithm.utils.PriorityQueueTest.MutableInteger;
import org.insa.graphs.algorithm.utils.PriorityQueueTest.TestParameters;
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.GraphReader;
import org.insa.graphs.model.io.PathReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ComparerAlgoTest {
private static Graph graph1; // INSA --> Bikini
private static Graph graph2; // Deux iles de Grèce
@Parameters
public static Collection<ShortestPathData> data() {
final String mapName1 = "/Users/auriane/Desktop/Maps/haute-garonne/haute-garonne.mapgr";
final String mapName2 = "/Users/auriane/Desktop/Maps/greece/greece.mapgr";
try {
FileInputStream F1 = new FileInputStream(mapName1);
FileInputStream F2 = new FileInputStream(mapName2);
}
catch(Exception e){
System.out.println("Maps not found");
}
try {
// Create a graph reader1.
final GraphReader reader1 = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName1))));
//Read the graph1.
graph1 = reader1.read();
// Create a graph reader.
final GraphReader reader2 = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName2))));
//Read the graph.
graph2 = reader2.read();
}
catch(Exception e){
System.out.println("Carte non lu");
}
List<ArcInspector> ListeInspector = ArcInspectorFactory.getAllFilters() ;
Collection<ShortestPathData> objects = new ArrayList<>();
// ---------------------Shortest path, all roads allowed : INSA --> Bikini -------------------------------------
objects.add(new ShortestPathData(graph1, graph1.get(10991), graph1.get(10991), ListeInspector.get(0)));
objects.add(new ShortestPathData(graph1, graph1.get(10991), graph1.get(63104), ListeInspector.get(0)));
//---------------------Fastest path, all roads allowed : INSA --> Bikini ---------------------------------------
objects.add(new ShortestPathData(graph1, graph1.get(10991), graph1.get(10991), ListeInspector.get(2)));
objects.add(new ShortestPathData(graph1, graph1.get(10991), graph1.get(63104), ListeInspector.get(2)));
// ---------------------Path with no solution : 2 islands in Greece ---------------------------------------------
objects.add(new ShortestPathData(graph2, graph2.get(4368), graph1.get(4374), ListeInspector.get(0)));
objects.add(new ShortestPathData(graph2, graph2.get(4368), graph1.get(4374), ListeInspector.get(2)));
return objects;
}
@Parameter
public ShortestPathData parameters;
// -----------------------------DijkstraSingleNodeSP-------------------------------------
@Test
public void DijkstraSingleNodeSP() {
Assume.assumeTrue(parameters.getOrigin() == parameters.getDestination() && parameters.getGraph().getMapName() != graph2.getMapName());
ShortestPathAlgorithm DA = new DijkstraAlgorithm(parameters);
ShortestPathAlgorithm BF = new BellmanFordAlgorithm(parameters);
assertEquals( DA.run().getPath() , BF.run().getPath());
}
// -----------------------------DijkstraNormalPathSP-------------------------------------
@Test
public void DijkstraNormalPathSP() {
Assume.assumeFalse(parameters.getOrigin() == parameters.getDestination() || parameters.getGraph().getMapName() == graph2.getMapName());
ShortestPathAlgorithm DA = new DijkstraAlgorithm(parameters);
ShortestPathAlgorithm BF = new BellmanFordAlgorithm(parameters);
//on compare la longueur des deux paths trouvés
assertEquals( DA.run().getPath().getLength(), BF.run().getPath().getLength(), 0.01 );
//on compare le temps de trajet des deux paths trouvés
assertEquals( DA.run().getPath().getMinimumTravelTime(), DA.run().getPath().getMinimumTravelTime() , 0.01);
}
// ---------------------------------AStarSingleNodeSP-------------------------------------
@Test
public void AStarSingleNodeSP() {
Assume.assumeTrue(parameters.getOrigin() == parameters.getDestination() && parameters.getGraph().getMapName() != graph2.getMapName());
AStarAlgorithm AS = new AStarAlgorithm(parameters);
ShortestPathAlgorithm BF = new BellmanFordAlgorithm(parameters);
assertEquals( AS.run().getPath() , BF.run().getPath());
}
// ----------------------------------AStarNormalPathSP-------------------------------------
@Test
public void AStarNormalPathSP() {
Assume.assumeFalse(parameters.getOrigin() == parameters.getDestination() || parameters.getGraph().getMapName() == graph2.getMapName());
AStarAlgorithm AS = new AStarAlgorithm(parameters);
ShortestPathAlgorithm BF = new BellmanFordAlgorithm(parameters);
//on compare la longueur des deux paths trouvés
assertEquals( AS.run().getPath().getLength(), BF.run().getPath().getLength(), 0.01 );
//on compare le temps de trajet des deux paths trouvés
assertEquals( AS.run().getPath().getMinimumTravelTime(), BF.run().getPath().getMinimumTravelTime() , 0.01);
}
// ----------------------------------Dijkstra No Solution-------------------------------------
public void DijkstraNoSolution() {
Assume.assumeTrue(parameters.getGraph().getMapName() == graph2.getMapName() );
ShortestPathAlgorithm DA = new DijkstraAlgorithm(parameters);
ShortestPathAlgorithm BF = new BellmanFordAlgorithm(parameters);
assertEquals( DA.run().getStatus() , BF.run().getStatus());
}
// ----------------------------------AStar No Solution-------------------------------------
public void AStarNoSolution() {
Assume.assumeTrue(parameters.getGraph().getMapName() == graph2.getMapName() );
AStarAlgorithm AS = new AStarAlgorithm(parameters);
ShortestPathAlgorithm BF = new BellmanFordAlgorithm(parameters);
assertEquals( AS.run().getStatus() , BF.run().getStatus());
}
}