From d46ea7fcb4cc5cbc7f5883b6d98b2fc9d3279899 Mon Sep 17 00:00:00 2001 From: Lacroix Raphael Date: Fri, 20 May 2022 10:56:52 +0200 Subject: [PATCH] Fixed A* time --- .../shortestpath/AStarAlgorithm.java | 13 +++++++++- .../graphs/algorithm/utils/LabelStar.java | 7 ++++++ .../org/insa/graphs/gui/simple/Launch.java | 24 +++++++++++++------ 3 files changed, 36 insertions(+), 8 deletions(-) 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 040dbf6..c510dba 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,5 +1,6 @@ package org.insa.graphs.algorithm.shortestpath; +import org.insa.graphs.algorithm.AbstractInputData; import org.insa.graphs.algorithm.AbstractSolution; import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.algorithm.utils.Label; @@ -21,8 +22,18 @@ public class AStarAlgorithm extends DijkstraAlgorithm { @Override protected void initLabel(){ int i = 0; + double maxSpeed = 130; + if (data.getMode() == AbstractInputData.Mode.TIME){ + if (gr.getGraphInformation().hasMaximumSpeed()){ + maxSpeed = gr.getGraphInformation().getMaximumSpeed(); + } + } for (Node l : gr.getNodes()){ - labels[i] = new LabelStar(l, false, null,data.getDestination()); + if (data.getMode() == AbstractInputData.Mode.TIME){ + labels[i] = new LabelStar(l, false, null,data.getDestination(), maxSpeed); + } else { + labels[i] = new LabelStar(l, false, null,data.getDestination()); + } i++; } } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/LabelStar.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/LabelStar.java index 6fd3e80..c801e3a 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/LabelStar.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/LabelStar.java @@ -1,5 +1,7 @@ package org.insa.graphs.algorithm.utils; +import org.insa.graphs.algorithm.AbstractInputData; +import org.insa.graphs.algorithm.shortestpath.ShortestPathData; import org.insa.graphs.model.Arc; import org.insa.graphs.model.Node; @@ -7,6 +9,11 @@ public class LabelStar extends Label{ private double crowCost; + public LabelStar(Node currNode, boolean marked, Arc father, Node dest, double maxSpeed){ + super(currNode,marked,father); + this.crowCost = currNode.getPoint().distanceTo(dest.getPoint())/(maxSpeed*3.6); + } + public LabelStar(Node currNode, boolean marked, Arc father, Node dest){ super(currNode,marked,father); this.crowCost = currNode.getPoint().distanceTo(dest.getPoint()); diff --git a/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java b/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java index 93abb88..46bb5d5 100644 --- a/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java +++ b/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java @@ -21,8 +21,13 @@ import org.insa.graphs.model.io.BinaryPathReader; import org.insa.graphs.model.io.GraphReader; import org.insa.graphs.model.io.PathReader; +// for colouring +import java.util.Random; + import java.awt.Color; + public class Launch { + /** * Create a new Drawing inside a JFrame an return it. * @@ -136,7 +141,12 @@ public class Launch { Path drawPath = createTestPath(graph, Id1, Id2, "BF"); if (drawPath != null){ if (!drawPath.isEmpty()){ - drawing.drawPath(drawPath); + + Random rand = new Random(); + float r = rand.nextFloat(); + float g = rand.nextFloat(); + float b = rand.nextFloat(); + drawing.drawPath(drawPath,new Color(r, g, b)); } } return resu; @@ -146,7 +156,7 @@ public class Launch { // Visit these directory to see the list of available files on Commetud. final String mapName = "/home/rlacroix/Bureau/3MIC/Be Graphe/mapinsa/insa.mapgr"; - final String pathName = "/home/rlacroix/Bureau/3MIC/Be Graphe/mapinsa/path_fr31insa_rangueil_r2.path"; + //final String pathName = "/home/rlacroix/Bureau/3MIC/Be Graphe/mapinsa/path_fr31insa_rangueil_r2.path"; // Create a graph reader. final GraphReader reader = new BinaryGraphReader( @@ -162,19 +172,19 @@ public class Launch { drawing.drawGraph(graph); // TODO: Create a PathReader. - final PathReader pathReader = new BinaryPathReader( - new DataInputStream(new BufferedInputStream(new FileInputStream(pathName)))); + //final PathReader pathReader = new BinaryPathReader( + // new DataInputStream(new BufferedInputStream(new FileInputStream(pathName)))); // TODO: Read the path. - final Path path = pathReader.readPath(graph); + //final Path path = pathReader.readPath(graph); // TODO: Draw the path. - drawing.drawPath(path); + //drawing.drawPath(path); System.out.println("==TESTS=="); - int nbTest = 1000; + int nbTest = 10000; int juste = 0; for (int i = 0; i < nbTest; i++){