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..ad58395 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,35 @@ package org.insa.graphs.algorithm.shortestpath; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import org.insa.graphs.algorithm.AbstractSolution; +import org.insa.graphs.algorithm.utils.BinaryHeap; +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 org.insa.graphs.model.Point; + +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import org.insa.graphs.algorithm.AbstractSolution; +import org.insa.graphs.algorithm.utils.BinaryHeap; +import org.insa.graphs.model.Arc; +import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Path; + public class AStarAlgorithm extends DijkstraAlgorithm { public AStarAlgorithm(ShortestPathData data) { super(data); } -} + @Override + protected Label LabelOrigin(ShortestPathData data){ + return new LabelStar(data.getOrigin(), null, Double.MAX_VALUE, Point.distance(data.getOrigin().getPoint(),data.getDestination().getPoint())); + } + +} \ No newline at end of file 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 c7e648b..2649473 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 @@ -30,10 +30,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { boolean notfini = true; for (Node node : nodes) { - labels.add(new Label(node)); + labels.add(new Label(node, null, Double.MAX_VALUE)); } - Node origin = data.getOrigin(); + Node origin = LabelOrigin(data).getCurrentNode(); notifyOriginProcessed(origin); labels.get(origin.getId()).setCost(0); tas.insert(labels.get(origin.getId())); @@ -92,4 +92,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { return solution; } + protected Label LabelOrigin(ShortestPathData data){ + return new Label(data.getOrigin(),null,Double.MAX_VALUE); + } + + protected Label createLabel(ShortestPathData data, Arc fatherArc, Label lastLabel){ + return new Label(data.getDestination(),fatherArc,lastLabel.getCost() + data.getCost(fatherArc)); + } + } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java index 8482a2c..1563e6e 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java @@ -9,11 +9,11 @@ private double cost; private Arc fatherArc; - public Label(Node currentNode) + public Label(Node currentNode, Arc fatherArc, double Cost) { this.currentNode = currentNode; - this.fatherArc = null; - this.cost = Double.MAX_VALUE; + this.fatherArc = fatherArc; + this.cost = Cost; this.mark = false; } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java index c78bafb..7fc05ac 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java @@ -5,19 +5,15 @@ import org.insa.graphs.model.*; public class LabelStar extends Label { private double CostEstime; - public LabelStar(Node node){ - super(node); - this.CostEstime = Double.POSITIVE_INFINITY ; + public LabelStar(Node node, Arc fatherArc, double Cost, double CostEstime){ + super(node, fatherArc, Cost); + this.CostEstime = CostEstime; } public double getCostEstime(){ return CostEstime; } - - public void setCostEstime(double CostEstime){ - this.CostEstime = CostEstime; - } - + @Override public double getTotalCost(){ return super.getCost() + getCostEstime(); diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java index 1e8f556..6767eee 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java @@ -141,8 +141,8 @@ public class BinaryHeap> implements PriorityQueue { if (isEmpty() || index < 0 || index >= this.currentSize) { throw new ElementNotFoundException(x); } - E lastItem = this.array.get(--this.currentSize); - this.arraySet(index,lastItem); + E last = this.array.get(--this.currentSize); + this.arraySet(index,last); this.percolateUp(index); this.percolateDown(index); }