Browse Source

Various improvements

Yohan Simard 3 years ago
parent
commit
25e08fc7f0

+ 2
- 1
.gitignore View File

@@ -17,4 +17,5 @@ doc
17 17
 *.mapfg
18 18
 *.mapgr
19 19
 *.path
20
-*.tgz
20
+*.tgz
21
+/rapport/

+ 9
- 8
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java View File

@@ -57,9 +57,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
57 57
         Node destination = data.getDestination();
58 58
         Node currentNode = null;
59 59
 
60
+/*
60 61
         // Debug / Tests
61 62
         double previousCost = 0;
62 63
         int iterationCounter = 0;
64
+*/
63 65
 
64 66
         while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
65 67
             // Get the node with the minimum cost
@@ -69,11 +71,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
69 71
 
70 72
 /*
71 73
             // Debug / Tests
72
-            assert(((BinaryHeap<Label>) heap).isValid());
73
-            assert(currentLabel.getTotalCost() >= previousCost);
74
+            assert (((BinaryHeap<Label>) heap).isValid());
75
+            assert (currentLabel.getTotalCost() >= previousCost);
74 76
             previousCost = currentLabel.getCost();
75
-*/
76 77
             iterationCounter++;
78
+*/
77 79
 
78 80
             for (Arc arc : currentNode.getSuccessors()) {
79 81
                 Node dest = arc.getDestination();
@@ -85,14 +87,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
85 87
                     double newCost = currentLabel.getCost() + arcCost;
86 88
                     double oldCost = destLabel.getCost();
87 89
                     if (newCost < oldCost) {
88
-                        try {
90
+                        if (Double.isFinite(oldCost)) {     // if already visited, remove from the heap before updating
89 91
                             heap.remove(destLabel);
90
-                        } catch (ElementNotFoundException ignored) {
92
+                        } else {                            // else, notify observers
93
+                            notifyNodeReached(dest);
91 94
                         }
92 95
                         destLabel.updateCost(newCost, arc);
93 96
                         heap.insert(destLabel);
94
-                        if (Double.isInfinite(oldCost))
95
-                            notifyNodeReached(dest);
96 97
                     }
97 98
                 }
98 99
             }
@@ -115,7 +116,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
115 116
             currentLabel = labels.get(arc.getOrigin().getId());
116 117
         }
117 118
 
118
-        System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
119
+//        System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
119 120
 
120 121
         Collections.reverse(path);
121 122
 

+ 6
- 5
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java View File

@@ -65,10 +65,11 @@ public class Label implements Comparable<Label> {
65 65
 
66 66
     @Override
67 67
     public int compareTo(Label o) {
68
-        int comp = Double.compare(getTotalCost(), o.getTotalCost());
69
-        if (comp == 0 && this.getClass() == LabelStar.class && o.getClass() == LabelStar.class)
70
-            return Double.compare(((LabelStar)this).getEstimatedCost(), ((LabelStar)o).getEstimatedCost());
71
-        else
72
-            return comp;
68
+        int cmp = Double.compare(this.getTotalCost(), o.getTotalCost());
69
+        if (cmp == 0) {
70
+            cmp = Double.compare(o.getCost(), this.getCost());
71
+        }
72
+        return cmp;
73 73
     }
74
+
74 75
 }

+ 0
- 1
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java View File

@@ -1,7 +1,6 @@
1 1
 package org.insa.graphs.algorithm.shortestpath;
2 2
 
3 3
 import org.insa.graphs.model.Node;
4
-import org.insa.graphs.model.Point;
5 4
 
6 5
 public class LabelStar extends Label {
7 6
 

+ 9
- 9
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/ShortestPathTest.java View File

@@ -66,16 +66,16 @@ public abstract class ShortestPathTest {
66 66
                 assertTrue(sol.isFeasible());
67 67
                 Path path = sol.getPath();
68 68
                 assertTrue(path.isValid());
69
-                // check the optimality locally
70
-                if (i == 0)
71
-                    assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
72
-                if (i == 1)
73
-                    assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
74
-                // Check result against Bellman Ford algorithm (except for the square map
75
-                // where there may be several paths with the same cost)
76
-                if (i != 0)
69
+                //  skip these tests for the square map where there may be several arcs with the same cost
70
+                if (i != 0) {
71
+                    // check the optimality locally
72
+                    if (arcInspector == arcInspectors[0])
73
+                        assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
74
+                    if (arcInspector == arcInspectors[1])
75
+                        assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
76
+                    // Check result against Bellman Ford algorithm
77 77
                     assertEquals(sol.getPath(), oracle.getPath());
78
-                System.out.println();
78
+                }
79 79
             }
80 80
         }
81 81
 

+ 1
- 0
be-graphes-all.iml View File

@@ -6,6 +6,7 @@
6 6
     <content url="file://$MODULE_DIR$">
7 7
       <excludeFolder url="file://$MODULE_DIR$/.idea" />
8 8
       <excludeFolder url="file://$MODULE_DIR$/maps" />
9
+      <excludeFolder url="file://$MODULE_DIR$/rapport" />
9 10
       <excludeFolder url="file://$MODULE_DIR$/target" />
10 11
     </content>
11 12
     <orderEntry type="inheritedJdk" />

+ 10
- 17
be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java View File

@@ -22,9 +22,8 @@ public class Launch {
22 22
 
23 23
     /**
24 24
      * Create a new Drawing inside a JFrame an return it.
25
-     * 
25
+     *
26 26
      * @return The created drawing.
27
-     * 
28 27
      * @throws Exception if something wrong happens when creating the graph.
29 28
      */
30 29
     public static Drawing createDrawing() throws Exception {
@@ -45,28 +44,22 @@ public class Launch {
45 44
     }
46 45
 
47 46
     public static void main(String[] args) throws Exception {
48
-
49
-        // Visit these directory to see the list of available files on Commetud.
50 47
         final String mapName = "maps/insa.mapgr";
51 48
         final String pathName = "maps/path_fr31insa_rangueil_insa.path";
52 49
 
53 50
         // Create a graph reader.
54
-        final GraphReader reader = new BinaryGraphReader(
55
-                new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
56
-
57
-        final Graph graph = reader.read();
58
-
59
-        // Create the drawing:
60
-        final Drawing drawing = createDrawing();
61
-
62
-        drawing.drawGraph(graph);
51
+        try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
52
+             final PathReader pathReader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))))) {
63 53
 
64
-        final PathReader pathReader = new BinaryPathReader(
65
-                new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))));
54
+            final Graph graph = reader.read();
66 55
 
67
-        final Path path = pathReader.readPath(graph);
56
+            // Create the drawing:
57
+            final Drawing drawing = createDrawing();
58
+            drawing.drawGraph(graph);
68 59
 
69
-        drawing.drawPath(path);
60
+            final Path path = pathReader.readPath(graph);
61
+            drawing.drawPath(path);
62
+        }
70 63
     }
71 64
 
72 65
 }

Loading…
Cancel
Save