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;
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.Path;
public class AStarAlgorithm extends DijkstraAlgorithm {
@ -16,94 +8,9 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
super(data);
}
public LabelStar newLabelStar(Node s, ShortestPathData data){
return new LabelStar(s, false, Integer.MAX_VALUE, null, s.getPoint().distanceTo(data.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;
//@Override
public LabelStar newLabel(Node s, ShortestPathData dataPath) {
return new LabelStar(s, false, Integer.MAX_VALUE, null,
s.getPoint().distanceTo(dataPath.getDestination().getPoint()));
}
}

View file

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

View file

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