From 8168b86d09d54a6cbbd6f7082e584c936e74695b Mon Sep 17 00:00:00 2001 From: Lacroix Raphael Date: Fri, 20 May 2022 08:18:27 +0200 Subject: [PATCH] finished tests and A* --- .../shortestpath/AStarAlgorithm.java | 20 ++++++++++++ .../shortestpath/DijkstraAlgorithm.java | 32 ++++++++++++------- .../insa/graphs/algorithm/utils/Label.java | 23 ++++++++++--- .../graphs/algorithm/utils/LabelStar.java | 18 +++++++++++ .../org/insa/graphs/gui/simple/Launch.java | 32 +++++++++++-------- 5 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/LabelStar.java diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java index fd172f0..040dbf6 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/AStarAlgorithm.java @@ -1,9 +1,29 @@ package org.insa.graphs.algorithm.shortestpath; +import org.insa.graphs.algorithm.AbstractSolution; +import org.insa.graphs.algorithm.utils.BinaryHeap; +import org.insa.graphs.algorithm.utils.Label; +import org.insa.graphs.algorithm.utils.LabelStar; +import org.insa.graphs.model.Arc; +import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Path; + +import java.util.ArrayList; +import java.util.Collections; + public class AStarAlgorithm extends DijkstraAlgorithm { public AStarAlgorithm(ShortestPathData data) { super(data); } + @Override + protected void initLabel(){ + int i = 0; + for (Node l : gr.getNodes()){ + labels[i] = new LabelStar(l, false, null,data.getDestination()); + i++; + } + } } 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 0896ebd..bfd0ca2 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 @@ -3,6 +3,7 @@ package org.insa.graphs.algorithm.shortestpath; import org.insa.graphs.algorithm.AbstractSolution; import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.algorithm.utils.Label; +import org.insa.graphs.algorithm.utils.LabelStar; import org.insa.graphs.model.Arc; import org.insa.graphs.model.Graph; import org.insa.graphs.model.Node; @@ -17,22 +18,29 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { public DijkstraAlgorithm(ShortestPathData data) { super(data); } + final ShortestPathData data = getInputData(); + Graph gr = data.getGraph(); - @Override - public ShortestPathSolution doRun() { - final ShortestPathData data = getInputData(); - Graph gr = data.getGraph(); + // array containing all the labels + Label[] labels = new Label[gr.size()]; - // array containing all the labels - Label[] labels = new Label[gr.size()]; - - - //initialization of the labels + protected void initLabel(){ int i = 0; for (Node l : gr.getNodes()){ labels[i] = new Label(l, false, null); i++; } + } + + @Override + public ShortestPathData getInputData() { + return super.getInputData(); + } + + public ShortestPathSolution doRun() { + + //initialization of the labels + initLabel(); // initialization of the heap & the first node BinaryHeap