Browse Source

Importation du code depuis GitHub

gasc 2 months ago
commit
59329c4dbc
100 changed files with 9963 additions and 0 deletions
  1. 28
    0
      README.md
  2. 25
    0
      be-graphes-algos/pom.xml
  3. 87
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AbstractAlgorithm.java
  4. 97
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AbstractInputData.java
  5. 88
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AbstractSolution.java
  6. 128
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AlgorithmFactory.java
  7. 43
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspector.java
  8. 177
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspectorFactory.java
  9. 24
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingAlgorithm.java
  10. 13
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingData.java
  11. 5
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingGraphicObserver.java
  12. 5
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingObserver.java
  13. 11
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingSolution.java
  14. 5
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingTextObserver.java
  15. 29
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchAlgorithm.java
  16. 13
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchData.java
  17. 5
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchGraphicObserver.java
  18. 5
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchObserver.java
  19. 11
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchSolution.java
  20. 5
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchTextObserver.java
  21. 58
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java
  22. 100
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/BellmanFordAlgorithm.java
  23. 154
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java
  24. 69
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ShortestPathAlgorithm.java
  25. 47
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ShortestPathData.java
  26. 37
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ShortestPathObserver.java
  27. 74
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ShortestPathSolution.java
  28. 37
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/ShortestPathTextObserver.java
  29. 234
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java
  30. 198
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeapFormatter.java
  31. 64
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java
  32. 32
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/ElementNotFoundException.java
  33. 16
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/EmptyPriorityQueueException.java
  34. 81
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java
  35. 30
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentObserver.java
  36. 36
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentTextObserver.java
  37. 159
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java
  38. 20
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentsData.java
  39. 57
    0
      be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentsSolution.java
  40. 15
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarTest.java
  41. 17
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BellmanFordTest.java
  42. 15
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinaryHeapTest.java
  43. 15
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinarySearchTreeTest.java
  44. 16
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java
  45. 196
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PccAlgorithmTest.java
  46. 320
    0
      be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PriorityQueueTest.java
  47. 87
    0
      be-graphes-gui/pom.xml
  48. 381
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/AlgorithmPanel.java
  49. 56
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/BlockingActionFactory.java
  50. 24
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/DrawingChangeListener.java
  51. 14
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/GraphChangeListener.java
  52. 123
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/GraphReaderProgressBar.java
  53. 862
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/MainWindow.java
  54. 424
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/NodesInputPanel.java
  55. 361
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/PathsPanel.java
  56. 33
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/RunningAction.java
  57. 289
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/SolutionPanel.java
  58. 54
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/StreamCapturer.java
  59. 62
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/ThreadWrapper.java
  60. 82
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/BasicGraphPalette.java
  61. 19
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/BlackAndWhiteGraphPalette.java
  62. 157
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/Drawing.java
  63. 14
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/DrawingClickListener.java
  64. 23
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/GraphPalette.java
  65. 116
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/MercatorProjection.java
  66. 68
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/PlateCarreProjection.java
  67. 51
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/Projection.java
  68. 747
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/BasicDrawing.java
  69. 521
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/MapViewDrawing.java
  70. 209
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/MapZoomControls.java
  71. 177
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/ZoomAndPanListener.java
  72. 73
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/MarkerAutoScaling.java
  73. 19
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/MarkerOverlay.java
  74. 97
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/MarkerUtils.java
  75. 42
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/Overlay.java
  76. 65
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/PaintUtils.java
  77. 5
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/PathOverlay.java
  78. 70
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/PointSetOverlay.java
  79. 92
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/overlays/PolylineAutoScaling.java
  80. 42
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/observers/ShortestPathGraphicObserver.java
  81. 42
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/observers/WeaklyConnectedComponentGraphicObserver.java
  82. 78
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java
  83. 21
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/utils/ColorUtils.java
  84. 151
    0
      be-graphes-gui/src/main/java/org/insa/graphs/gui/utils/FileUtils.java
  85. BIN
      be-graphes-gui/src/main/resources/delete-icon.png
  86. BIN
      be-graphes-gui/src/main/resources/marker_mask.bin
  87. BIN
      be-graphes-gui/src/main/resources/save-icon.png
  88. BIN
      be-graphes-gui/src/main/resources/zoomIn.png
  89. BIN
      be-graphes-gui/src/main/resources/zoomOut.png
  90. 17
    0
      be-graphes-model/pom.xml
  91. 236
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/AccessRestrictions.java
  92. 70
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/Arc.java
  93. 55
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/ArcBackward.java
  94. 68
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/ArcForward.java
  95. 131
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/Graph.java
  96. 204
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/GraphStatistics.java
  97. 159
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/Node.java
  98. 297
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/Path.java
  99. 74
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/Point.java
  100. 0
    0
      be-graphes-model/src/main/java/org/insa/graphs/model/RoadInformation.java

+ 28
- 0
README.md View File

@@ -0,0 +1,28 @@
1
+# I3MIIL11 - Bureau d'étude graphes
2
+
3
+Répertoire de travail pour le bureau d'étude graphes de 3 MIC à l'INSA de Toulouse.
4
+
5
+## Remarques
6
+Ce BE sera à priori fait sous VSCode (sous Ubuntu 20.04.02) au lieu d'Eclispe pour des raisons de praticité et d'habitudes (et aussi parce que je n'ai pas énormément de place sur mon disque dur). Jusqu'à présent ce choix ne m'a posé aucun problème hormis pour générer la docs.
7
+
8
+## Algorithme de Dijkstra (PCC)
9
+Voici quelques résultats visuels de mon algorithme de Dijkstra en mode plus court chemin. D'abord sans observateurs, en vert sans contrainte de chemin, en rouge les routes autorisées aux voitures uniquement :
10
+
11
+![2 chemins](rsc/img1.png)
12
+
13
+Exemple de résultat avec observateurs :
14
+
15
+![avec observateurs](rsc/img2.png)
16
+
17
+## Test
18
+### Test avec oracle 
19
+Pour effectuer ces tests, j'ai utiliser le GUI pour créer un oracle avec Bellman Ford (un trajet entre l'aéroport LFBO et l'INSA de Toulouse sur la carte Haute Garonne). Notez que les tests peuvent être effectués avec n'importe quel autre oracle (il suffit de changer la carte et l'oracle).
20
+ 
21
+On vérifie alors que la solution retournée est equivalente à l'oracle. *Note: On vérifie pas l'égalité strict des chemins car (théoriquement)  deux chemins optimaux peuvent co-exister.*
22
+
23
+### Test d'optimalité (tests sans oracle)
24
+Plusieurs tests sans oracle ont été implémentés :
25
+- Test des sous-chemins : tous les sous-chemins doivent également suivre les mêmes nodes
26
+- Test du vol d'oiseau : la distance en vol d'oiseau doit être plus courte que la distance obtenue
27
+- Test de l'inégalité triangulaire : le même trajet passant par un troisième point doit être plus long ou égal en distance au chemin trouvé
28
+

+ 25
- 0
be-graphes-algos/pom.xml View File

@@ -0,0 +1,25 @@
1
+<?xml version="1.0"?>
2
+<project
3
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4
+	xmlns="http://maven.apache.org/POM/4.0.0"
5
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
6
+	<modelVersion>4.0.0</modelVersion>
7
+	
8
+	<parent>
9
+		<groupId>org.insa.graphs</groupId>
10
+		<artifactId>be-graphes-all</artifactId>
11
+		<version>0.0.1-SNAPSHOT</version>
12
+	</parent>
13
+	
14
+	<artifactId>be-graphes-algos</artifactId>
15
+	<name>be-graphes-algos</name>
16
+	
17
+	<dependencies>
18
+		<dependency>
19
+			<groupId>org.insa.graphs</groupId>
20
+			<artifactId>be-graphes-model</artifactId>
21
+			<version>${project.version}</version>
22
+		</dependency>
23
+	</dependencies>
24
+	
25
+</project>

+ 87
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AbstractAlgorithm.java View File

@@ -0,0 +1,87 @@
1
+package org.insa.graphs.algorithm;
2
+
3
+import java.time.Duration;
4
+import java.time.Instant;
5
+import java.util.ArrayList;
6
+
7
+/**
8
+ * Base class for algorithm classes.
9
+ *
10
+ * @param <Observer> Observer type for the algorithm.
11
+ */
12
+public abstract class AbstractAlgorithm<Observer> {
13
+
14
+    // Input data for the algorithm
15
+    protected final AbstractInputData data;
16
+
17
+    // List of observers for the algorithm
18
+    protected final ArrayList<Observer> observers;
19
+
20
+    /**
21
+     * Create a new algorithm with an empty list of observers.
22
+     * 
23
+     * @param data Input data for the algorithm.
24
+     */
25
+    protected AbstractAlgorithm(AbstractInputData data) {
26
+        this.data = data;
27
+        this.observers = new ArrayList<Observer>();
28
+    }
29
+
30
+    /**
31
+     * Create a new algorithm with the given list of observers.
32
+     * 
33
+     * @param data Input data for the algorithm.
34
+     * @param observers Initial list of observers for the algorithm.
35
+     */
36
+    protected AbstractAlgorithm(AbstractInputData data, ArrayList<Observer> observers) {
37
+        this.data = data;
38
+        this.observers = observers;
39
+    }
40
+
41
+    /**
42
+     * Add an observer to this algorithm.
43
+     * 
44
+     * @param observer Observer to add to this algorithm.
45
+     */
46
+    public void addObserver(Observer observer) {
47
+        observers.add(observer);
48
+    }
49
+
50
+    /**
51
+     * @return The list of observers for this algorithm.
52
+     */
53
+    public ArrayList<Observer> getObservers() {
54
+        return observers;
55
+    }
56
+
57
+    /**
58
+     * @return Input for this algorithm.
59
+     */
60
+    public AbstractInputData getInputData() {
61
+        return data;
62
+    }
63
+
64
+    /**
65
+     * Run the algorithm and return the solution.
66
+     * 
67
+     * This methods internally time the call to doRun() and update the result of the
68
+     * call with the computed solving time.
69
+     * 
70
+     * @return The solution found by the algorithm (may not be a feasible solution).
71
+     */
72
+    public AbstractSolution run() {
73
+        Instant start = Instant.now();
74
+        AbstractSolution solution = this.doRun();
75
+        solution.setSolvingTime(Duration.between(start, Instant.now()));
76
+        return solution;
77
+    }
78
+
79
+    /**
80
+     * Abstract method that should be implemented by child class.
81
+     * 
82
+     * @return The solution found, must not be null (use an infeasible or unknown
83
+     * status if necessary).
84
+     */
85
+    protected abstract AbstractSolution doRun();
86
+
87
+}

+ 97
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AbstractInputData.java View File

@@ -0,0 +1,97 @@
1
+package org.insa.graphs.algorithm;
2
+
3
+import org.insa.graphs.model.Arc;
4
+import org.insa.graphs.model.Graph;
5
+import org.insa.graphs.model.GraphStatistics;
6
+
7
+/**
8
+ * Base class for algorithm input data classes. This class contains the basic
9
+ * data that are required by most graph algorithms, i.e. a graph, a mode (time /
10
+ * length) and a filter for the arc.
11
+ *
12
+ */
13
+public abstract class AbstractInputData {
14
+
15
+    /**
16
+     * Enum specifying the top mode of the algorithms.
17
+     * 
18
+     * @see ArcInspector
19
+     */
20
+    public enum Mode {
21
+        TIME, LENGTH
22
+    }
23
+
24
+    // Graph
25
+    private final Graph graph;
26
+
27
+    // Arc filter.
28
+    protected final ArcInspector arcInspector;
29
+
30
+    /**
31
+     * Create a new AbstractInputData instance for the given graph, mode and filter.
32
+     * 
33
+     * @param graph Graph for this input data.
34
+     * @param arcInspector Arc inspector for this input data.
35
+     */
36
+    protected AbstractInputData(Graph graph, ArcInspector arcInspector) {
37
+        this.graph = graph;
38
+        this.arcInspector = arcInspector;
39
+    }
40
+
41
+    /**
42
+     * @return Graph associated with this input.
43
+     */
44
+    public Graph getGraph() {
45
+        return graph;
46
+    }
47
+
48
+    /**
49
+     * Retrieve the cost associated with the given arc according to the underlying
50
+     * arc inspector.
51
+     * 
52
+     * @param arc Arc for which cost should be retrieved.
53
+     * 
54
+     * @return Cost for the given arc.
55
+     * 
56
+     * @see ArcInspector
57
+     */
58
+    public double getCost(Arc arc) {
59
+        return this.arcInspector.getCost(arc);
60
+    }
61
+
62
+    /**
63
+     * @return Mode associated with this input data.
64
+     * 
65
+     * @see Mode
66
+     */
67
+    public Mode getMode() {
68
+        return this.arcInspector.getMode();
69
+    }
70
+
71
+    /**
72
+     * Retrieve the maximum speed associated with this input data, or
73
+     * {@link GraphStatistics#NO_MAXIMUM_SPEED} if none is associated. The maximum
74
+     * speed associated with input data is different from the maximum speed
75
+     * associated with graph (accessible via {@link Graph#getGraphInformation()}).
76
+     * 
77
+     * @return The maximum speed for this inspector, or
78
+     *         {@link GraphStatistics#NO_MAXIMUM_SPEED} if none is set.
79
+     */
80
+    public int getMaximumSpeed() {
81
+        return this.arcInspector.getMaximumSpeed();
82
+    }
83
+
84
+    /**
85
+     * Check if the given arc is allowed for the filter corresponding to this input.
86
+     * 
87
+     * @param arc Arc to check.
88
+     * 
89
+     * @return true if the given arc is allowed.
90
+     * 
91
+     * @see ArcInspector
92
+     */
93
+    public boolean isAllowed(Arc arc) {
94
+        return this.arcInspector.isAllowed(arc);
95
+    }
96
+
97
+}

+ 88
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AbstractSolution.java View File

@@ -0,0 +1,88 @@
1
+package org.insa.graphs.algorithm;
2
+
3
+import java.time.Duration;
4
+
5
+/**
6
+ * Base class for solution classes returned by the algorithm. This class
7
+ * contains the basic information that any solution should have: status of the
8
+ * solution (unknown, infeasible, etc.), solving time and the original input
9
+ * data.
10
+ */
11
+public abstract class AbstractSolution {
12
+
13
+    /**
14
+     * Possible status for a solution.
15
+     *
16
+     */
17
+    public enum Status {
18
+        UNKNOWN, INFEASIBLE, FEASIBLE, OPTIMAL,
19
+    };
20
+
21
+    // Status of the solution.
22
+    private final Status status;
23
+
24
+    // Solving time for the solution.
25
+    private Duration solvingTime;
26
+
27
+    // Original input of the solution.
28
+    private final AbstractInputData data;
29
+
30
+    /**
31
+     * Create a new abstract solution with unknown status.
32
+     * 
33
+     * @param data
34
+     */
35
+    protected AbstractSolution(AbstractInputData data) {
36
+        this.data = data;
37
+        this.solvingTime = Duration.ZERO;
38
+        this.status = Status.UNKNOWN;
39
+    }
40
+
41
+    /**
42
+     * 
43
+     * @param data
44
+     * @param status
45
+     */
46
+    protected AbstractSolution(AbstractInputData data, Status status) {
47
+        this.data = data;
48
+        this.status = status;
49
+    }
50
+
51
+    /**
52
+     * @return Original input for this solution.
53
+     */
54
+    public AbstractInputData getInputData() {
55
+        return data;
56
+    }
57
+
58
+    /**
59
+     * @return Status of this solution.
60
+     */
61
+    public Status getStatus() {
62
+        return status;
63
+    }
64
+
65
+    /**
66
+     * @return Solving time of this solution.
67
+     */
68
+    public Duration getSolvingTime() {
69
+        return solvingTime;
70
+    }
71
+
72
+    /**
73
+     * Set the solving time of this solution.
74
+     * 
75
+     * @param solvingTime Solving time for the solution.
76
+     */
77
+    protected void setSolvingTime(Duration solvingTime) {
78
+        this.solvingTime = solvingTime;
79
+    }
80
+
81
+    /**
82
+     * @return true if the solution is feasible or optimal.
83
+     */
84
+    public boolean isFeasible() {
85
+        return status == Status.FEASIBLE || status == Status.OPTIMAL;
86
+    }
87
+
88
+}

+ 128
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/AlgorithmFactory.java View File

@@ -0,0 +1,128 @@
1
+package org.insa.graphs.algorithm;
2
+
3
+import java.lang.reflect.Constructor;
4
+import java.util.IdentityHashMap;
5
+import java.util.LinkedHashMap;
6
+import java.util.Map;
7
+import java.util.Set;
8
+import java.util.TreeSet;
9
+
10
+import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
11
+import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
12
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
13
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
14
+import org.insa.graphs.algorithm.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
15
+
16
+/**
17
+ * Factory class used to register and retrieve algorithms based on their common
18
+ * ancestor and name.
19
+ *
20
+ */
21
+public class AlgorithmFactory {
22
+
23
+    // Map between algorithm names and class.
24
+    private final static Map<Class<? extends AbstractAlgorithm<?>>, Map<String, Class<? extends AbstractAlgorithm<?>>>> ALGORITHMS = new IdentityHashMap<>();
25
+
26
+    static {
27
+        // Register weakly-connected components algorithm:
28
+        registerAlgorithm(WeaklyConnectedComponentsAlgorithm.class, "WCC basic",
29
+                WeaklyConnectedComponentsAlgorithm.class);
30
+
31
+        // Register shortest path algorithm:
32
+        registerAlgorithm(ShortestPathAlgorithm.class, "Bellman-Ford", BellmanFordAlgorithm.class);
33
+        registerAlgorithm(ShortestPathAlgorithm.class, "Dijkstra", DijkstraAlgorithm.class);
34
+        registerAlgorithm(ShortestPathAlgorithm.class, "A*", AStarAlgorithm.class);
35
+
36
+        // Register your algorithms here:
37
+        // registerAlgorithm(CarPoolingAlgorithm.class, "My Awesome Algorithm",
38
+        // MyCarPoolingAlgorithm.class);
39
+    }
40
+
41
+    /**
42
+     * Register the given algorithm class with the given name as a child class of
43
+     * the given base algorithm.
44
+     * 
45
+     * @param baseAlgorithm Base algorithm class that corresponds to the newly
46
+     *                      registered algorithm class (e.g., generic algorithm
47
+     *                      class for the problem).
48
+     * @param name          Name for the registered algorithm class.
49
+     * @param algoClass     Algorithm class to register.
50
+     */
51
+    public static void registerAlgorithm(Class<? extends AbstractAlgorithm<?>> baseAlgorithm,
52
+            String name, Class<? extends AbstractAlgorithm<?>> algoClass) {
53
+        if (!ALGORITHMS.containsKey(baseAlgorithm)) {
54
+            ALGORITHMS.put(baseAlgorithm, new LinkedHashMap<>());
55
+        }
56
+        ALGORITHMS.get(baseAlgorithm).put(name, algoClass);
57
+    }
58
+
59
+    /**
60
+     * Create an instance of the given algorithm class using the given input data.
61
+     * Assuming algorithm correspond to a class "Algorithm", this function returns
62
+     * an object equivalent to `new Algorithm(data)`.
63
+     * 
64
+     * @param algorithm Class of the algorithm to create.
65
+     * @param data      Input data for the algorithm.
66
+     * 
67
+     * @return A new instance of the given algorithm class using the given data.
68
+     * 
69
+     * @throws Exception if something wrong happens when constructing the object,
70
+     *                   i.e. the given input data does not correspond to the given
71
+     *                   algorithm and/or no constructor that takes a single
72
+     *                   parameter of type (data.getClass()) exists.
73
+     */
74
+    public static AbstractAlgorithm<?> createAlgorithm(
75
+            Class<? extends AbstractAlgorithm<?>> algorithm, AbstractInputData data)
76
+            throws Exception {
77
+        // Retrieve the set of constructors for the given algorithm class.
78
+        Constructor<?>[] constructors = algorithm.getDeclaredConstructors();
79
+
80
+        // Within this set, find the constructor that can be called with "data" (only).
81
+        AbstractAlgorithm<?> constructed = null;
82
+        for (Constructor<?> c: constructors) {
83
+            Class<?>[] params = c.getParameterTypes();
84
+            if (params.length == 1 && params[0].isAssignableFrom(data.getClass())) {
85
+                c.setAccessible(true);
86
+                constructed = (AbstractAlgorithm<?>) c.newInstance(new Object[] { data });
87
+                break;
88
+            }
89
+        }
90
+        return constructed;
91
+    }
92
+
93
+    /**
94
+     * Return the algorithm class corresponding to the given base algorithm class
95
+     * and name. The algorithm must have been previously registered using
96
+     * registerAlgorithm.
97
+     * 
98
+     * @param baseAlgorithm Base algorithm class for the algorithm to retrieve.
99
+     * @param name          Name of the algorithm to retrieve.
100
+     * 
101
+     * @return Class corresponding to the given name.
102
+     * 
103
+     * @see #registerAlgorithm
104
+     */
105
+    public static Class<? extends AbstractAlgorithm<?>> getAlgorithmClass(
106
+            Class<? extends AbstractAlgorithm<?>> baseAlgorithm, String name) {
107
+        return ALGORITHMS.get(baseAlgorithm).get(name);
108
+    }
109
+
110
+    /**
111
+     * Return the list of names corresponding to the registered algorithm classes
112
+     * for the given base algorithm class.
113
+     * 
114
+     * @param baseAlgorithm Base algorithm class for the algorithm class names to
115
+     *                      retrieve.
116
+     * 
117
+     * @return Names of the currently registered algorithms.
118
+     * 
119
+     * @see #registerAlgorithm
120
+     */
121
+    public static Set<String> getAlgorithmNames(
122
+            Class<? extends AbstractAlgorithm<?>> baseAlgorithm) {
123
+        if (!ALGORITHMS.containsKey(baseAlgorithm)) {
124
+            return new TreeSet<>();
125
+        }
126
+        return ALGORITHMS.get(baseAlgorithm).keySet();
127
+    }
128
+}

+ 43
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspector.java View File

@@ -0,0 +1,43 @@
1
+package org.insa.graphs.algorithm;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData.Mode;
4
+import org.insa.graphs.model.Arc;
5
+import org.insa.graphs.model.GraphStatistics;
6
+
7
+/**
8
+ * This class can be used to indicate to an algorithm which arcs can be used and
9
+ * the costs of the usable arcs..
10
+ *
11
+ */
12
+public interface ArcInspector {
13
+
14
+    /**
15
+     * Check if the given arc can be used (is allowed).
16
+     * 
17
+     * @param arc Arc to check.
18
+     * 
19
+     * @return true if the given arc is allowed.
20
+     */
21
+    public boolean isAllowed(Arc arc);
22
+
23
+    /**
24
+     * Find the cost of the given arc.
25
+     * 
26
+     * @param arc Arc for which the cost should be returned.
27
+     * 
28
+     * @return Cost of the arc.
29
+     */
30
+    public double getCost(Arc arc);
31
+
32
+    /**
33
+     * @return The maximum speed for this inspector, or
34
+     *         {@link GraphStatistics#NO_MAXIMUM_SPEED} if none is set.
35
+     */
36
+    public int getMaximumSpeed();
37
+
38
+    /**
39
+     * @return Mode for this arc inspector.
40
+     */
41
+    public Mode getMode();
42
+
43
+}

+ 177
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspectorFactory.java View File

@@ -0,0 +1,177 @@
1
+package org.insa.graphs.algorithm;
2
+
3
+import java.util.ArrayList;
4
+import java.util.EnumSet;
5
+import java.util.List;
6
+
7
+import org.insa.graphs.algorithm.AbstractInputData.Mode;
8
+import org.insa.graphs.model.Arc;
9
+import org.insa.graphs.model.GraphStatistics;
10
+import org.insa.graphs.model.AccessRestrictions.AccessMode;
11
+import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
12
+
13
+public class ArcInspectorFactory {
14
+
15
+    /**
16
+     * @return List of all arc filters in this factory.
17
+     */
18
+    public static List<ArcInspector> getAllFilters() {
19
+        List<ArcInspector> filters = new ArrayList<>();
20
+
21
+        // Common filters:
22
+
23
+        // No filter (all arcs allowed):
24
+        filters.add(new ArcInspector() {
25
+            @Override
26
+            public boolean isAllowed(Arc arc) {
27
+                return true;
28
+            }
29
+
30
+            @Override
31
+            public double getCost(Arc arc) {
32
+                return arc.getLength();
33
+            }
34
+
35
+            @Override
36
+            public int getMaximumSpeed() {
37
+                return GraphStatistics.NO_MAXIMUM_SPEED;
38
+            }
39
+
40
+            @Override
41
+            public Mode getMode() {
42
+                return Mode.LENGTH;
43
+            }
44
+
45
+            @Override
46
+            public String toString() {
47
+                return "Shortest path, all roads allowed";
48
+            }
49
+        });
50
+
51
+        // Only road allowed for cars and length:
52
+        filters.add(new ArcInspector() {
53
+            @Override
54
+            public boolean isAllowed(Arc arc) {
55
+                return arc.getRoadInformation().getAccessRestrictions()
56
+                        .isAllowedForAny(AccessMode.MOTORCAR, EnumSet.complementOf(EnumSet
57
+                                .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE)));
58
+            }
59
+
60
+            @Override
61
+            public double getCost(Arc arc) {
62
+                return arc.getLength();
63
+            }
64
+
65
+            @Override
66
+            public int getMaximumSpeed() {
67
+                return GraphStatistics.NO_MAXIMUM_SPEED;
68
+            }
69
+
70
+            @Override
71
+            public Mode getMode() {
72
+                return Mode.LENGTH;
73
+            }
74
+
75
+            @Override
76
+            public String toString() {
77
+                return "Shortest path, only roads open for cars";
78
+            }
79
+        });
80
+
81
+        // Only road allowed for cars and time:
82
+
83
+        filters.add(new ArcInspector() {
84
+            @Override
85
+            public boolean isAllowed(Arc arc) {
86
+                return true;
87
+            }
88
+
89
+            @Override
90
+            public double getCost(Arc arc) {
91
+                return arc.getMinimumTravelTime();
92
+            }
93
+
94
+            @Override
95
+            public int getMaximumSpeed() {
96
+                return GraphStatistics.NO_MAXIMUM_SPEED;
97
+            }
98
+
99
+            @Override
100
+            public Mode getMode() {
101
+                return Mode.TIME;
102
+            }
103
+
104
+            @Override
105
+            public String toString() {
106
+                return "Fastest path, all roads allowed";
107
+            }
108
+        });
109
+
110
+        filters.add(new ArcInspector() {
111
+            @Override
112
+            public boolean isAllowed(Arc arc) {
113
+                return arc.getRoadInformation().getAccessRestrictions()
114
+                        .isAllowedForAny(AccessMode.MOTORCAR, EnumSet.complementOf(EnumSet
115
+                                .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE)));
116
+            }
117
+
118
+            @Override
119
+            public double getCost(Arc arc) {
120
+                return arc.getMinimumTravelTime();
121
+            }
122
+
123
+            @Override
124
+            public int getMaximumSpeed() {
125
+                return GraphStatistics.NO_MAXIMUM_SPEED;
126
+            }
127
+
128
+            @Override
129
+            public Mode getMode() {
130
+                return Mode.TIME;
131
+            }
132
+
133
+            @Override
134
+            public String toString() {
135
+                return "Fastest path, only roads open for cars";
136
+            }
137
+        });
138
+
139
+        // Non-private roads for pedestrian and bicycle:
140
+        filters.add(new ArcInspector() {
141
+
142
+            @Override
143
+            public boolean isAllowed(Arc arc) {
144
+                return arc.getRoadInformation().getAccessRestrictions()
145
+                        .isAllowedForAny(AccessMode.FOOT, EnumSet.complementOf(EnumSet
146
+                                .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE)));
147
+            }
148
+
149
+            @Override
150
+            public double getCost(Arc arc) {
151
+                return arc.getTravelTime(
152
+                        Math.min(getMaximumSpeed(), arc.getRoadInformation().getMaximumSpeed()));
153
+            }
154
+
155
+            @Override
156
+            public String toString() {
157
+                return "Fastest path for pedestrian";
158
+            }
159
+
160
+            @Override
161
+            public int getMaximumSpeed() {
162
+                return 5;
163
+            }
164
+
165
+            @Override
166
+            public Mode getMode() {
167
+                return Mode.TIME;
168
+            }
169
+        });
170
+
171
+        // Add your own filters here (do not forget to implement toString()
172
+        // to get an understandable output!):
173
+
174
+        return filters;
175
+    }
176
+
177
+}

+ 24
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingAlgorithm.java View File

@@ -0,0 +1,24 @@
1
+package org.insa.graphs.algorithm.carpooling;
2
+
3
+import org.insa.graphs.algorithm.AbstractAlgorithm;
4
+
5
+public abstract class CarPoolingAlgorithm extends AbstractAlgorithm<CarPoolingObserver> {
6
+
7
+    protected CarPoolingAlgorithm(CarPoolingData data) {
8
+        super(data);
9
+    }
10
+
11
+    @Override
12
+    public CarPoolingSolution run() {
13
+        return (CarPoolingSolution) super.run();
14
+    }
15
+
16
+    @Override
17
+    protected abstract CarPoolingSolution doRun();
18
+
19
+    @Override
20
+    public CarPoolingData getInputData() {
21
+        return (CarPoolingData) super.getInputData();
22
+    }
23
+
24
+}

+ 13
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingData.java View File

@@ -0,0 +1,13 @@
1
+package org.insa.graphs.algorithm.carpooling;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData;
4
+import org.insa.graphs.algorithm.ArcInspector;
5
+import org.insa.graphs.model.Graph;
6
+
7
+public class CarPoolingData extends AbstractInputData {
8
+
9
+    protected CarPoolingData(Graph graph, ArcInspector arcFilter) {
10
+        super(graph, arcFilter);
11
+    }
12
+
13
+}

+ 5
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingGraphicObserver.java View File

@@ -0,0 +1,5 @@
1
+package org.insa.graphs.algorithm.carpooling;
2
+
3
+public class CarPoolingGraphicObserver implements CarPoolingObserver {
4
+
5
+}

+ 5
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingObserver.java View File

@@ -0,0 +1,5 @@
1
+package org.insa.graphs.algorithm.carpooling;
2
+
3
+public interface CarPoolingObserver {
4
+
5
+}

+ 11
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingSolution.java View File

@@ -0,0 +1,11 @@
1
+package org.insa.graphs.algorithm.carpooling;
2
+
3
+import org.insa.graphs.algorithm.AbstractSolution;
4
+
5
+public class CarPoolingSolution extends AbstractSolution {
6
+
7
+    protected CarPoolingSolution(CarPoolingData data, Status status) {
8
+        super(data, status);
9
+    }
10
+
11
+}

+ 5
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/carpooling/CarPoolingTextObserver.java View File

@@ -0,0 +1,5 @@
1
+package org.insa.graphs.algorithm.carpooling;
2
+
3
+public class CarPoolingTextObserver implements CarPoolingObserver {
4
+
5
+}

+ 29
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchAlgorithm.java View File

@@ -0,0 +1,29 @@
1
+package org.insa.graphs.algorithm.packageswitch;
2
+
3
+import org.insa.graphs.algorithm.AbstractAlgorithm;
4
+
5
+public abstract class PackageSwitchAlgorithm extends AbstractAlgorithm<PackageSwitchObserver> {
6
+
7
+    /**
8
+     * Create a new PackageSwitchAlgorithm with the given data.
9
+     * 
10
+     * @param data
11
+     */
12
+    protected PackageSwitchAlgorithm(PackageSwitchData data) {
13
+        super(data);
14
+    }
15
+
16
+    @Override
17
+    public PackageSwitchSolution run() {
18
+        return (PackageSwitchSolution) super.run();
19
+    }
20
+
21
+    @Override
22
+    protected abstract PackageSwitchSolution doRun();
23
+
24
+    @Override
25
+    public PackageSwitchData getInputData() {
26
+        return (PackageSwitchData) super.getInputData();
27
+    }
28
+
29
+}

+ 13
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchData.java View File

@@ -0,0 +1,13 @@
1
+package org.insa.graphs.algorithm.packageswitch;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData;
4
+import org.insa.graphs.algorithm.ArcInspector;
5
+import org.insa.graphs.model.Graph;
6
+
7
+public class PackageSwitchData extends AbstractInputData {
8
+
9
+    protected PackageSwitchData(Graph graph, ArcInspector arcFilter) {
10
+        super(graph, arcFilter);
11
+    }
12
+
13
+}

+ 5
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchGraphicObserver.java View File

@@ -0,0 +1,5 @@
1
+package org.insa.graphs.algorithm.packageswitch;
2
+
3
+public class PackageSwitchGraphicObserver implements PackageSwitchObserver {
4
+
5
+}

+ 5
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchObserver.java View File

@@ -0,0 +1,5 @@
1
+package org.insa.graphs.algorithm.packageswitch;
2
+
3
+public interface PackageSwitchObserver {
4
+
5
+}

+ 11
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchSolution.java View File

@@ -0,0 +1,11 @@
1
+package org.insa.graphs.algorithm.packageswitch;
2
+
3
+import org.insa.graphs.algorithm.AbstractSolution;
4
+
5
+public class PackageSwitchSolution extends AbstractSolution {
6
+
7
+    protected PackageSwitchSolution(PackageSwitchData data, Status status) {
8
+        super(data, status);
9
+    }
10
+
11
+}

+ 5
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/packageswitch/PackageSwitchTextObserver.java View File

@@ -0,0 +1,5 @@
1
+package org.insa.graphs.algorithm.packageswitch;
2
+
3
+public class PackageSwitchTextObserver implements PackageSwitchObserver {
4
+
5
+}

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

@@ -0,0 +1,58 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData.Mode;
4
+import org.insa.graphs.model.Node;
5
+import org.insa.graphs.model.Point;
6
+
7
+public class AStarAlgorithm extends DijkstraAlgorithm {
8
+
9
+    Node arrivee;
10
+    Mode mode;
11
+    double max_speed;
12
+
13
+    class LabelStar extends Label {
14
+
15
+        Node arrivee;
16
+        double heuristiqueArrive;
17
+
18
+        public LabelStar(Node sommet_courant, Node pere, double cout, Node arrivee) {
19
+            super(sommet_courant, pere, cout);
20
+            this.arrivee = arrivee;
21
+
22
+            switch (mode) {
23
+                case TIME:
24
+                    this.heuristiqueArrive = this.sommet_courant.getPoint().distanceTo(arrivee.getPoint()) / max_speed;
25
+                    break;
26
+                case LENGTH:
27
+                    this.heuristiqueArrive = Point.distance(super.sommet_courant.getPoint(), arrivee.getPoint());
28
+                    break;
29
+            }
30
+        }
31
+
32
+        @Override
33
+        public int compareTo(Label other) throws IllegalArgumentException {
34
+            return Double.compare(
35
+                this.cout + heuristiqueArrive, 
36
+                other.cout + ((LabelStar)other).heuristiqueArrive);
37
+        }
38
+    }
39
+
40
+    public AStarAlgorithm(ShortestPathData data) {
41
+        super(data);
42
+        this.arrivee = data.getDestination();
43
+        this.mode = data.getMode();
44
+        this.max_speed = (double) data.getMaximumSpeed()/3.6;
45
+        if (this.max_speed < 0){
46
+            this.max_speed = (double) data.getGraph().getGraphInformation().getMaximumSpeed() / 3.6;                
47
+        }
48
+    }
49
+
50
+    @Override
51
+    public Label createLabel(Node n) {
52
+        return new LabelStar(n, null, Float.MAX_VALUE, arrivee);
53
+    }
54
+
55
+    public Label createLabel(Node n, double cout) {
56
+        return new LabelStar(n, null, cout, arrivee);
57
+    }
58
+}

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

@@ -0,0 +1,100 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.Collections;
6
+
7
+import org.insa.graphs.algorithm.AbstractSolution.Status;
8
+import org.insa.graphs.model.Arc;
9
+import org.insa.graphs.model.Graph;
10
+import org.insa.graphs.model.Node;
11
+import org.insa.graphs.model.Path;
12
+
13
+public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
14
+
15
+    public BellmanFordAlgorithm(ShortestPathData data) {
16
+        super(data);
17
+    }
18
+
19
+    @Override
20
+    protected ShortestPathSolution doRun() {
21
+
22
+        // Retrieve the graph.
23
+        ShortestPathData data = getInputData();
24
+        Graph graph = data.getGraph();
25
+
26
+        final int nbNodes = graph.size();
27
+
28
+        // Initialize array of distances.
29
+        double[] distances = new double[nbNodes];
30
+        Arrays.fill(distances, Double.POSITIVE_INFINITY);
31
+        distances[data.getOrigin().getId()] = 0;
32
+
33
+        // Notify observers about the first event (origin processed).
34
+        notifyOriginProcessed(data.getOrigin());
35
+
36
+        // Initialize array of predecessors.
37
+        Arc[] predecessorArcs = new Arc[nbNodes];
38
+
39
+        // Actual algorithm, we will assume the graph does not contain negative
40
+        // cycle...
41
+        boolean found = false;
42
+        for (int i = 0; !found && i < nbNodes; ++i) {
43
+            found = true;
44
+            for (Node node: graph.getNodes()) {
45
+                for (Arc arc: node.getSuccessors()) {
46
+
47
+                    // Small test to check allowed roads...
48
+                    if (!data.isAllowed(arc)) {
49
+                        continue;
50
+                    }
51
+
52
+                    // Retrieve weight of the arc.
53
+                    double w = data.getCost(arc);
54
+                    double oldDistance = distances[arc.getDestination().getId()];
55
+                    double newDistance = distances[node.getId()] + w;
56
+
57
+                    if (Double.isInfinite(oldDistance) && Double.isFinite(newDistance)) {
58
+                        notifyNodeReached(arc.getDestination());
59
+                    }
60
+
61
+                    // Check if new distances would be better, if so update...
62
+                    if (newDistance < oldDistance) {
63
+                        found = false;
64
+                        distances[arc.getDestination().getId()] = distances[node.getId()] + w;
65
+                        predecessorArcs[arc.getDestination().getId()] = arc;
66
+                    }
67
+                }
68
+            }
69
+        }
70
+
71
+        ShortestPathSolution solution = null;
72
+
73
+        // Destination has no predecessor, the solution is infeasible...
74
+        if (predecessorArcs[data.getDestination().getId()] == null) {
75
+            solution = new ShortestPathSolution(data, Status.INFEASIBLE);
76
+        }
77
+        else {
78
+
79
+            // The destination has been found, notify the observers.
80
+            notifyDestinationReached(data.getDestination());
81
+
82
+            // Create the path from the array of predecessors...
83
+            ArrayList<Arc> arcs = new ArrayList<>();
84
+            Arc arc = predecessorArcs[data.getDestination().getId()];
85
+            while (arc != null) {
86
+                arcs.add(arc);
87
+                arc = predecessorArcs[arc.getOrigin().getId()];
88
+            }
89
+
90
+            // Reverse the path...
91
+            Collections.reverse(arcs);
92
+
93
+            // Create the final solution.
94
+            solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
95
+        }
96
+
97
+        return solution;
98
+    }
99
+
100
+}

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

@@ -0,0 +1,154 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Collections;
5
+import java.util.HashMap;
6
+
7
+import org.insa.graphs.algorithm.AbstractSolution.Status;
8
+import org.insa.graphs.model.Arc;
9
+import org.insa.graphs.model.Node;
10
+import org.insa.graphs.model.Path;
11
+
12
+import org.insa.graphs.algorithm.utils.*;
13
+
14
+public class DijkstraAlgorithm extends ShortestPathAlgorithm {
15
+
16
+    class Label implements Comparable<Label> {
17
+        Node sommet_courant, pere;
18
+        Boolean marque;
19
+        double cout;
20
+
21
+        public Label(Node sommet_courant, Node pere, double cout) {
22
+            this.sommet_courant = sommet_courant;
23
+            this.pere = pere;
24
+            this.marque = false;
25
+            this.cout = cout;
26
+        }
27
+
28
+        public double getCost() {
29
+            return cout;
30
+        }
31
+
32
+        public void setMark() {
33
+            this.marque = true;
34
+        }
35
+
36
+        public int compareTo(Label other) {
37
+            return Double.compare(cout, other.cout);
38
+        }
39
+    }
40
+
41
+    public DijkstraAlgorithm(ShortestPathData data) {
42
+        super(data);
43
+    }
44
+
45
+    public Label createLabel(Node n){
46
+        return new Label(n, null, Float.MAX_VALUE);
47
+    }
48
+
49
+    public Label createLabel(Node n, double cout) {
50
+        return new Label(n, null, cout);
51
+    }
52
+
53
+    @Override
54
+    protected ShortestPathSolution doRun() {
55
+        // Récupération des données du problème
56
+        final ShortestPathData data = getInputData();
57
+
58
+        // Cas triviaux
59
+        if (data.getOrigin() == data.getDestination())
60
+            return new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), data.getOrigin()));
61
+
62
+        // Tas des nodes visités, initialisée avec le node d'origine
63
+        BinaryHeap<Label> visite = new BinaryHeap<Label>();
64
+        visite.insert(new Label(data.getOrigin(), null, 0));
65
+
66
+        notifyOriginProcessed(data.getOrigin());
67
+
68
+        // Map de correspondance Node - Label initialisée avec des labels par défaut.
69
+        HashMap<Node, Label> map = new HashMap<Node, Label>();
70
+        for (Node n : data.getGraph().getNodes())
71
+            map.put(n, createLabel(n));
72
+
73
+        /// Varibles de fonctionnement
74
+        // Varible contenant le node le plus intéressant
75
+        Label smallest;
76
+        // Variable contenant le node destination
77
+        Node dest = data.getDestination();
78
+
79
+        while (true) {
80
+            // Est-ce qu'il reste des nodes à explorer ?
81
+            // Si oui, on récupère le plus intéressant
82
+            // Sinon, il n'y a pas de solution --> on quitte
83
+            try {
84
+                smallest = visite.deleteMin();
85
+            } catch (EmptyPriorityQueueException e) {
86
+                return new ShortestPathSolution(data, Status.INFEASIBLE);
87
+            }
88
+
89
+            // On marque ce node
90
+            smallest.setMark();
91
+            notifyNodeMarked(smallest.sommet_courant);
92
+
93
+            // Si on est arrivé, on arrête
94
+            if (smallest.sommet_courant == dest) break;
95
+
96
+            // Pour chacun des sommets fils, on regarde s'ils proposent des chemins plus intéressants
97
+            for (Arc arc : smallest.sommet_courant.getSuccessors()) {
98
+
99
+                // On test si le chemin n'est pas permis, on skip
100
+                if (!data.isAllowed(arc)) continue;
101
+
102
+                Node node = arc.getDestination();
103
+                Label l = map.get(node);
104
+
105
+                // S'il est déjà marqué, on skip
106
+                if (l.marque) continue;
107
+
108
+                double n_cout = smallest.getCost() + data.getCost(arc);
109
+
110
+                //if (l.compareTo(createLabel(node, n_cout)) == 1) {
111
+                if (l.cout > n_cout) {
112
+                    visite.tryremove(l);
113
+                    l.cout = n_cout;
114
+                    l.pere = smallest.sommet_courant;
115
+                    l.sommet_courant = node;
116
+                    notifyNodeReached(node);
117
+                    visite.insert(l);
118
+                }
119
+            }
120
+        }
121
+
122
+        notifyDestinationReached(smallest.sommet_courant);
123
+
124
+        // Liste des nodes formant la route entre notre origine et destination
125
+        ArrayList<Node> nodes = new ArrayList<Node>();
126
+        Label last = smallest;
127
+
128
+        // On remplit la liste en se référant au père à chaque fois
129
+        nodes.add(last.sommet_courant);
130
+        while (true) {
131
+            smallest = map.get(smallest.pere);
132
+            nodes.add(smallest.sommet_courant);
133
+            if (smallest == map.get(data.getOrigin())) break;
134
+        }
135
+
136
+        // On inverve le sens des nodes 
137
+        Collections.reverse(nodes);
138
+
139
+        // On créé le chemin
140
+        Path p = Path.createShortestPathFromNodes(data.getGraph(), nodes);
141
+
142
+        /** Vérification des résultats --> Toutes les tests sont ok */
143
+        // Vérification des coûts croisants (l'adjectif, pas la patisserie)
144
+        //for (Node n: nodes) System.out.println(map.get(n).cout);
145
+
146
+        // Vérification du chemin
147
+        //if (p.isValid()) System.out.println("Chemin valide !");
148
+        //System.out.println("CD: " + last.cout + "; CPCC: " + p.getLength());
149
+
150
+
151
+        // On créé on chemin à partir de la liste que l'on donne en solution
152
+        return new ShortestPathSolution(data, Status.OPTIMAL, p);
153
+    }
154
+}

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

@@ -0,0 +1,69 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import org.insa.graphs.algorithm.AbstractAlgorithm;
4
+import org.insa.graphs.model.Node;
5
+
6
+public abstract class ShortestPathAlgorithm extends AbstractAlgorithm<ShortestPathObserver> {
7
+
8
+    protected ShortestPathAlgorithm(ShortestPathData data) {
9
+        super(data);
10
+    }
11
+
12
+    @Override
13
+    public ShortestPathSolution run() {
14
+        return (ShortestPathSolution) super.run();
15
+    }
16
+
17
+    @Override
18
+    protected abstract ShortestPathSolution doRun();
19
+
20
+    @Override
21
+    public ShortestPathData getInputData() {
22
+        return (ShortestPathData) super.getInputData();
23
+    }
24
+
25
+    /**
26
+     * Notify all observers that the origin has been processed.
27
+     * 
28
+     * @param node Origin.
29
+     */
30
+    public void notifyOriginProcessed(Node node) {
31
+        for (ShortestPathObserver obs: getObservers()) {
32
+            obs.notifyOriginProcessed(node);
33
+        }
34
+    }
35
+
36
+    /**
37
+     * Notify all observers that a node has been reached for the first time.
38
+     * 
39
+     * @param node Node that has been reached.
40
+     */
41
+    public void notifyNodeReached(Node node) {
42
+        for (ShortestPathObserver obs: getObservers()) {
43
+            obs.notifyNodeReached(node);
44
+        }
45
+    }
46
+
47
+    /**
48
+     * Notify all observers that a node has been marked, i.e. its final value has
49
+     * been set.
50
+     * 
51
+     * @param node Node that has been marked.
52
+     */
53
+    public void notifyNodeMarked(Node node) {
54
+        for (ShortestPathObserver obs: getObservers()) {
55
+            obs.notifyNodeMarked(node);
56
+        }
57
+    }
58
+
59
+    /**
60
+     * Notify all observers that the destination has been reached.
61
+     * 
62
+     * @param node Destination.
63
+     */
64
+    public void notifyDestinationReached(Node node) {
65
+        for (ShortestPathObserver obs: getObservers()) {
66
+            obs.notifyDestinationReached(node);
67
+        }
68
+    }
69
+}

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

@@ -0,0 +1,47 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData;
4
+import org.insa.graphs.algorithm.ArcInspector;
5
+import org.insa.graphs.model.Graph;
6
+import org.insa.graphs.model.Node;
7
+
8
+public class ShortestPathData extends AbstractInputData {
9
+
10
+    // Origin and destination nodes.
11
+    private final Node origin, destination;
12
+
13
+    /**
14
+     * Construct a new instance of ShortestPathInputData with the given parameters.
15
+     * 
16
+     * @param graph Graph in which the path should be looked for.
17
+     * @param origin Origin node of the path.
18
+     * @param destination Destination node of the path.
19
+     * @param arcInspector Filter for arcs (used to allow only a specific set of
20
+     *        arcs in the graph to be used).
21
+     */
22
+    public ShortestPathData(Graph graph, Node origin, Node destination, ArcInspector arcInspector) {
23
+        super(graph, arcInspector);
24
+        this.origin = origin;
25
+        this.destination = destination;
26
+    }
27
+
28
+    /**
29
+     * @return Origin node for the path.
30
+     */
31
+    public Node getOrigin() {
32
+        return origin;
33
+    }
34
+
35
+    /**
36
+     * @return Destination node for the path.
37
+     */
38
+    public Node getDestination() {
39
+        return destination;
40
+    }
41
+
42
+    @Override
43
+    public String toString() {
44
+        return "Shortest-path from #" + origin.getId() + " to #" + destination.getId() + " ["
45
+                + this.arcInspector.toString().toLowerCase() + "]";
46
+    }
47
+}

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

@@ -0,0 +1,37 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import org.insa.graphs.model.Node;
4
+
5
+public interface ShortestPathObserver {
6
+	
7
+	/**
8
+	 * Notify the observer that the origin has been processed.
9
+	 * 
10
+	 * @param node Origin.
11
+	 */
12
+	public void notifyOriginProcessed(Node node);
13
+	
14
+	/**
15
+	 * Notify the observer that a node has been reached for the first
16
+	 * time.
17
+	 * 
18
+	 * @param node Node that has been reached.
19
+	 */
20
+	public void notifyNodeReached(Node node);
21
+	
22
+	/**
23
+	 * Notify the observer that a node has been marked, i.e. its final
24
+	 * value has been set.
25
+	 * 
26
+	 * @param node Node that has been marked.
27
+	 */
28
+	public void notifyNodeMarked(Node node);
29
+
30
+	/**
31
+	 * Notify the observer that the destination has been reached.
32
+	 * 
33
+	 * @param node Destination.
34
+	 */
35
+	public void notifyDestinationReached(Node node);
36
+	
37
+}

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

@@ -0,0 +1,74 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData.Mode;
4
+import org.insa.graphs.model.Arc;
5
+import org.insa.graphs.model.Path;
6
+import org.insa.graphs.algorithm.AbstractSolution;
7
+
8
+public class ShortestPathSolution extends AbstractSolution {
9
+
10
+    // Optimal solution.
11
+    private final Path path;
12
+
13
+    /**
14
+     * Create a new infeasible shortest-path solution for the given input and
15
+     * status.
16
+     * 
17
+     * @param data Original input data for this solution.
18
+     * @param status Status of the solution (UNKNOWN / INFEASIBLE).
19
+     */
20
+    public ShortestPathSolution(ShortestPathData data, Status status) {
21
+        super(data, status);
22
+        this.path = null;
23
+    }
24
+
25
+    /**
26
+     * Create a new shortest-path solution.
27
+     * 
28
+     * @param data Original input data for this solution.
29
+     * @param status Status of the solution (FEASIBLE / OPTIMAL).
30
+     * @param path Path corresponding to the solution.
31
+     */
32
+    public ShortestPathSolution(ShortestPathData data, Status status, Path path) {
33
+        super(data, status);
34
+        this.path = path;
35
+    }
36
+
37
+    @Override
38
+    public ShortestPathData getInputData() {
39
+        return (ShortestPathData) super.getInputData();
40
+    }
41
+
42
+    /**
43
+     * @return The path of this solution, if any.
44
+     */
45
+    public Path getPath() {
46
+        return path;
47
+    }
48
+
49
+    @Override
50
+    public String toString() {
51
+        String info = null;
52
+        if (!isFeasible()) {
53
+            info = String.format("No path found from node #%d to node #%d",
54
+                    getInputData().getOrigin().getId(), getInputData().getDestination().getId());
55
+        }
56
+        else {
57
+            double cost = 0;
58
+            for (Arc arc: getPath().getArcs()) {
59
+                cost += getInputData().getCost(arc);
60
+            }
61
+            info = String.format("Found a path from node #%d to node #%d",
62
+                    getInputData().getOrigin().getId(), getInputData().getDestination().getId());
63
+            if (getInputData().getMode() == Mode.LENGTH) {
64
+                info = String.format("%s, %.4f kilometers", info, cost / 1000.0);
65
+            }
66
+            else {
67
+                info = String.format("%s, %.4f minutes", info, cost / 60.0);
68
+            }
69
+        }
70
+        info += " in " + getSolvingTime().getSeconds() + " seconds.";
71
+        return info;
72
+    }
73
+
74
+}

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

@@ -0,0 +1,37 @@
1
+package org.insa.graphs.algorithm.shortestpath;
2
+
3
+import java.io.PrintStream;
4
+
5
+import org.insa.graphs.model.Node;
6
+
7
+public class ShortestPathTextObserver implements ShortestPathObserver {
8
+
9
+    private final PrintStream stream;
10
+
11
+    public ShortestPathTextObserver(PrintStream stream) {
12
+        this.stream = stream;
13
+    }
14
+
15
+    @Override
16
+    public void notifyOriginProcessed(Node node) {
17
+        // TODO Auto-generated method stub
18
+
19
+    }
20
+
21
+    @Override
22
+    public void notifyNodeReached(Node node) {
23
+        stream.println("Node " + node.getId() + " reached.");
24
+    }
25
+
26
+    @Override
27
+    public void notifyNodeMarked(Node node) {
28
+        stream.println("Node " + node.getId() + " marked.");
29
+    }
30
+
31
+    @Override
32
+    public void notifyDestinationReached(Node node) {
33
+        // TODO Auto-generated method stub
34
+
35
+    }
36
+
37
+}

+ 234
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java View File

@@ -0,0 +1,234 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import java.util.ArrayList;
4
+
5
+/**
6
+ * Implements a binary heap containing elements of type E.
7
+ *
8
+ * Note that all comparisons are based on the compareTo method, hence E must
9
+ * implement Comparable
10
+ * 
11
+ * @author Mark Allen Weiss
12
+ * @author DLB
13
+ */
14
+public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
15
+
16
+    // Number of elements in heap.
17
+    private int currentSize;
18
+
19
+    // The heap array.
20
+    protected final ArrayList<E> array;
21
+
22
+    /**
23
+     * Construct a new empty binary heap.
24
+     */
25
+    public BinaryHeap() {
26
+        this.currentSize = 0;
27
+        this.array = new ArrayList<E>();
28
+    }
29
+
30
+    /**
31
+     * Construct a copy of the given heap.
32
+     * 
33
+     * @param heap Binary heap to copy.
34
+     */
35
+    public BinaryHeap(BinaryHeap<E> heap) {
36
+        this.currentSize = heap.currentSize;
37
+        this.array = new ArrayList<E>(heap.array);
38
+    }
39
+
40
+    /**
41
+     * Set an element at the given index.
42
+     * 
43
+     * @param index Index at which the element should be set.
44
+     * @param value Element to set.
45
+     */
46
+    private void arraySet(int index, E value) {
47
+        if (index == this.array.size()) {
48
+            this.array.add(value);
49
+        }
50
+        else {
51
+            this.array.set(index, value);
52
+        }
53
+    }
54
+
55
+    /**
56
+     * @return Index of the parent of the given index.
57
+     */
58
+    protected int indexParent(int index) {
59
+        return (index - 1) / 2;
60
+    }
61
+
62
+    /**
63
+     * @return Index of the left child of the given index.
64
+     */
65
+    protected int indexLeft(int index) {
66
+        return index * 2 + 1;
67
+    }
68
+
69
+    /**
70
+     * Internal method to percolate up in the heap.
71
+     * 
72
+     * @param index Index at which the percolate begins.
73
+     */
74
+    private void percolateUp(int index) {
75
+        E x = this.array.get(index);
76
+
77
+        for (; index > 0
78
+                && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
79
+                        index)) {
80
+            E moving_val = this.array.get(indexParent(index));
81
+            this.arraySet(index, moving_val);
82
+        }
83
+
84
+        this.arraySet(index, x);
85
+    }
86
+
87
+    /**
88
+     * Internal method to percolate down in the heap.
89
+     * 
90
+     * @param index Index at which the percolate begins.
91
+     */
92
+    private void percolateDown(int index) {
93
+        int ileft = indexLeft(index);
94
+        int iright = ileft + 1;
95
+
96
+        if (ileft < this.currentSize) {
97
+            E current = this.array.get(index);
98
+            E left = this.array.get(ileft);
99
+            boolean hasRight = iright < this.currentSize;
100
+            E right = (hasRight) ? this.array.get(iright) : null;
101
+
102
+            if (!hasRight || left.compareTo(right) < 0) {
103
+                // Left is smaller
104
+                if (left.compareTo(current) < 0) {
105
+                    this.arraySet(index, left);
106
+                    this.arraySet(ileft, current);
107
+                    this.percolateDown(ileft);
108
+                }
109
+            }
110
+            else {
111
+                // Right is smaller
112
+                if (right.compareTo(current) < 0) {
113
+                    this.arraySet(index, right);
114
+                    this.arraySet(iright, current);
115
+                    this.percolateDown(iright);
116
+                }
117
+            }
118
+        }
119
+    }
120
+
121
+    @Override
122
+    public boolean isEmpty() {
123
+        return this.currentSize == 0;
124
+    }
125
+
126
+    @Override
127
+    public int size() {
128
+        return this.currentSize;
129
+    }
130
+
131
+    @Override
132
+    public void insert(E x) {
133
+        int index = this.currentSize++;
134
+        this.arraySet(index, x);
135
+        this.percolateUp(index);
136
+    }
137
+
138
+    @Override
139
+    public void remove(E x) throws ElementNotFoundException {
140
+        // Difficile de faire mieux que du O(n) ici 
141
+        // Surtout qu'un tas binaire n'est pas fait pour
142
+        // chercher un élément...
143
+        int ie = this.array.indexOf(x);
144
+
145
+        //int ie = 0;
146
+    	//while (ie<this.currentSize && !this.array.get(ie).equals(x)) ie++;
147
+
148
+
149
+        if (this.isEmpty() || ie < 0 || ie >= this.currentSize) 
150
+            throw new ElementNotFoundException(x);
151
+
152
+        E last = this.array.get(--this.currentSize);
153
+        this.arraySet(ie, last);
154
+        this.percolateUp(ie);
155
+        this.percolateDown(ie);
156
+    }
157
+
158
+    /** Try to remove an element without raising any exception */
159
+    public void tryremove(E x) {
160
+        try {
161
+            remove(x);
162
+        }
163
+        catch (ElementNotFoundException e) {
164
+            ;
165
+        }
166
+    }
167
+
168
+    public boolean isIn(E x) {
169
+        return this.array.contains(x);
170
+    }
171
+
172
+    @Override
173
+    public E findMin() throws EmptyPriorityQueueException {
174
+        if (isEmpty())
175
+            throw new EmptyPriorityQueueException();
176
+        return this.array.get(0);
177
+    }
178
+
179
+    @Override
180
+    public E deleteMin() throws EmptyPriorityQueueException {
181
+        E minItem = findMin();
182
+        E lastItem = this.array.get(--this.currentSize);
183
+        this.arraySet(0, lastItem);
184
+        this.percolateDown(0);
185
+        return minItem;
186
+    }
187
+
188
+    /**
189
+     * Creates a multi-lines string representing a sorted view of this binary heap.
190
+     * 
191
+     * @return a string containing a sorted view this binary heap.
192
+     */
193
+    public String toStringSorted() {
194
+        return BinaryHeapFormatter.toStringSorted(this, -1);
195
+    }
196
+
197
+    /**
198
+     * Creates a multi-lines string representing a sorted view of this binary heap.
199
+     * 
200
+     * @param maxElement Maximum number of elements to display. or {@code -1} to
201
+     *                   display all the elements.
202
+     * 
203
+     * @return a string containing a sorted view this binary heap.
204
+     */
205
+    public String toStringSorted(int maxElement) {
206
+        return BinaryHeapFormatter.toStringSorted(this, maxElement);
207
+    }
208
+
209
+    /**
210
+     * Creates a multi-lines string representing a tree view of this binary heap.
211
+     * 
212
+     * @return a string containing a tree view of this binary heap.
213
+     */
214
+    public String toStringTree() {
215
+        return BinaryHeapFormatter.toStringTree(this, Integer.MAX_VALUE);
216
+    }
217
+
218
+    /**
219
+     * Creates a multi-lines string representing a tree view of this binary heap.
220
+     * 
221
+     * @param maxDepth Maximum depth of the tree to display.
222
+     * 
223
+     * @return a string containing a tree view of this binary heap.
224
+     */
225
+    public String toStringTree(int maxDepth) {
226
+        return BinaryHeapFormatter.toStringTree(this, maxDepth);
227
+    }
228
+
229
+    @Override
230
+    public String toString() {
231
+        return BinaryHeapFormatter.toStringTree(this, 8);
232
+    }
233
+
234
+}

+ 198
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeapFormatter.java View File

@@ -0,0 +1,198 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class BinaryHeapFormatter {
6
+
7
+    /**
8
+     * This class is used by {@link #toStringTree}, and simply contains three string
9
+     * accumulating. This is an immutable class.
10
+     *
11
+     */
12
+    private static class Context {
13
+
14
+        // Output text:
15
+        public final String acu;
16
+
17
+        // Margin to get back exactly under the current position:
18
+        public final String margin;
19
+
20
+        // Last margin used for the last child of a node. The markers are different:
21
+        public final String lastmargin;
22
+
23
+        /**
24
+         * Creaet a new {@code Context}.
25
+         * 
26
+         * @param acu        The accumulated string.
27
+         * @param margin     The current margin.
28
+         * @param lastMargin The last margin used.
29
+         */
30
+        public Context(String acu, String margin, String lastMargin) {
31
+            this.acu = acu;
32
+            this.margin = margin;
33
+            this.lastmargin = lastMargin;
34
+        }
35
+
36
+        /**
37
+         * Creates a new context by appending newlines to this context.
38
+         * 
39
+         * @param n Number of newlines to append.
40
+         * 
41
+         * @return a new context with {@code n} newlines appended.
42
+         */
43
+        public Context appendNewlines(int n) {
44
+            if (n <= 0) {
45
+                return this;
46
+            }
47
+            else {
48
+                return (new Context(this.acu + "\n" + this.margin, this.margin, this.lastmargin)
49
+                        .appendNewlines(n - 1));
50
+            }
51
+        }
52
+
53
+        /**
54
+         * Creates a new context by appending the given string to this context.
55
+         * 
56
+         * @param count Number of spaces to add to the margin, or {@code null} to use
57
+         *              the length of the string.
58
+         * @param text  String to append.
59
+         * 
60
+         * @return a new context with {@code text} appended.
61
+         */
62
+        public Context appendText(Integer count, String text) {
63
+            int cnt = (count == null) ? text.length() : count;
64
+            final String spaces = new String(new char[cnt]).replace('\0', ' ');
65
+            return new Context(this.acu + text, this.margin + spaces, this.lastmargin + spaces);
66
+        }
67
+
68
+        /**
69
+         * Creates a new context by appending a branch to this context.
70
+         * 
71
+         * @param n     Number of spaces to add to the margin, or {@code null} to use
72
+         *              the length of the string.
73
+         * @param label Name of the branch.
74
+         * 
75
+         * @return a new context with the branch appended.
76
+         */
77
+        public Context appendBranch(Integer count, String label) {
78
+            final Context ctxt = this.appendText(count, label);
79
+
80
+            if (count == null) {
81
+                return new Context(ctxt.acu + "_", ctxt.margin + "|", ctxt.margin + " ");
82
+            }
83
+            else {
84
+                return new Context(ctxt.acu, ctxt.margin + "|", ctxt.margin + " ")
85
+                        .appendNewlines(1);
86
+
87
+            }
88
+        }
89
+    }
90
+
91
+    /*
92
+     * Input : ready to write the current node at the current context position.
93
+     * Output : the last character of acu is the last character of the current node.
94
+     */
95
+    protected static <E extends Comparable<E>> Context toStringLoop(BinaryHeap<E> heap,
96
+            Context ctxt, int node, int max_depth) {
97
+
98
+        if (max_depth < 0) {
99
+            return ctxt.appendText(null, "...");
100
+        }
101
+        else {
102
+            E nodeval = heap.array.get(node);
103
+            String nodevals = nodeval.toString();
104
+
105
+            ArrayList<Integer> childs = new ArrayList<Integer>();
106
+            // Add childs
107
+            int index_left = heap.indexLeft(node);
108
+            int index_right = index_left + 1;
109
+
110
+            if (index_left < heap.size()) {
111
+                childs.add(index_left);
112
+            }
113
+            if (index_right < heap.size()) {
114
+                childs.add(index_right);
115
+            }
116
+
117
+            Context ctxt2 = childs.isEmpty() ? ctxt.appendText(null, nodevals)
118
+                    : ctxt.appendBranch(1, nodevals);
119
+
120
+            for (int ch = 0; ch < childs.size(); ch++) {
121
+                boolean is_last = (ch == childs.size() - 1);
122
+                int child = childs.get(ch);
123
+
124
+                if (is_last) {
125
+                    Context ctxt3 = new Context(ctxt2.acu, ctxt2.lastmargin, ctxt2.lastmargin);
126
+                    ctxt2 = new Context(toStringLoop(heap, ctxt3.appendText(null, "___"), child,
127
+                            max_depth - 1).acu, ctxt2.margin, ctxt2.lastmargin);
128
+                }
129
+                else {
130
+                    ctxt2 = new Context(toStringLoop(heap, ctxt2.appendText(null, "___"), child,
131
+                            max_depth - 1).acu, ctxt2.margin, ctxt2.lastmargin).appendNewlines(2);
132
+                }
133
+            }
134
+
135
+            return ctxt2;
136
+        }
137
+    }
138
+
139
+    /**
140
+     * Creates a multi-lines string representing a tree view of the given binary
141
+     * heap.
142
+     * 
143
+     * @param heap     The binary heap to display.
144
+     * @param maxDepth Maximum depth of the tree to display.
145
+     * 
146
+     * @return a string containing a tree view of the given binary heap.
147
+     */
148
+    public static <E extends Comparable<E>> String toStringTree(BinaryHeap<E> heap, int maxDepth) {
149
+        final Context init_context = new Context("   ", "   ", "   ");
150
+        final Context result = toStringLoop(heap, init_context, 0, maxDepth);
151
+        return result.acu;
152
+    }
153
+
154
+    /**
155
+     * Creates a multi-lines string representing a sorted view of the given binary
156
+     * heap.
157
+     * 
158
+     * @param heap       The binary heap to display.
159
+     * @param maxElement Maximum number of elements to display. or {@code -1} to
160
+     *                   display all the elements.
161
+     * 
162
+     * @return a string containing a sorted view the given binary heap.
163
+     */
164
+    public static <E extends Comparable<E>> String toStringSorted(BinaryHeap<E> heap,
165
+            int max_elements) {
166
+        String result = "";
167
+        final BinaryHeap<E> copy = new BinaryHeap<E>(heap);
168
+
169
+        final String truncate;
170
+        if (max_elements < 0 || max_elements >= heap.size()) {
171
+            truncate = "";
172
+        }
173
+        else {
174
+            truncate = ", only " + max_elements + " elements are shown";
175
+        }
176
+
177
+        result += "========  Sorted HEAP  (size = " + heap.size() + truncate + ")  ========\n\n";
178
+
179
+        while (!copy.isEmpty() && max_elements-- != 0) {
180
+            result += copy.deleteMin() + "\n";
181
+        }
182
+
183
+        result += "\n--------  End of heap  --------";
184
+
185
+        return result;
186
+    }
187
+
188
+    public static void main(String[] args) {
189
+        final BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
190
+
191
+        for (int i = 0; i < 12; i++) {
192
+            heap.insert(i);
193
+        }
194
+
195
+        System.out.println(heap.toStringSorted(-1));
196
+        System.out.println(heap.toStringTree(6));
197
+    }
198
+}

+ 64
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java View File

@@ -0,0 +1,64 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import java.util.SortedSet;
4
+import java.util.TreeSet;
5
+
6
+public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<E> {
7
+
8
+    // Underlying implementation
9
+    private final SortedSet<E> sortedSet;
10
+
11
+    /**
12
+     * Create a new empty binary search tree.
13
+     */
14
+    public BinarySearchTree() {
15
+        this.sortedSet = new TreeSet<>();
16
+    }
17
+
18
+    /**
19
+     * Create a copy of the given binary search tree.
20
+     * 
21
+     * @param bst Binary search tree to copy.
22
+     */
23
+    public BinarySearchTree(BinarySearchTree<E> bst) {
24
+        this.sortedSet = new TreeSet<>(bst.sortedSet);
25
+    }
26
+
27
+    @Override
28
+    public boolean isEmpty() {
29
+        return sortedSet.isEmpty();
30
+    }
31
+
32
+    @Override
33
+    public int size() {
34
+        return sortedSet.size();
35
+    }
36
+
37
+    @Override
38
+    public void insert(E x) {
39
+        sortedSet.add(x);
40
+    }
41
+
42
+    @Override
43
+    public void remove(E x) throws ElementNotFoundException {
44
+        if (!sortedSet.remove(x)) {
45
+            throw new ElementNotFoundException(x);
46
+        }
47
+    }
48
+
49
+    @Override
50
+    public E findMin() throws EmptyPriorityQueueException {
51
+        if (isEmpty()) {
52
+            throw new EmptyPriorityQueueException();
53
+        }
54
+        return sortedSet.first();
55
+    }
56
+
57
+    @Override
58
+    public E deleteMin() throws EmptyPriorityQueueException {
59
+        E min = findMin();
60
+        remove(min);
61
+        return min;
62
+    }
63
+
64
+}

+ 32
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/ElementNotFoundException.java View File

@@ -0,0 +1,32 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+public class ElementNotFoundException extends RuntimeException {
4
+
5
+    /**
6
+     * 
7
+     */
8
+    private static final long serialVersionUID = 1L;
9
+
10
+    // Element not found
11
+    private final Object element;
12
+
13
+    /**
14
+     * @param element Element that was not found.
15
+     */
16
+    public ElementNotFoundException(Object element) {
17
+        this.element = element;
18
+    }
19
+
20
+    /**
21
+     * @return The element that was not found.
22
+     */
23
+    public Object getElement() {
24
+        return this.element;
25
+    }
26
+
27
+    @Override
28
+    public String toString() {
29
+        return "element not found: " + element;
30
+    }
31
+
32
+}

+ 16
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/EmptyPriorityQueueException.java View File

@@ -0,0 +1,16 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+public class EmptyPriorityQueueException extends RuntimeException {
4
+
5
+    /**
6
+     * 
7
+     */
8
+    private static final long serialVersionUID = 1L;
9
+
10
+    /**
11
+     * 
12
+     */
13
+    public EmptyPriorityQueueException() {
14
+    }
15
+
16
+}

+ 81
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java View File

@@ -0,0 +1,81 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+/**
4
+ * Interface representing a basic priority queue.
5
+ * 
6
+ * Implementation should enforce the required complexity of each method.
7
+ * 
8
+ */
9
+public interface PriorityQueue<E extends Comparable<E>> {
10
+
11
+    /**
12
+     * Check if the priority queue is empty.
13
+     * 
14
+     * <p>
15
+     * <b>Complexity:</b> <i>O(1)</i>
16
+     * </p>
17
+     * 
18
+     * @return true if the queue is empty, false otherwise.
19
+     */
20
+    public boolean isEmpty();
21
+
22
+    /**
23
+     * Get the number of elements in this queue.
24
+     * 
25
+     * <p>
26
+     * <b>Complexity:</b> <i>O(1)</i>
27
+     * </p>
28
+     * 
29
+     * @return Current size (number of elements) of this queue.
30
+     */
31
+    public int size();
32
+
33
+    /**
34
+     * Insert the given element into the queue.
35
+     * 
36
+     * <p>
37
+     * <b>Complexity:</b> <i>O(log n)</i>
38
+     * </p>
39
+     * 
40
+     * @param x Item to insert.
41
+     */
42
+    public void insert(E x);
43
+
44
+    /**
45
+     * Remove the given element from the priority queue.
46
+     * 
47
+     * <p>
48
+     * <b>Complexity:</b> <i>O(log n)</i>
49
+     * </p>
50
+     * 
51
+     * @param x Item to remove.
52
+     */
53
+    public void remove(E x) throws ElementNotFoundException;
54
+
55
+    /**
56
+     * Retrieve (but not remove) the smallest item in the queue.
57
+     * 
58
+     * <p>
59
+     * <b>Complexity:</b> <i>O(1)</i>
60
+     * </p>
61
+     * 
62
+     * @return The smallest item in the queue.
63
+     * 
64
+     * @throws EmptyPriorityQueueException if this queue is empty.
65
+     */
66
+    public E findMin() throws EmptyPriorityQueueException;
67
+
68
+    /**
69
+     * Remove and return the smallest item from the priority queue.
70
+     * 
71
+     * <p>
72
+     * <b>Complexity:</b> <i>O(log n)</i>
73
+     * </p>
74
+     * 
75
+     * @return The smallest item in the queue.
76
+     * 
77
+     * @throws EmptyPriorityQueueException if this queue is empty.
78
+     */
79
+    public E deleteMin() throws EmptyPriorityQueueException;
80
+
81
+}

+ 30
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentObserver.java View File

@@ -0,0 +1,30 @@
1
+package org.insa.graphs.algorithm.weakconnectivity;
2
+
3
+import java.util.ArrayList;
4
+
5
+import org.insa.graphs.model.Node;
6
+
7
+public interface WeaklyConnectedComponentObserver {
8
+
9
+	/**
10
+	 * Notify that the algorithm is entering a new component.
11
+	 * 
12
+	 * @param curNode Starting node for the component.
13
+	 */
14
+	public void notifyStartComponent(Node curNode);
15
+	
16
+	/**
17
+	 * Notify that a new node has been found for the current component.
18
+	 * 
19
+	 * @param node New node found for the current component.
20
+	 */
21
+	public void notifyNewNodeInComponent(Node node);
22
+	
23
+	/**
24
+	 * Notify that the algorithm has computed a new component.
25
+	 * 
26
+	 * @param nodes List of nodes in the component.
27
+	 */
28
+	public void notifyEndComponent(ArrayList<Node> nodes);
29
+
30
+}

+ 36
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentTextObserver.java View File

@@ -0,0 +1,36 @@
1
+package org.insa.graphs.algorithm.weakconnectivity;
2
+
3
+import java.io.PrintStream;
4
+import java.util.ArrayList;
5
+
6
+import org.insa.graphs.model.Node;
7
+
8
+public class WeaklyConnectedComponentTextObserver implements WeaklyConnectedComponentObserver {
9
+	
10
+	// Number of the current component.
11
+	private int numComponent = 1;
12
+	
13
+	// Output stream
14
+	PrintStream stream;
15
+
16
+	public WeaklyConnectedComponentTextObserver(PrintStream stream) {
17
+		this.stream = stream;
18
+	}
19
+
20
+	@Override
21
+	public void notifyStartComponent(Node curNode) {
22
+		stream.print("Entering component #" + numComponent + " from node #" + curNode.getId() + "... ");
23
+	}
24
+
25
+	@Override
26
+	public void notifyNewNodeInComponent(Node node) {
27
+	}
28
+
29
+	@Override
30
+	public void notifyEndComponent(ArrayList<Node> nodes) {
31
+		stream.println(nodes.size() + " nodes found.");
32
+		stream.flush();
33
+		numComponent += 1;
34
+	}
35
+
36
+}

+ 159
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java View File

@@ -0,0 +1,159 @@
1
+package org.insa.graphs.algorithm.weakconnectivity;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.HashSet;
6
+import java.util.LinkedList;
7
+import java.util.Queue;
8
+
9
+import org.insa.graphs.algorithm.AbstractAlgorithm;
10
+import org.insa.graphs.algorithm.AbstractSolution.Status;
11
+import org.insa.graphs.model.Arc;
12
+import org.insa.graphs.model.Graph;
13
+import org.insa.graphs.model.Node;
14
+
15
+public class WeaklyConnectedComponentsAlgorithm
16
+        extends AbstractAlgorithm<WeaklyConnectedComponentObserver> {
17
+
18
+    /**
19
+     * @param data Input data for this algorithm.
20
+     */
21
+    public WeaklyConnectedComponentsAlgorithm(WeaklyConnectedComponentsData data) {
22
+        super(data);
23
+    }
24
+
25
+    @Override
26
+    public WeaklyConnectedComponentsSolution run() {
27
+        return (WeaklyConnectedComponentsSolution) super.run();
28
+    }
29
+
30
+    @Override
31
+    public WeaklyConnectedComponentsData getInputData() {
32
+        return (WeaklyConnectedComponentsData) super.getInputData();
33
+    }
34
+
35
+    /**
36
+     * Notify all observers that the algorithm is entering a new component.
37
+     * 
38
+     * @param curNode Starting node for the component.
39
+     */
40
+    protected void notifyStartComponent(Node curNode) {
41
+        for (WeaklyConnectedComponentObserver obs: getObservers()) {
42
+            obs.notifyStartComponent(curNode);
43
+        }
44
+    }
45
+
46
+    /**
47
+     * Notify all observers that a new node has been found for the current
48
+     * component.
49
+     * 
50
+     * @param node New node found for the current component.
51
+     */
52
+    protected void notifyNewNodeInComponent(Node node) {
53
+        for (WeaklyConnectedComponentObserver obs: getObservers()) {
54
+            obs.notifyNewNodeInComponent(node);
55
+        }
56
+    }
57
+
58
+    /**
59
+     * Notify all observers that the algorithm has computed a new component.
60
+     * 
61
+     * @param nodes List of nodes in the component.
62
+     */
63
+    protected void notifyEndComponent(ArrayList<Node> nodes) {
64
+        for (WeaklyConnectedComponentObserver obs: getObservers()) {
65
+            obs.notifyEndComponent(nodes);
66
+        }
67
+    }
68
+
69
+    /**
70
+     * @return An adjacency list for the undirected graph equivalent to the stored
71
+     *         graph.
72
+     */
73
+    protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
74
+        int nNodes = getInputData().getGraph().size();
75
+        ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
76
+        for (int i = 0; i < nNodes; ++i) {
77
+            res.add(new HashSet<Integer>());
78
+        }
79
+
80
+        for (Node node: getInputData().getGraph().getNodes()) {
81
+            for (Arc arc: node.getSuccessors()) {
82
+                res.get(node.getId()).add(arc.getDestination().getId());
83
+                if (arc.getRoadInformation().isOneWay()) {
84
+                    res.get(arc.getDestination().getId()).add(node.getId());
85
+                }
86
+            }
87
+        }
88
+
89
+        return res;
90
+    }
91
+
92
+    /**
93
+     * Apply a breadth first search algorithm on the given undirected graph
94
+     * (adjacency list), starting at node cur, and marking nodes in marked.
95
+     * 
96
+     * @param marked
97
+     * @param cur
98
+     * 
99
+     * @return
100
+     */
101
+    protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
102
+        Graph graph = getInputData().getGraph();
103
+        ArrayList<Node> component = new ArrayList<Node>();
104
+
105
+        // Using a queue because we are doing a BFS
106
+        Queue<Integer> queue = new LinkedList<Integer>();
107
+
108
+        // Notify observers about the current component.
109
+        notifyStartComponent(graph.get(cur));
110
+
111
+        // Add original node and loop until the queue is empty.
112
+        queue.add(cur);
113
+        marked[cur] = true;
114
+        while (!queue.isEmpty()) {
115
+            Node node = graph.get(queue.remove());
116
+            component.add(node);
117
+
118
+            // Notify observers
119
+            notifyNewNodeInComponent(node);
120
+
121
+            for (Integer destId: ugraph.get(node.getId())) {
122
+                Node dest = graph.get(destId);
123
+                if (!marked[dest.getId()]) {
124
+                    queue.add(destId);
125
+                    marked[destId] = true;
126
+                }
127
+            }
128
+        }
129
+
130
+        notifyEndComponent(component);
131
+
132
+        return component;
133
+    }
134
+
135
+    @Override
136
+    protected WeaklyConnectedComponentsSolution doRun() {
137
+
138
+        Graph graph = getInputData().getGraph();
139
+        ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
140
+        boolean[] marked = new boolean[graph.size()];
141
+        Arrays.fill(marked, false);
142
+
143
+        ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();
144
+
145
+        // perform algorithm
146
+        int cur = 0;
147
+        while (cur < marked.length) {
148
+            // Apply BFS
149
+            components.add(this.bfs(ugraph, marked, cur));
150
+
151
+            // Find next non-marked
152
+            for (; cur < marked.length && marked[cur]; ++cur)
153
+                ;
154
+        }
155
+
156
+        return new WeaklyConnectedComponentsSolution(getInputData(), Status.OPTIMAL, components);
157
+    }
158
+
159
+}

+ 20
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentsData.java View File

@@ -0,0 +1,20 @@
1
+package org.insa.graphs.algorithm.weakconnectivity;
2
+
3
+import org.insa.graphs.algorithm.AbstractInputData;
4
+import org.insa.graphs.model.Graph;
5
+
6
+public class WeaklyConnectedComponentsData extends AbstractInputData {
7
+
8
+    /**
9
+     * @param graph Graph for which components should be retrieved.
10
+     */
11
+    public WeaklyConnectedComponentsData(Graph graph) {
12
+        super(graph, null);
13
+    }
14
+
15
+    @Override
16
+    public String toString() {
17
+        return "Weakly-connected components from #0.";
18
+    }
19
+
20
+}

+ 57
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/weakconnectivity/WeaklyConnectedComponentsSolution.java View File

@@ -0,0 +1,57 @@
1
+package org.insa.graphs.algorithm.weakconnectivity;
2
+
3
+import java.util.ArrayList;
4
+
5
+import org.insa.graphs.algorithm.AbstractSolution;
6
+import org.insa.graphs.model.Node;
7
+
8
+public class WeaklyConnectedComponentsSolution extends AbstractSolution {
9
+
10
+    // Components
11
+    private ArrayList<ArrayList<Node>> components;
12
+
13
+    protected WeaklyConnectedComponentsSolution(WeaklyConnectedComponentsData data) {
14
+        super(data);
15
+    }
16
+
17
+    protected WeaklyConnectedComponentsSolution(WeaklyConnectedComponentsData data, Status status,
18
+            ArrayList<ArrayList<Node>> components) {
19
+        super(data, status);
20
+        this.components = components;
21
+    }
22
+
23
+    @Override
24
+    public WeaklyConnectedComponentsData getInputData() {
25
+        return (WeaklyConnectedComponentsData) super.getInputData();
26
+    }
27
+
28
+    /**
29
+     * @return Components of the solution, if any.
30
+     */
31
+    public ArrayList<ArrayList<Node>> getComponents() {
32
+        return components;
33
+    }
34
+
35
+    /*
36
+     * (non-Javadoc)
37
+     * 
38
+     * @see java.lang.Object#toString()
39
+     */
40
+    @Override
41
+    public String toString() {
42
+        int nIsolated = 0;
43
+        int nGt10 = 0;
44
+        for (ArrayList<Node> component: components) {
45
+            if (component.size() == 1) {
46
+                nIsolated += 1;
47
+            }
48
+            else if (component.size() > 10) {
49
+                nGt10 += 1;
50
+            }
51
+        }
52
+        return "Found " + components.size() + " components (" + nGt10 + " with more than 10 nodes, "
53
+                + nIsolated + " isolated nodes) in " + getSolvingTime().getSeconds() + " seconds.";
54
+
55
+    }
56
+
57
+}

+ 15
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/AStarTest.java View File

@@ -0,0 +1,15 @@
1
+package org.insa.graphs.algorithm.utils;
2
+import java.io.IOException;
3
+
4
+import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
5
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
6
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
7
+
8
+public class AStarTest extends PccAlgorithmTest {
9
+    public AStarTest() throws IOException {}
10
+
11
+    @Override
12
+    protected ShortestPathAlgorithm createAlgorithm(ShortestPathData data) {
13
+        return new AStarAlgorithm(data);
14
+    }
15
+}

+ 17
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BellmanFordTest.java View File

@@ -0,0 +1,17 @@
1
+package org.insa.graphs.algorithm.utils;
2
+import java.io.IOException;
3
+
4
+import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
5
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
6
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
7
+
8
+// Note : Faire l'ensemble des tests avec Bellman-Ford est long, l'algorithme étant peu efficace.
9
+
10
+public class BellmanFordTest extends PccAlgorithmTest {
11
+    public BellmanFordTest() throws IOException {}
12
+
13
+    @Override
14
+    protected ShortestPathAlgorithm createAlgorithm(ShortestPathData data) {
15
+        return new BellmanFordAlgorithm(data);
16
+    }
17
+}

+ 15
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinaryHeapTest.java View File

@@ -0,0 +1,15 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+public class BinaryHeapTest extends PriorityQueueTest {
4
+
5
+    @Override
6
+    public PriorityQueue<MutableInteger> createQueue() {
7
+        return new BinaryHeap<>();
8
+    }
9
+
10
+    @Override
11
+    public PriorityQueue<MutableInteger> createQueue(PriorityQueue<MutableInteger> queue) {
12
+        return new BinaryHeap<>((BinaryHeap<MutableInteger>) queue);
13
+    }
14
+
15
+}

+ 15
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinarySearchTreeTest.java View File

@@ -0,0 +1,15 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+public class BinarySearchTreeTest extends PriorityQueueTest {
4
+
5
+    @Override
6
+    public PriorityQueue<MutableInteger> createQueue() {
7
+        return new BinarySearchTree<>();
8
+    }
9
+
10
+    @Override
11
+    public PriorityQueue<MutableInteger> createQueue(PriorityQueue<MutableInteger> queue) {
12
+        return new BinarySearchTree<>((BinarySearchTree<MutableInteger>) queue);
13
+    }
14
+
15
+}

+ 16
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/DijkstraTest.java View File

@@ -0,0 +1,16 @@
1
+package org.insa.graphs.algorithm.utils;
2
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
3
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
4
+
5
+import java.io.IOException;
6
+
7
+import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
8
+
9
+public class DijkstraTest extends PccAlgorithmTest {
10
+    public DijkstraTest() throws IOException {}
11
+
12
+    @Override
13
+    protected ShortestPathAlgorithm createAlgorithm(ShortestPathData data) {
14
+        return new DijkstraAlgorithm(data);
15
+    }
16
+}

+ 196
- 0
be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PccAlgorithmTest.java View File

@@ -0,0 +1,196 @@
1
+package org.insa.graphs.algorithm.utils;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+import static org.junit.Assert.assertTrue;
5
+
6
+import org.junit.Test;
7
+
8
+import java.io.BufferedInputStream;
9
+import java.io.DataInputStream;
10
+import java.io.FileInputStream;
11
+import java.io.IOException;
12
+import java.util.List;
13
+
14
+import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
15
+import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
16
+import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
17
+