From bed0f37cfadf03d53df8abc520f13c9afc44e633 Mon Sep 17 00:00:00 2001 From: Clement LACAU Date: Tue, 21 May 2024 17:56:24 +0200 Subject: [PATCH] Before big modif --- .../shortestpath/DijkstraAlgorithm.java | 2 +- .../ShortestPathAlgorithmTest.java | 69 +++++++++++++++++-- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java index adb8a68..93637d3 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java @@ -128,7 +128,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { if (arc.getDestination().getId() == current_label.getNode().getId() && data.isAllowed(arc) && data.getCost(arc) < minCost) { - minCost = data.getCost(arc); + minCost = Math.min(data.getCost(arc), minCost); minCostArc = arc; } } diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/ShortestPathAlgorithmTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/ShortestPathAlgorithmTest.java index 19c3f0e..7933763 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/ShortestPathAlgorithmTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/shortestpath/ShortestPathAlgorithmTest.java @@ -9,9 +9,11 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Random; import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspectorFactory; +import org.insa.graphs.algorithm.AbstractInputData.Mode; import org.insa.graphs.model.Graph; import org.insa.graphs.model.Node; import org.insa.graphs.model.Path; @@ -89,10 +91,32 @@ public abstract class ShortestPathAlgorithmTest { final BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); final ShortestPathSolution bell_path = bellman.doRun(); - assert(path.getPath().isValid()); - assert(path.isFeasible()); - assert(Math.abs(algo.getCostPath() - path.getPath().getLength()) < 1.0); - assert(Math.abs(path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0); + if (bell_path.getPath() != null) { + if (bell_path.getPath().isValid()) { + assert(path.getPath().isValid()); + assert(path.isFeasible()); + if (data.getMode() == Mode.LENGTH) { + if (Math.abs( algo.getCostPath() - (double) path.getPath().getLength()) !=0) { + System.out.println(data.getOrigin().getId()); + System.out.println(data.getDestination().getId()); + System.out.println(algo.getCostPath() + " " + path.getPath().getLength()); + } + assert(Math.abs((float) algo.getCostPath() - path.getPath().getLength()) < 1); + assert(Math.abs(path.getPath().getLength() - bell_path.getPath().getLength()) < 1); + } + else { + assert(Math.abs(algo.getCostPath() - path.getPath().getMinimumTravelTime()) == 0); + assert(Math.abs(path.getPath().getMinimumTravelTime() - bell_path.getPath().getMinimumTravelTime()) == 0); + } + } + else { + assert(!path.getPath().isValid()); + assert(!path.isFeasible()); + } + } + else { + assert(!path.isFeasible()); + } } /* @@ -103,13 +127,13 @@ public abstract class ShortestPathAlgorithmTest { final ShortestPathAlgorithm algo = initAlgo(data); final ShortestPathSolution path = algo.doRun(); - + assert(!path.isFeasible()); assert(path.getPath() == null); } @Test - /* + /* * Map: carre.mapgr * Chemin: 19 --> 4 * Tous chemins permis @@ -119,6 +143,7 @@ public abstract class ShortestPathAlgorithmTest { public void chemin_court_CARRE_length() { ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs Graph myGraph = graph.get(0); + Node origin = myGraph.get(19); Node destination = myGraph.get(4); @@ -401,4 +426,36 @@ public abstract class ShortestPathAlgorithmTest { // On peut aussi supposer que le nombre d'arcs empruntés est très grand. assert(path.getPath().getArcs().size() > 1000); } + + + +@Test + /* + * Chemin long sur la carte de Toulouse. + * Même si Bellman est de plus long à faire long à faire, ce test prend moins + * de 3s, on estime que ce n'est pas trop et on va utiliser Bellman. + * Par contre, dans le test sur la région midi_pyrenees qui arrive après, on va + * être obligé de trouver une autre solution. + * Origine: 16644 + * Destination: 39229 + * Mode: LENGTH + * PATH UTILISE : ../Paths/custom_paths/long_path_tls.path + */ + public void cheminsToulouse() { + ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); + Graph myGraph = graph.get(2); + Node origin = myGraph.get(16644); + Node destination = myGraph.get(39229); + + Random rand = new Random(); + + int size_graph = myGraph.getNodes().size(); + + for (int i = 0 ; i < 50 ; i++) { + origin = myGraph.get(Math.abs(rand.nextInt()) % size_graph); + destination = myGraph.get(Math.abs(rand.nextInt()) % size_graph); + + assertBellmanHasSameResult(myGraph, origin, destination, arcInspector); + } + } }