factorisation de label, labelstar, dijkstra et astar

This commit is contained in:
Pellerin Tiphaine 2026-05-28 17:37:06 +02:00
parent 07e85edc3c
commit a68e27f24d
10 changed files with 77 additions and 174 deletions

View file

@ -1,14 +1,6 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Collections;
import org.insa.graphs.algorithm.AbstractSolution.Status;
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.Node;
import org.insa.graphs.model.Path;
public class AStarAlgorithm extends DijkstraAlgorithm { public class AStarAlgorithm extends DijkstraAlgorithm {
@ -16,94 +8,9 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
super(data); super(data);
} }
public LabelStar newLabelStar(Node s, ShortestPathData data){ //@Override
return new LabelStar(s, false, Integer.MAX_VALUE, null, s.getPoint().distanceTo(data.getDestination().getPoint())); public LabelStar newLabel(Node s, ShortestPathData dataPath) {
} return new LabelStar(s, false, Integer.MAX_VALUE, null,
s.getPoint().distanceTo(dataPath.getDestination().getPoint()));
@Override
public ShortestPathSolution doRun() {
// retrieve data from the input problem (getInputData() is inherited from the
// parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData();
Graph graph = data.getGraph();
final int nbNodes = graph.size();
// ArrayList<Label> listLabel = new ArrayList<Label>(nbNodes) ;
LabelStar[] listLabel = new LabelStar[nbNodes];
BinaryHeap<Label> tas = new BinaryHeap<Label>();
// variable that will contain the solution of the shortest path problem
ShortestPathSolution solution;
// initialisation
for(Node nod : graph.getNodes()){
listLabel[nod.getId()] = null ;
if (nod.equals(data.getOrigin())){
listLabel[nod.getId()] = new LabelStar(nod, false, 0, null, nod.getPoint().distanceTo(data.getDestination().getPoint()));
tas.insert(listLabel[nod.getId()]);
notifyOriginProcessed(nod);
}
}
Label xl = tas.findMin();
// iterations
while (!tas.isEmpty() && xl.sommetCourant!= data.getDestination()){
xl = tas.findMin();
//System.out.println(xl.getCout());
Node x = xl.getSommetCourant() ;
notifyNodeMarked(x);
xl.marque = true ;
for(Arc a : x.getSuccessors()){
if(data.isAllowed(a)){
if(listLabel[a.getDestination().getId()] == null){
listLabel[a.getDestination().getId()] = newLabelStar(a.getDestination(), data);
}
Node n = a.getDestination();
if (!listLabel[n.getId()].getMarque()){
final var c = listLabel[n.getId()].getCost() ;
final var w = listLabel[x.getId()].getCost() + data.getCost(a) ;
if (c>w){
if(listLabel[n.getId()].cout!=Integer.MAX_VALUE){
tas.remove(listLabel[n.getId()]);
} else {
notifyNodeReached(n);
}
listLabel[n.getId()].cout = w;
tas.insert(listLabel[n.getId()]);
listLabel[n.getId()].pere = a ;
}
}
}
}
tas.remove(xl);
}
// when the algorithm terminates, return the solution that has been found
if (listLabel[data.getDestination().getId()].getCost() == Integer.MAX_VALUE) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
// The destination has been found, notify the observers.
notifyDestinationReached(data.getDestination());
// Create the path from the array of predecessors...
ArrayList<Arc> arcs = new ArrayList<>();
LabelStar nCourant = listLabel[data.getDestination().getId()];
while (nCourant.pere!=null){
arcs.add(nCourant.getPere());
nCourant = listLabel[nCourant.getPere().getOrigin().getId()];
}
// Reverse the path...
Collections.reverse(arcs);
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL,
new Path(graph, arcs));
}
return solution;
} }
} }

View file

@ -16,20 +16,18 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data); super(data);
} }
public Label newLabel(Node s){ public Label newLabel(Node s) {
return new Label(s, false, Integer.MAX_VALUE, null); return new Label(s, false, Double.MAX_VALUE, null);
} }
@Override @Override
public ShortestPathSolution doRun() { public ShortestPathSolution doRun() {
System.out.println("on est dans l'algo");
// retrieve data from the input problem (getInputData() is inherited from the // retrieve data from the input problem (getInputData() is inherited from the
// parent class ShortestPathAlgorithm) // parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData(); final ShortestPathData dataInput = getInputData();
Graph graph = data.getGraph(); Graph graph = dataInput.getGraph();
final int nbNodes = graph.size(); final int nbNodes = graph.size();
// ArrayList<Label> listLabel = new ArrayList<Label>(nbNodes) ;
Label[] listLabel = new Label[nbNodes]; Label[] listLabel = new Label[nbNodes];
BinaryHeap<Label> tas = new BinaryHeap<>(); BinaryHeap<Label> tas = new BinaryHeap<>();
@ -37,9 +35,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ShortestPathSolution solution; ShortestPathSolution solution;
// initialisation // initialisation
for(Node nod : graph.getNodes()){ for (Node nod : graph.getNodes()) {
listLabel[nod.getId()] = null ; listLabel[nod.getId()] = null;
if (nod.equals(data.getOrigin())){ if (nod.equals(dataInput.getOrigin())) {
listLabel[nod.getId()] = new Label(nod, false, 0, null); listLabel[nod.getId()] = new Label(nod, false, 0, null);
tas.insert(listLabel[nod.getId()]); tas.insert(listLabel[nod.getId()]);
notifyOriginProcessed(nod); notifyOriginProcessed(nod);
@ -47,33 +45,31 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
Label xl = tas.findMin(); Label xl = tas.findMin();
// iterations // iterations
while (!tas.isEmpty() && xl.sommetCourant!= data.getDestination()){ while (!tas.isEmpty() && xl.sommetCourant != dataInput.getDestination()) {
xl = tas.findMin(); xl = tas.findMin();
//System.out.println(xl.getCout()); Node x = xl.getSommetCourant();
Node x = xl.getSommetCourant() ;
notifyNodeMarked(x); notifyNodeMarked(x);
xl.marque = true ; xl.marque = true;
for(Arc a : x.getSuccessors()){ for (Arc a : x.getSuccessors()) {
if(data.isAllowed(a)){ if (dataInput.isAllowed(a)) {
if(listLabel[a.getDestination().getId()] == null){
listLabel[a.getDestination().getId()] = newLabel(a.getDestination());}
Node n = a.getDestination(); Node n = a.getDestination();
if (!listLabel[n.getId()].getMarque()){ if (listLabel[n.getId()] == null) {
final var c = listLabel[n.getId()].cout ; listLabel[n.getId()] = newLabel(n);
final var w = listLabel[x.getId()].cout + data.getCost(a) ; }
if (c<w){ if (!listLabel[n.getId()].getMarque()) {
listLabel[n.getId()].cout = c; final var c = listLabel[n.getId()].getTotalCost();
} final var w = listLabel[x.getId()].getTotalCost() + dataInput.getCost(a);
else{ if (c > w) {
if(listLabel[n.getId()].cout!=Integer.MAX_VALUE){ if (listLabel[n.getId()].getCost() != Double.MAX_VALUE) {
tas.remove(listLabel[n.getId()]); tas.remove(listLabel[n.getId()]);
} else { }
else {
notifyNodeReached(n); notifyNodeReached(n);
} }
listLabel[n.getId()].cout = w; listLabel[n.getId()].update(a,w);
tas.insert(listLabel[n.getId()]); tas.insert(listLabel[n.getId()]);
listLabel[n.getId()].pere = a ;
} }
} }
} }
@ -82,19 +78,17 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
// when the algorithm terminates, return the solution that has been found // when the algorithm terminates, return the solution that has been found
if (listLabel[dataInput.getDestination().getId()].getCost() == Double.MAX_VALUE) {
solution = new ShortestPathSolution(dataInput, Status.INFEASIBLE);
if (listLabel[data.getDestination().getId()].cout == Integer.MAX_VALUE) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
} }
else { else {
// The destination has been found, notify the observers. // The destination has been found, notify the observers.
notifyDestinationReached(data.getDestination()); notifyDestinationReached(dataInput.getDestination());
// Create the path from the array of predecessors... // Create the path from the array of predecessors...
ArrayList<Arc> arcs = new ArrayList<>(); ArrayList<Arc> arcs = new ArrayList<>();
Label nCourant = listLabel[data.getDestination().getId()]; Label nCourant = listLabel[dataInput.getDestination().getId()];
while (nCourant.pere!=null){ while (nCourant.pere != null) {
arcs.add(nCourant.getPere()); arcs.add(nCourant.getPere());
nCourant = listLabel[nCourant.getPere().getOrigin().getId()]; nCourant = listLabel[nCourant.getPere().getOrigin().getId()];
} }
@ -102,11 +96,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Collections.reverse(arcs); Collections.reverse(arcs);
// Create the final solution. // Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, solution = new ShortestPathSolution(dataInput, Status.OPTIMAL,
new Path(graph, arcs)); new Path(graph, arcs));
} }
return solution; return solution;
} }

View file

@ -3,44 +3,54 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
public class Label implements Comparable<Label>{ public class Label implements Comparable<Label> {
protected Node sommetCourant ; protected Node sommetCourant;
protected Boolean marque ; protected Boolean marque;
protected double cout ; protected double cout;
protected Arc pere ; protected Arc pere;
public Label(Node s, Boolean m, double c, Arc p){ public Label(Node s, Boolean m, double c, Arc p) {
this.sommetCourant = s; this.sommetCourant = s;
this.cout = c; this.cout = c;
this.marque = m; this.marque = m;
this.pere = p; this.pere = p;
} }
public Node getSommetCourant(){ public Node getSommetCourant() {
return this.sommetCourant ; return this.sommetCourant;
} }
public Boolean getMarque(){ public Boolean getMarque() {
return this.marque ; return this.marque;
} }
public Arc getPere(){ public Arc getPere() {
return this.pere ; return this.pere;
} }
public double getCost(){ public double getCost() {
return this.cout ; return this.cout;
} }
public int compareTo(Label l){ public double getTotalCost() {
if (this.getCost() < l.getCost()){ return this.cout;
}
public void update(Arc p, double c){
this.pere = p;
this.cout = c;
}
@Override
public int compareTo(Label l) {
if (this.getCost() < l.getCost()) {
return -1; return -1;
} }
else if(this.getCost()>l.getCost()){ else if (this.getCost() > l.getCost()) {
return 1; return 1;
} }
else{ else {
return 0; return 0;
} }
} }

View file

@ -3,22 +3,22 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
public class LabelStar extends Label implements Comparable<Label>{ public class LabelStar extends Label implements Comparable<Label> {
protected double coutDest ; protected double coutDest;
public LabelStar(Node s, Boolean m, double c, Arc p, double cd){ public LabelStar(Node s, Boolean m, double c, Arc p, double cd) {
super(s,m,c,p); super(s, m, c, p);
this.coutDest = cd ; this.coutDest = cd;
} }
public double getCoutDest () { public double getCoutDest() {
return this.coutDest ; return this.coutDest;
} }
@Override @Override
public double getCost() { public double getTotalCost() {
return this.coutDest + this.cout ; return this.coutDest + this.cout;
} }
} }

View file

@ -60,7 +60,6 @@ public class Path {
} }
arcs.add(chemins.get(index)); arcs.add(chemins.get(index));
} }
// TODO:
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -108,7 +107,6 @@ public class Path {
} }
arcs.add(chemins.get(index)); arcs.add(chemins.get(index));
} }
// TODO:
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -262,7 +260,6 @@ public class Path {
return false; return false;
} }
} }
// TODO:
return true; return true;
} }
@ -276,7 +273,6 @@ public class Path {
for (Arc a : this.arcs) { for (Arc a : this.arcs) {
l += a.getLength(); l += a.getLength();
} }
// TODO:
return l; return l;
} }
/** /**
@ -291,7 +287,6 @@ public class Path {
for (Arc a : this.arcs) { for (Arc a : this.arcs) {
l += a.getTravelTime(speed); l += a.getTravelTime(speed);
} }
// TODO:
return l; return l;
} }
@ -302,7 +297,6 @@ public class Path {
* @return Minimum travel time to travel this path (in seconds). * @return Minimum travel time to travel this path (in seconds).
*/ */
public double getMinimumTravelTime() { public double getMinimumTravelTime() {
// TODO:
double l = 0; double l = 0;
for (Arc a : this.arcs) { for (Arc a : this.arcs) {
l += a.getMinimumTravelTime(); l += a.getMinimumTravelTime();