123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package org.insa.graphs.algorithm.shortestpath;
-
- import static org.junit.Assert.*;
-
- import org.insa.graphs.*;//voir si on ne peut pas importer launch autrement
-
- import java.io.BufferedInputStream;
- import java.io.DataInputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.EnumMap;
-
- import org.insa.graphs.algorithm.ArcInspector;
- import org.insa.graphs.algorithm.ArcInspectorFactory;
- import org.insa.graphs.model.*;
- import org.insa.graphs.model.AccessRestrictions.AccessMode;
- import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
- import org.insa.graphs.model.RoadInformation.RoadType;
- 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;
- import org.junit.BeforeClass;
- import org.junit.Test;
-
- public class DijkstraAlgorithm_et_AStarTest{
-
- //copié sur PathTest.java
- // 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 ab, ac, ad, bf, bd, cd, cg, df, de, de2, dh, ef, eg, eh, fg, gh;
-
- private static ShortestPathData dataal, datacl, dataat, datact, datapt, onenodata, emptydata, invalidata;
-
- // permet de savoir quels arcs autoriser(cf l.187 excel ggdoc)
- private static ArcInspector alllen = ArcInspectorFactory.getAllFilters().get(0);
- private static ArcInspector carlen = ArcInspectorFactory.getAllFilters().get(1);
- private static ArcInspector alltime = ArcInspectorFactory.getAllFilters().get(2);
- private static ArcInspector cartime = ArcInspectorFactory.getAllFilters().get(3);
- private static ArcInspector pietime = ArcInspectorFactory.getAllFilters().get(4);
-
- private static ShortestPathSolution dijkal, dijkcl, dijkat, dijkct, dijkpt, onenodijk, emptydijk, invalidijk;
- private static ShortestPathSolution bfaal, bfacl, bfaat, bfact, bfapt; //les BF, Dijkstra et A* sur trois lignes différentes par souci de lisibilité
- private static ShortestPathSolution asal, ascl, asat, asct, aspt, asonenod, asempty, asinvalid;
-
-
- @BeforeClass
- public static void initAll() throws IOException {
-
- //TODO: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHHH
- //rabbithole, mission annulée
- //sinon, utilisation de toAccessInformation(), à condition de savoir quel bit correspond à quoi...
- //gestion des restrictions pour pouvoir créer les Bellman-Ford
- AccessRestrictions voiture, pedestre;
- /*AccessRestrictions.AccessRestriction authorise = AccessRestrictions.AccessRestriction.ALLOWED;
- EnumMap<AccessMode, AccessRestrictions.AccessRestriction> pmap, vmap;
- pmap = null;//dkgfvbwslidfue djeflmmqff^dfomqf^sf652s894fd5swrtwedf+e
- vmap = null;//cette initalisation ne marche pas, COMMENT faire? ->toAccessInformation de voiture et pedestre sans passer par des EnumMap
- pmap.put(AccessMode.FOOT, authorise);
- //vmap.put(AccessMode.MOTORCAR, authorise);*/
- //bloc actuellement inutile
-
- voiture = new AccessRestrictions();//vmap en paramètre si possible
- pedestre = new AccessRestrictions();//pmap en paramètre si possible
-
- RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, voiture, false, 36, ""),
- speed20 = new RoadInformation(RoadType.MOTORWAY, voiture, false, 72, ""),
- pietonable = new RoadInformation(RoadType.PEDESTRIAN, pedestre, false, 5, "");
- //cyclable = new RoadInformation(RoadType.CYCLEWAY, null, false, 20, ""),
- //toutes les routes ici sont à double sens
- //attention, les piétons ont une maxspeed définie à 5
-
- // Create nodes
- nodes = new Node[9];
- for (int i = 0; i < nodes.length; ++i) {
- nodes[i] = new Node(i, null);
- }
-
- // définition des arcs (graphe custom)
- ab = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
- ac = Node.linkNodes(nodes[0], nodes[2], 50, speed20, null);
- ad = Node.linkNodes(nodes[0], nodes[3], 15, speed10, null);
- bd = Node.linkNodes(nodes[1], nodes[3], 15, speed20, null);
- bf = Node.linkNodes(nodes[1], nodes[5], 20, speed10, null);
- cd = Node.linkNodes(nodes[2], nodes[3], 10, speed20, null);
- cg = Node.linkNodes(nodes[2], nodes[6], 15, speed10, null);
- de = Node.linkNodes(nodes[3], nodes[4], 15, speed20, null);
- de2= Node.linkNodes(nodes[3], nodes[4], 25, pietonable, null);
- df = Node.linkNodes(nodes[3], nodes[5], 8, pietonable, null);
- dh = Node.linkNodes(nodes[3], nodes[7], 12, pietonable, null);
- ef = Node.linkNodes(nodes[4], nodes[5], 10, speed10, null);
- eg = Node.linkNodes(nodes[4], nodes[6], 10, speed20, null);
- eh = Node.linkNodes(nodes[4], nodes[7], 5, pietonable, null);
- fg = Node.linkNodes(nodes[5], nodes[6], 10, pietonable, null);
- gh = Node.linkNodes(nodes[6], nodes[7], 12, pietonable, null);
-
-
- graph = new Graph("ID", "", Arrays.asList(nodes), null);
-
- //initialisation des datas
- dataal= new ShortestPathData(graph, nodes[0], nodes[4], alllen);//a->e
- datacl= new ShortestPathData(graph, nodes[1], nodes[6], carlen);//b->g
- dataat= new ShortestPathData(graph, nodes[7], nodes[1], alltime);//h->b
- datact= new ShortestPathData(graph, nodes[2], nodes[1], cartime);//c->b
- datapt= new ShortestPathData(graph, nodes[5], nodes[4], pietime);//f->e
- onenodata=new ShortestPathData(graph, nodes[2], nodes[2], pietime);
- emptydata=new ShortestPathData(graph, null, null, pietime);
- invalidata=new ShortestPathData(graph, nodes[0], nodes[8], carlen);//h aurait dû être inaccessible aux voitures
- //pour compenser, ajout du node i non relié
-
- //initialisation des Dijkstras
- dijkal = (new DijkstraAlgorithm(dataal)).run();
- dijkcl = (new DijkstraAlgorithm(datacl)).run();
- dijkat = (new DijkstraAlgorithm(dataat)).run();
- dijkct = (new DijkstraAlgorithm(datact)).run();
- dijkpt = (new DijkstraAlgorithm(datapt)).run();
- onenodijk = (new DijkstraAlgorithm(onenodata)).run();
- emptydijk = (new DijkstraAlgorithm(emptydata)).run();
- invalidijk = (new DijkstraAlgorithm(invalidata)).run();
-
- //initialisation des A*
- asal = (new DijkstraAlgorithm(dataal)).run();
- ascl = (new DijkstraAlgorithm(datacl)).run();
- asat = (new DijkstraAlgorithm(dataat)).run();
- asct = (new DijkstraAlgorithm(datact)).run();
- aspt = (new DijkstraAlgorithm(datapt)).run();
- asonenod = (new DijkstraAlgorithm(onenodata)).run();
- asempty = (new DijkstraAlgorithm(emptydata)).run();
- asinvalid = (new DijkstraAlgorithm(invalidata)).run();
-
- //initialisation des Bellman-Ford pour comparaison
- bfaal = (new BellmanFordAlgorithm(dataal)).run();
- bfacl = (new BellmanFordAlgorithm(datacl)).run();
- bfaat = (new BellmanFordAlgorithm(dataat)).run();
- bfact = (new BellmanFordAlgorithm(datact)).run();
- bfapt = (new BellmanFordAlgorithm(datapt)).run();
-
- }
-
-
-
-
- /* ( Test faits directement via la console et DijkstraAlgorithm.java, actuellement commentés:
- - coûts croissants
- - nbr successeurs cohérents
- - tas valide. ) */
-
-
-
- @Test
- public void cheminValideD() {
- assertTrue(dijkal.getPath().isValid());
- assertTrue(dijkcl.getPath().isValid());
- assertTrue(dijkat.getPath().isValid());
- assertTrue(dijkct.getPath().isValid());
- assertTrue(dijkpt.getPath().isValid());
- assertTrue(onenodijk.getPath().isValid());
- assertTrue(emptydijk.getPath().isValid());
- assertTrue(invalidijk.getPath().isValid());
- }//note: invalidata ne peut pas trouver de chemin et contient ainsi un path null, donc valide
-
- @Test
- public void cheminValideA() {
- assertTrue(asal.getPath().isValid());
- assertTrue(ascl.getPath().isValid());
- assertTrue(asat.getPath().isValid());
- assertTrue(asct.getPath().isValid());
- assertTrue(aspt.getPath().isValid());
- assertTrue(asonenod.getPath().isValid());
- assertTrue(asempty.getPath().isValid());
- assertTrue(asinvalid.getPath().isValid());
- }
-
- @Test
- public void faisableD() {
- assertTrue(dijkal.isFeasible());
- assertTrue(dijkcl.isFeasible());
- assertTrue(dijkat.isFeasible());
- assertTrue(dijkct.isFeasible());
- assertTrue(dijkpt.isFeasible());
- assertTrue(onenodijk.isFeasible());
- assertFalse(emptydijk.isFeasible());
- assertFalse(invalidijk.isFeasible());
- }
-
- @Test
- public void faisableA() {
- assertTrue(asal.isFeasible());
- assertTrue(ascl.isFeasible());
- assertTrue(asat.isFeasible());
- assertTrue(asct.isFeasible());
- assertTrue(aspt.isFeasible());
- assertTrue(asonenod.isFeasible());
- assertFalse(asempty.isFeasible());
- assertFalse(asinvalid.isFeasible());
- }
-
- //résultat identique à Bellman-Ford (sur les petits scénarios)
- @Test
- public void DsameasBF() {
- assertTrue(Float.compare(dijkal.getPath().getLength(),bfaal.getPath().getLength())==0);
- assertTrue(Float.compare(dijkcl.getPath().getLength(),bfacl.getPath().getLength())==0);
- assertTrue(Double.compare(dijkat.getPath().getMinimumTravelTime(),bfaat.getPath().getMinimumTravelTime())==0);
- assertTrue(Double.compare(dijkct.getPath().getMinimumTravelTime(),bfact.getPath().getMinimumTravelTime())==0);
- assertTrue(Double.compare(dijkpt.getPath().getMinimumTravelTime(),bfapt.getPath().getMinimumTravelTime())==0);
- }
-
- @Test
- public void AsameasBF() {
- assertTrue(Float.compare(asal.getPath().getLength(),bfaal.getPath().getLength())==0);
- assertTrue(Float.compare(ascl.getPath().getLength(),bfacl.getPath().getLength())==0);
- assertTrue(Double.compare(asat.getPath().getMinimumTravelTime(),bfaat.getPath().getMinimumTravelTime())==0);
- assertTrue(Double.compare(asct.getPath().getMinimumTravelTime(),bfact.getPath().getMinimumTravelTime())==0);
- assertTrue(Double.compare(aspt.getPath().getMinimumTravelTime(),bfapt.getPath().getMinimumTravelTime())==0);
- }
-
- //grand scénario avec oracle:
- @Test
- public void testMap() throws FileNotFoundException, IOException{//A FAIRE
-
- String mapaddr = "/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/haute-garonne.mapgr";
- String pathaddr ="/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31_insa_aeroport_length.path";
-
- GraphReader graphread = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapaddr))));
- PathReader pathread = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathaddr))));
-
- Graph graphmap = graphread.read();
- Path pathmap = pathread.readPath(graphmap);
-
- //après test, il faut prendre carlen et non alllen en ArcInspector (sinon A* et Dijkstra sont plus courts)
- DijkstraAlgorithm dijkmap = new DijkstraAlgorithm(new ShortestPathData(graphmap, pathmap.getOrigin(), pathmap.getDestination(), carlen));
- AStarAlgorithm asmap = new AStarAlgorithm(new ShortestPathData(graphmap, pathmap.getOrigin(), pathmap.getDestination(), carlen));
- assertTrue(dijkmap.run().getPath().getLength()==pathmap.getLength());
- assertTrue(asmap.run().getPath().getLength()==pathmap.getLength());
- //comparaison de la longueur
- }
-
- //long car la map bretagne (sans oracle possible) est assez importante
- @Test
- public void testMapNoOracle() throws FileNotFoundException, IOException{//A FAIRE
-
- String mapaddr = "/home/favary/Bureau/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/bretagne.mapgr";
-
- GraphReader graphread = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapaddr))));
-
- Graph graphmap = graphread.read();
- Node Origine = graphmap.getNodes().get(100000);//Brest
- Node Destination = graphmap.getNodes().get(6000);//Rennes
-
- //sans possibilité d'utiliser un pathmap, on fait une estimation arbitrairement basée sur la distance réelle
- float estimation=1.2f*(float) (Origine.getPoint().distanceTo(Destination.getPoint()));
- //1.2 très arbitraire, choisi car entre 1 (absurde) et sqrt(2)
- //ne peut pas être adapté à tous les trajets, mais ici le graphe est assez couvrant et le trajet assez long
-
- DijkstraAlgorithm dijkmap = new DijkstraAlgorithm(new ShortestPathData(graphmap, Origine, Destination, carlen));
- AStarAlgorithm asmap = new AStarAlgorithm(new ShortestPathData(graphmap, Origine, Destination, carlen));
- assertTrue(dijkmap.run().getPath().getLength()<estimation);
- assertTrue(asmap.run().getPath().getLength()<estimation);//<= car pas optimal sans oracle
- //comparaison de la longueur
- }
- }
|