This commit is contained in:
Nabzzz 2020-06-01 18:20:49 +02:00
parent b48d1f16ec
commit 1e2c385211
11 changed files with 272 additions and 29 deletions

View file

@ -13,26 +13,37 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
@Override protected void init(Label[] tabLabel, ShortestPathData data)
{ double maxspeed=data.getMaximumSpeed();
System.out.println("Max speed avant: "+maxspeed+"\n");
if(maxspeed==-1)
{
maxspeed=(data.getGraph().getGraphInformation().getMaximumSpeed());
//System.out.println("Max speed 2: "+maxspeed+"\n");
maxspeed=maxspeed/3.6;
}
System.out.println("Max speed 3: "+maxspeed+"\n");
//System.out.println("Max speed avant: "+maxspeed+"\n");
//System.out.println("Max speed 3: "+maxspeed+"\n");
if(data.getMode()==Mode.LENGTH)
{
maxspeed=1.0;
}
System.out.println("Max speed 4: "+maxspeed+"\n");
//Initialization
for(Node n: data.getGraph().getNodes())
else
{
if(maxspeed==-1)
{
maxspeed=(data.getGraph().getGraphInformation().getMaximumSpeed());
//System.out.println("Max speed 2: "+maxspeed+"\n");
maxspeed=maxspeed/3.6;
}
}
//System.out.println("Max speed 4: "+maxspeed+"\n");
//System.out.println("Origin latitude: "+data.getOrigin().getPoint().getLatitude() +"; origin longitude: "+data.getOrigin().getPoint().getLongitude()+"\n");
System.out.println("Destination latitude: "+data.getDestination().getPoint().getLatitude() +"; destination longitude: "+data.getDestination().getPoint().getLongitude()+"\n");
//Initialization
for(Node n: data.getGraph().getNodes())
{
tabLabel[n.getId()]=new LabelStar(n,Point.distance(n.getPoint(),data.getDestination().getPoint())/maxspeed);
tabLabel[n.getId()]=new LabelStar(n,Point.distance(n.getPoint(),data.getDestination().getPoint())/maxspeed);
}
//tabLabel[data.getOrigin().getId()].setCout(Point.distance(data.getOrigin().getPoint(),data.getDestination().getPoint())/maxspeed);
}
}

View file

@ -37,12 +37,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override
protected ShortestPathSolution doRun() {
double temps_debut=System.currentTimeMillis();
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
Graph graph = data.getGraph();
final int nbNodes=graph.size();
BinaryHeap<Label> tas=new BinaryHeap<Label>();
double coutPrec=0.0;
double coutPrec=0.0; //Sert uniquement pour les tests
Label []tabLabel=new Label[nbNodes];
init(tabLabel, data);
//Initialization
@ -53,11 +54,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
tabLabel[data.getOrigin().getId()].setCout(0);
tas.insert(tabLabel[data.getOrigin().getId()]);
this.notifyOriginProcessed(data.getOrigin());
//int nbMarque=0;
int iterations=0;
int nbExplore=0;// sert uniquement pour les tests
int nbMarque=1;// sert uniquement pour les tests
int iterations=0; // sert uniquement pour les tests
while(!tabLabel[data.getDestination().getId()].isMarque())
{
iterations++;
iterations++; //sert uniquement pour les tests.
Node X;
try
{
@ -70,7 +72,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
tabLabel[X.getId()].setMarque(true);
this.notifyNodeMarked(X);
if(tabLabel[X.getId()].getCoutTotal()<coutPrec)
{
System.out.println("Problème! Les coûts ne sont pas croissants \n");
@ -78,7 +79,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
System.out.println("Coût précédent: "+ coutPrec +"\n");
}
coutPrec=tabLabel[X.getId()].getCoutTotal();
//nbMarque++;
if(tabLabel[data.getDestination().getId()].isMarque())
{
break;
}
nbMarque++;
try
{
tas.remove(tabLabel[X.getId()]);
@ -104,7 +109,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
catch(ElementNotFoundException e) {tas.insert(tabLabel[Y.getId()]);
tabLabel[Y.getId()].setPere(a);
this.notifyNodeReached(Y);}
this.notifyNodeReached(Y);
nbExplore++;}
}
@ -134,18 +140,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
System.out.println("Nombre d'arcs: "+arcs.size() + "\n");
System.out.println("Nombre d'itérations: "+iterations + "\n");
System.out.println("Nombre de noeuds marqués: "+ nbMarque + "\n");
System.out.println("Nombre de noeuds explorés: "+ nbExplore + "\n");
// Reverse the path...
Collections.reverse(arcs);
//Vérification grâce à Path.createShortestPathFromNodes
Path p= new Path(graph,arcs);
//List<Node> noeuds=new ArrayList<Node>();
float cout_total=0;
for(Arc a: arcs)
{
cout_total += a.getLength();
}
if(p.getLength()==cout_total)
if((int) p.getLength()*100== (int) tabLabel[data.getDestination().getId()].getCout()*100)
{
System.out.println("La longueur du plus court chemin est la même que celle calculée grâce à l'algo \n");
}
@ -153,7 +157,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
{
System.out.println("ERREUR: La longueur du plus court chemin n'est la même que celle calculée grâce à l'algo \n");
System.out.println("Cout pcc="+cout_total);
System.out.println("Cout pcc="+ tabLabel[data.getDestination().getId()].getCout());
System.out.println("Cout algo="+p.getLength());
}
/*noeuds.add(data.getDestination());
@ -198,9 +202,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}
return solution;
double temps_fin=System.currentTimeMillis();
double total_temps=temps_fin-temps_debut;
System.out.println("Duree d'execution:"+total_temps);
return solution;
}
}

View file

@ -0,0 +1,224 @@
import org.insa.graphs.model.*;
import org.insa.graphs.algorithm.*;
import org.insa.graphs.algorithm.shortestpath.*;
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.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.RoadInformation;
import org.insa.graphs.model.RoadInformation.RoadType;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
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 OptiTest {
private static Graph graph1;
private static Graph graph2;
private static Graph graph3;
@Parameters
public static Collection<ShortestPathData> data() {
//**************************************************************
//**************************************************************
//CREATION DU GRAPHE DE LA CARTE HAUTE-GARONNE
final String mapName1 = "C:\\Users\\nmouk\\Documents\\GIT\\MAPS\\haute-garonne\\europe\\france\\haute-garonne.mapgr" ;
try {
final FileInputStream F1= new FileInputStream(mapName1);
}
catch(Exception e) {System.out.println("Fichier non existant!");}
// Create a graph reader.
try {
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName1))));
// Small graph use for tests
graph1 = reader.read();
}
catch(Exception e) {System.out.println("Fichier non lu!");}
//******************************************************************
//******************************************************************
//CREATION DU GRAPHE DE LA CARTE CARRE
final String mapName2 = "C:\\Users\\nmouk\\Documents\\GIT\\MAPS\\carre\\extras\\carre.mapgr" ;
try {
final FileInputStream F2= new FileInputStream(mapName2);
}
catch(Exception e) {System.out.println("Fichier non existant!");}
// Create a graph reader.
try {
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName2))));
// Small graph use for tests
graph2 = reader.read();
}
catch(Exception e) {System.out.println("Fichier non lu!");}
//******************************************************************
//******************************************************************
//CREATION DU GRAPHE DE LA CARTE POLYNESIE FRANCAISE
final String mapName3 = "C:\\Users\\nmouk\\Documents\\GIT\\MAPS\\french-polynesia\\oceania\\french-polynesia.mapgr" ;
try {
final FileInputStream F3= new FileInputStream(mapName2);
}
catch(Exception e) {System.out.println("Fichier non existant!");}
// Create a graph reader.
try {
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName3))));
// Small graph use for tests
graph3 = reader.read();
}
catch(Exception e) {System.out.println("Fichier non lu!");}
Collection<ShortestPathData> objects = new ArrayList<>();
//***************CARTE HAUTE-GARONNE****************************
//**************************************************************
//CHEMIN NUL AVEC 2 MODES DE DEPLACEMENTS
objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(0)));
objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(2)));
//CHEMIN AVEC 3 MODES DE DEPLACEMENTS
objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(0)));
objects.add(new ShortestPathData(graph1, graph1.get(10991),graph1.get(10991),ArcInspectorFactory.getAllFilters().get(2)));
//***************CARTE CARRE************************************
//**************************************************************
//CHEMIN NUL AVEC 2 MODES DE DEPLACEMENTS
objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(19),ArcInspectorFactory.getAllFilters().get(0)));
objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(19),ArcInspectorFactory.getAllFilters().get(2)));
//CHEMIN AVEC 2 MODES DE DEPLACEMENTS
objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(5),ArcInspectorFactory.getAllFilters().get(0)));
objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(5),ArcInspectorFactory.getAllFilters().get(2)));
objects.add(new ShortestPathData(graph2, graph2.get(19),graph2.get(5),ArcInspectorFactory.getAllFilters().get(1)));
//*************CARTE POLYNESIE FRANCAISE**********************
//************************************************************
//CHEMIN IMPOSSIBLE AVEC 2 MODES DE DEPLACEMENTS
objects.add(new ShortestPathData(graph3, graph3.get(8302),graph3.get(4904),ArcInspectorFactory.getAllFilters().get(0)));
objects.add(new ShortestPathData(graph3, graph3.get(8302),graph3.get(4904),ArcInspectorFactory.getAllFilters().get(2)));
return objects;
}
@Parameter
public ShortestPathData parameter;
@Test
public void testCheminNul()
{ //Shortest path, all roads allowed
Assume.assumeTrue(parameter.getOrigin().getId()==parameter.getDestination().getId() && parameter.getGraph().getMapId() != graph3.getMapId());
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(parameter);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(parameter);
DijkstraAlgorithm Astar= new AStarAlgorithm(parameter);
assertEquals(Dijkstra.run().isFeasible(),BF.run().isFeasible());
assertEquals(Astar.run().isFeasible(),BF.run().isFeasible());
}
@Test
public void testCheminCourt()
{
Assume.assumeTrue(parameter.getOrigin().getId()!=parameter.getDestination().getId() && parameter.getGraph().getMapId() != graph3.getMapId());
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(parameter);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(parameter);
DijkstraAlgorithm Astar= new AStarAlgorithm(parameter);
assertEquals(Dijkstra.run().getPath().getLength(), BF.run().getPath().getLength(),0.01);
assertEquals(Astar.run().getPath().getLength(),BF.run().getPath().getLength(),0.01);
assertEquals(Dijkstra.run().getPath().getMinimumTravelTime(), BF.run().getPath().getMinimumTravelTime(),0.01);
assertEquals(Astar.run().getPath().getMinimumTravelTime(),BF.run().getPath().getMinimumTravelTime(),0.01);
}
@Test
public void testCheminImpossible()
{ //Shortest path, all roads allowed
Assume.assumeTrue(parameter.getGraph().getMapId() == graph3.getMapId());
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(parameter);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(parameter);
DijkstraAlgorithm Astar= new AStarAlgorithm(parameter);
assertEquals(Dijkstra.run().isFeasible(),BF.run().isFeasible());
assertEquals(Astar.run().isFeasible(),BF.run().isFeasible());
}
/*
@Test
public void testCheminLong()
{
ShortestPathData data1=new ShortestPathData(graph, longPath.getOrigin(),longPath.getDestination(),ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
assertEquals(Dijkstra.run().toString(),BF.run().toString());
assertEquals(Astar.run().toString(),BF.run().toString());
}
@Test
public void testgrandeBoucle()
{
ShortestPathData data1=new ShortestPathData(graph, loopPath.getOrigin(),loopPath.getDestination(),ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
assertEquals(Dijkstra.run().toString(),BF.run().toString());
assertEquals(Astar.run().toString(),BF.run().toString());
}
@Test
public void testBoucle()
{
ShortestPathData data1=new ShortestPathData(graph, longLoopPath.getOrigin(),longLoopPath.getDestination(),ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
assertEquals(Dijkstra.run().toString(),BF.run().toString());
assertEquals(Astar.run().toString(),BF.run().toString());
}
@Test
public void Testinvalide()
{
ShortestPathData data1=new ShortestPathData(graph, nodes[0],nodes[5],ArcInspectorFactory.getAllFilters().get(0));
ShortestPathAlgorithm Dijkstra= new DijkstraAlgorithm(data1);
ShortestPathAlgorithm BF= new BellmanFordAlgorithm(data1);
DijkstraAlgorithm Astar= new AStarAlgorithm(data1);
assertEquals(Dijkstra.run().toString(),BF.run().toString());
assertEquals(Astar.run().toString(),BF.run().toString());
}
*/
}

View file

@ -19,6 +19,7 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public abstract class PriorityQueueTest {

View file

@ -86,6 +86,8 @@ public class PathTest {
public void testImmutability() {
emptyPath.getArcs().add(a2b);
}
@Test
public void testIsEmpty() {