A* pas fini

This commit is contained in:
Fu Boyu 2023-04-21 12:16:03 +02:00
parent 39ea623f84
commit c53d3eddce
5 changed files with 46 additions and 16 deletions

View file

@ -1,9 +1,35 @@
package org.insa.graphs.algorithm.shortestpath; 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 class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) { public AStarAlgorithm(ShortestPathData data) {
super(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()));
}
}

View file

@ -30,10 +30,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
boolean notfini = true; boolean notfini = true;
for (Node node : nodes) { 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); notifyOriginProcessed(origin);
labels.get(origin.getId()).setCost(0); labels.get(origin.getId()).setCost(0);
tas.insert(labels.get(origin.getId())); tas.insert(labels.get(origin.getId()));
@ -92,4 +92,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
return solution; 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));
}
} }

View file

@ -9,11 +9,11 @@
private double cost; private double cost;
private Arc fatherArc; private Arc fatherArc;
public Label(Node currentNode) public Label(Node currentNode, Arc fatherArc, double Cost)
{ {
this.currentNode = currentNode; this.currentNode = currentNode;
this.fatherArc = null; this.fatherArc = fatherArc;
this.cost = Double.MAX_VALUE; this.cost = Cost;
this.mark = false; this.mark = false;
} }

View file

@ -5,19 +5,15 @@ import org.insa.graphs.model.*;
public class LabelStar extends Label { public class LabelStar extends Label {
private double CostEstime; private double CostEstime;
public LabelStar(Node node){ public LabelStar(Node node, Arc fatherArc, double Cost, double CostEstime){
super(node); super(node, fatherArc, Cost);
this.CostEstime = Double.POSITIVE_INFINITY ; this.CostEstime = CostEstime;
} }
public double getCostEstime(){ public double getCostEstime(){
return CostEstime; return CostEstime;
} }
public void setCostEstime(double CostEstime){
this.CostEstime = CostEstime;
}
@Override @Override
public double getTotalCost(){ public double getTotalCost(){
return super.getCost() + getCostEstime(); return super.getCost() + getCostEstime();

View file

@ -141,8 +141,8 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
if (isEmpty() || index < 0 || index >= this.currentSize) { if (isEmpty() || index < 0 || index >= this.currentSize) {
throw new ElementNotFoundException(x); throw new ElementNotFoundException(x);
} }
E lastItem = this.array.get(--this.currentSize); E last = this.array.get(--this.currentSize);
this.arraySet(index,lastItem); this.arraySet(index,last);
this.percolateUp(index); this.percolateUp(index);
this.percolateDown(index); this.percolateDown(index);
} }