plus rien ne marche, on a changé l'endroit de création des labels

This commit is contained in:
Pellerin Tiphaine 2026-05-12 17:43:43 +02:00
parent 66e4c4e8f6
commit c6814121a4
16 changed files with 172 additions and 25 deletions

0
.Rhistory Normal file
View file

View file

@ -1,9 +1,107 @@
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.Path;
public class AStarAlgorithm extends DijkstraAlgorithm { public class AStarAlgorithm extends DijkstraAlgorithm {
public AStarAlgorithm(ShortestPathData data) { public AStarAlgorithm(ShortestPathData data) {
super(data); super(data);
} }
@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 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 = null;
// initialisation
for(Node nod : graph.getNodes()){
LabelStar l = new LabelStar(nod, false, Integer.MAX_VALUE, null, nod.getPoint().distanceTo(data.getDestination().getPoint()));
listLabel[nod.getId()] = l ;
if (nod.equals(data.getOrigin())){
tas.insert(l);
l.cout = 0 ;
notifyOriginProcessed(nod);
}
}
LabelStar 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)){
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){
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()].cout == 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

@ -17,7 +17,7 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
} }
@Override @Override
protected ShortestPathSolution doRun() { public ShortestPathSolution doRun() {
// Retrieve the graph. // Retrieve the graph.
ShortestPathData data = getInputData(); ShortestPathData data = getInputData();

View file

@ -3,11 +3,11 @@ package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap; import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph; import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.model.Path; import org.insa.graphs.model.Path;
public class DijkstraAlgorithm extends ShortestPathAlgorithm { public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -16,8 +16,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data); super(data);
} }
public Label newLabel(Node s){
return new Label(s, false, Integer.MAX_VALUE, null);
}
@Override @Override
protected ShortestPathSolution doRun() { public ShortestPathSolution doRun() {
System.out.println("on est dans l'algo"); 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
@ -27,18 +31,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
final int nbNodes = graph.size(); final int nbNodes = graph.size();
// ArrayList<Label> listLabel = new ArrayList<Label>(nbNodes) ; // ArrayList<Label> listLabel = new ArrayList<Label>(nbNodes) ;
Label[] listLabel = new Label[nbNodes]; Label[] listLabel = new Label[nbNodes];
BinaryHeap<Label> tas = new BinaryHeap<Label>(); BinaryHeap<Label> tas = new BinaryHeap<>();
// variable that will contain the solution of the shortest path problem // variable that will contain the solution of the shortest path problem
ShortestPathSolution solution = null; ShortestPathSolution solution;
// initialisation // initialisation
for(Node nod : graph.getNodes()){ for(Node nod : graph.getNodes()){
Label l = new Label(nod, false, Integer.MAX_VALUE, null); listLabel[nod.getId()] = null ;
listLabel[nod.getId()] = l ; if (nod.equals(data.getOrigin())){
if (nod.getId()==data.getOrigin().getId()){ tas.insert(new Label(nod, false, 0, null));
tas.insert(l);
l.cout = 0 ;
notifyOriginProcessed(nod); notifyOriginProcessed(nod);
} }
} }
@ -47,15 +49,18 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// iterations // iterations
while (!tas.isEmpty() && xl.sommetCourant!= data.getDestination()){ while (!tas.isEmpty() && xl.sommetCourant!= data.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(data.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()].getMarque()){
int c = listLabel[n.getId()].cout ; final var c = listLabel[n.getId()].cout ;
int w = listLabel[x.getId()].cout + (int)a.getLength() ; final var w = listLabel[x.getId()].cout + data.getCost(a) ;
if (c<w){ if (c<w){
listLabel[n.getId()].cout = c; listLabel[n.getId()].cout = c;
} }

View file

@ -7,10 +7,10 @@ public class Label implements Comparable<Label>{
protected Node sommetCourant ; protected Node sommetCourant ;
protected Boolean marque ; protected Boolean marque ;
protected int cout ; protected double cout ;
protected Arc pere ; protected Arc pere ;
public Label(Node s, Boolean m, int 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;
@ -25,24 +25,19 @@ public class Label implements Comparable<Label>{
return this.marque ; return this.marque ;
} }
public int getCout(){
return this.cout;
}
public Arc getPere(){ public Arc getPere(){
return this.pere ; return this.pere ;
} }
public int getCost(){ public double getCost(){
int cost = this.cout ; return this.cout ;
return cost ;
} }
public int compareTo(Label l){ public int compareTo(Label l){
if (this.cout < l.cout){ if (this.getCost() < l.getCost()){
return -1; return -1;
} }
else if(this.cout>l.cout){ else if(this.getCost()>l.getCost()){
return 1; return 1;
} }
else{ else{

View file

@ -0,0 +1,24 @@
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>{
protected double coutDest ;
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 ;
}
@Override
public double getCost() {
return this.coutDest + this.cout ;
}
}

View file

@ -9,6 +9,12 @@ import java.io.FileInputStream;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathData;
import org.insa.graphs.algorithm.shortestpath.ShortestPathSolution;
import org.insa.graphs.gui.drawing.Drawing; import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.components.BasicDrawing; import org.insa.graphs.gui.drawing.components.BasicDrawing;
import org.insa.graphs.model.Graph; import org.insa.graphs.model.Graph;
@ -43,6 +49,11 @@ public class Launch {
return basicDrawing; return basicDrawing;
} }
private static ShortestPathSolution paths ;
private static ShortestPathSolution paths2 ;
private static DijkstraAlgorithm dijkstra ;
private static BellmanFordAlgorithm bellman ;
private static ShortestPathData data ;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// visit these directory to see the list of available files on commetud. // visit these directory to see the list of available files on commetud.
@ -78,6 +89,22 @@ public class Launch {
// TODO: draw the path on the drawing // TODO: draw the path on the drawing
drawing.drawPath(path); drawing.drawPath(path);
data = new ShortestPathData(graph, path.getOrigin(),path.getDestination(), ArcInspectorFactory.getAllFilters().get(0)) ;
dijkstra = new DijkstraAlgorithm(data) ;
bellman = new BellmanFordAlgorithm(data);
paths = dijkstra.doRun() ;
paths2 = bellman.doRun() ;
if (paths.getPath().isValid()){
System.out.println("c'est valide");
}
if (paths.getPath().getLength()==path.getLength()) {
System.out.println("même longueur ");
}
if (paths.getPath().getLength()==paths2.getPath().getLength()) {
System.out.println("Dijkstra donne le même résultat que Bellman");
}
} }
} }

View file

@ -270,7 +270,6 @@ public class Path {
* Compute the length of this path (in meters). * Compute the length of this path (in meters).
* *
* @return Total length of the path (in meters). * @return Total length of the path (in meters).
* @deprecated Need to be implemented.
*/ */
public float getLength() { public float getLength() {
float l = 0; float l = 0;
@ -280,7 +279,6 @@ public class Path {
// TODO: // TODO:
return l; return l;
} }
/** /**
* Compute the time required to travel this path if moving at the given speed. * Compute the time required to travel this path if moving at the given speed.
* *