Astar bientot
Esse commit está contido em:
pai
65f19a9876
commit
0d6cf379f4
6 arquivos alterados com 112 adições e 9 exclusões
|
@ -1,9 +1,89 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||||
|
|
||||||
|
import java.lang.invoke.LambdaConversionException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Collections;
|
||||||
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
|
import org.insa.graphs.model.Arc;
|
||||||
|
import org.insa.graphs.model.Node;
|
||||||
|
import org.insa.graphs.model.Path;
|
||||||
|
import org.insa.graphs.model.Point;
|
||||||
public class AStarAlgorithm extends DijkstraAlgorithm {
|
public class AStarAlgorithm extends DijkstraAlgorithm {
|
||||||
|
|
||||||
public AStarAlgorithm(ShortestPathData data) {
|
public AStarAlgorithm(ShortestPathData data) {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
protected ShortestPathSolution doRun() {
|
||||||
|
final ShortestPathData data = getInputData();
|
||||||
|
ShortestPathSolution solution = null;
|
||||||
|
// TODO:
|
||||||
|
|
||||||
|
ArrayList<Label> List_Label = new ArrayList<Label>(data.getGraph().size()); //Liste de labels
|
||||||
|
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
|
||||||
|
ArrayList<Arc> Arcs = new ArrayList<Arc>();
|
||||||
|
|
||||||
|
/* Initialise nos label */
|
||||||
|
for (Node x: data.getGraph().getNodes())
|
||||||
|
{
|
||||||
|
if(x != data.getOrigin()){
|
||||||
|
LabelStar a= new LabelStar(x,Double.MAX_VALUE,null,Point.distance(x.getPoint(),data.getDestination().getPoint()));
|
||||||
|
List_Label.add(x.getId(), a);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
LabelStar a = new LabelStar(data.getOrigin(), 0, null,Point.distance(x.getPoint(),data.getDestination().getPoint()));
|
||||||
|
List_Label.add(data.getOrigin().getId(), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Tas.insert(List_Label.get(data.getOrigin().getId()));
|
||||||
|
Label x;
|
||||||
|
|
||||||
|
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
||||||
|
x = Tas.findMin();
|
||||||
|
x.setMarque(true);
|
||||||
|
Tas.deleteMin();
|
||||||
|
|
||||||
|
for(Arc suivant : x.getSommet().getSuccessors()){
|
||||||
|
// Small test to check allowed roads...
|
||||||
|
if (!data.isAllowed(suivant)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Label l=List_Label.get(suivant.getDestination().getId());
|
||||||
|
if(!l.isMarque()){
|
||||||
|
Boolean changé = false;
|
||||||
|
if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){
|
||||||
|
changé = true;
|
||||||
|
}
|
||||||
|
if(changé){
|
||||||
|
if (l.getTotalCost() != Double.MAX_VALUE){
|
||||||
|
|
||||||
|
Tas.remove(l);
|
||||||
|
}
|
||||||
|
l.setCost(x.getTotalCost()+data.getCost(suivant));
|
||||||
|
Tas.insert(l);
|
||||||
|
l.setParent(suivant);
|
||||||
|
|
||||||
|
notifyNodeReached(suivant.getDestination());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Label dest =List_Label.get(data.getDestination().getId());
|
||||||
|
|
||||||
|
while (dest.getParent() != null){
|
||||||
|
Arcs.add(dest.getParent());
|
||||||
|
dest = List_Label.get(dest.getParent().getOrigin().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.reverse(Arcs);
|
||||||
|
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
|
||||||
|
return solution;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,15 +58,15 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Label l=List_Label.get(suivant.getDestination().getId());
|
Label l=List_Label.get(suivant.getDestination().getId());
|
||||||
if(!l.isMarque()){
|
if(!l.isMarque()){
|
||||||
Boolean changé = false;
|
Boolean changé = false;
|
||||||
if (x.getCost()+data.getCost(suivant) < l.getCost()){
|
if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){
|
||||||
changé = true;
|
changé = true;
|
||||||
}
|
}
|
||||||
if(changé){
|
if(changé){
|
||||||
if (l.getCost() != Double.MAX_VALUE){
|
if (l.getTotalCost() != Double.MAX_VALUE){
|
||||||
|
|
||||||
Tas.remove(l);
|
Tas.remove(l);
|
||||||
}
|
}
|
||||||
l.setCost(x.getCost()+data.getCost(suivant));
|
l.setCost(x.getTotalCost()+data.getCost(suivant));
|
||||||
Tas.insert(l);
|
Tas.insert(l);
|
||||||
l.setParent(suivant);
|
l.setParent(suivant);
|
||||||
|
|
||||||
|
|
|
@ -22,9 +22,9 @@ public class Label implements Comparable<Label> {
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Label a) {
|
public int compareTo(Label a) {
|
||||||
int result;
|
int result;
|
||||||
if (a.getCost() < cost){
|
if (a.getTotalCost() < cost){
|
||||||
result = 1;
|
result = 1;
|
||||||
}else if (a.getCost()>cost){
|
}else if (a.getTotalCost()>cost){
|
||||||
result = -1;
|
result = -1;
|
||||||
}else {
|
}else {
|
||||||
result = 0;
|
result = 0;
|
||||||
|
@ -38,7 +38,7 @@ public class Label implements Comparable<Label> {
|
||||||
public boolean isMarque() {
|
public boolean isMarque() {
|
||||||
return marque;
|
return marque;
|
||||||
}
|
}
|
||||||
public double getCost() {
|
public double getTotalCost() {
|
||||||
return cost;
|
return cost;
|
||||||
}
|
}
|
||||||
public Arc getParent() {
|
public Arc getParent() {
|
||||||
|
|
|
@ -5,11 +5,11 @@ import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
|
|
||||||
public class LabelStar extends Label {
|
public class LabelStar extends Label{
|
||||||
private double cost_Destination;
|
private double cost_Destination;
|
||||||
private double cost_Origin;
|
private double cost_Origin;
|
||||||
|
|
||||||
public LabelStar(Node sommet, double cost_Origin,double cost_Destination, Arc parent)
|
public LabelStar(Node sommet, double cost_Origin, Arc parent,double cost_Destination)
|
||||||
{
|
{
|
||||||
super(sommet, cost_Origin, parent);
|
super(sommet, cost_Origin, parent);
|
||||||
this.cost_Origin = cost_Origin;
|
this.cost_Origin = cost_Origin;
|
||||||
|
@ -17,10 +17,33 @@ public class LabelStar extends Label {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Label a) {
|
||||||
|
return super.compareTo(a);
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public double getTotalCost() {
|
public double getTotalCost() {
|
||||||
return cost_Destination+cost_Origin;
|
return cost_Destination+cost_Origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public double getCost_Destination() {
|
||||||
|
return cost_Destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setCost_Destination(double cost_Destination) {
|
||||||
|
this.cost_Destination = cost_Destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public double getCost_Origin() {
|
||||||
|
return cost_Origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setCost_Origin(double cost_Origin) {
|
||||||
|
this.cost_Origin = cost_Origin;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Arquivo binário não exibido.
Arquivo binário não exibido.
Carregando…
Referência em uma nova issue