plus rien ne marche, on a changé l'endroit de création des labels
This commit is contained in:
parent
66e4c4e8f6
commit
c6814121a4
16 changed files with 172 additions and 25 deletions
0
.Rhistory
Normal file
0
.Rhistory
Normal file
|
|
@ -1,9 +1,107 @@
|
|||
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 AStarAlgorithm(ShortestPathData 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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
public ShortestPathSolution doRun() {
|
||||
|
||||
// Retrieve the graph.
|
||||
ShortestPathData data = getInputData();
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ 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.algorithm.AbstractSolution.Status;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
|
@ -16,8 +16,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public Label newLabel(Node s){
|
||||
return new Label(s, false, Integer.MAX_VALUE, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
public ShortestPathSolution doRun() {
|
||||
|
||||
System.out.println("on est dans l'algo");
|
||||
// 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();
|
||||
// ArrayList<Label> listLabel = new ArrayList<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
|
||||
ShortestPathSolution solution = null;
|
||||
ShortestPathSolution solution;
|
||||
|
||||
// initialisation
|
||||
for(Node nod : graph.getNodes()){
|
||||
Label l = new Label(nod, false, Integer.MAX_VALUE, null);
|
||||
listLabel[nod.getId()] = l ;
|
||||
if (nod.getId()==data.getOrigin().getId()){
|
||||
tas.insert(l);
|
||||
l.cout = 0 ;
|
||||
listLabel[nod.getId()] = null ;
|
||||
if (nod.equals(data.getOrigin())){
|
||||
tas.insert(new Label(nod, false, 0, null));
|
||||
notifyOriginProcessed(nod);
|
||||
}
|
||||
}
|
||||
|
|
@ -47,15 +49,18 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// 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()] = newLabel(a.getDestination());}
|
||||
Node n = a.getDestination();
|
||||
if (!listLabel[n.getId()].getMarque()){
|
||||
int c = listLabel[n.getId()].cout ;
|
||||
int w = listLabel[x.getId()].cout + (int)a.getLength() ;
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ public class Label implements Comparable<Label>{
|
|||
|
||||
protected Node sommetCourant ;
|
||||
protected Boolean marque ;
|
||||
protected int cout ;
|
||||
protected double cout ;
|
||||
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.cout = c;
|
||||
this.marque = m;
|
||||
|
|
@ -25,24 +25,19 @@ public class Label implements Comparable<Label>{
|
|||
return this.marque ;
|
||||
}
|
||||
|
||||
public int getCout(){
|
||||
return this.cout;
|
||||
}
|
||||
|
||||
public Arc getPere(){
|
||||
return this.pere ;
|
||||
}
|
||||
|
||||
public int getCost(){
|
||||
int cost = this.cout ;
|
||||
return cost ;
|
||||
public double getCost(){
|
||||
return this.cout ;
|
||||
}
|
||||
|
||||
public int compareTo(Label l){
|
||||
if (this.cout < l.cout){
|
||||
if (this.getCost() < l.getCost()){
|
||||
return -1;
|
||||
}
|
||||
else if(this.cout>l.cout){
|
||||
else if(this.getCost()>l.getCost()){
|
||||
return 1;
|
||||
}
|
||||
else{
|
||||
|
|
|
|||
|
|
@ -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 ;
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -9,6 +9,12 @@ import java.io.FileInputStream;
|
|||
import javax.swing.JFrame;
|
||||
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.components.BasicDrawing;
|
||||
import org.insa.graphs.model.Graph;
|
||||
|
|
@ -43,6 +49,11 @@ public class Launch {
|
|||
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 {
|
||||
|
||||
// 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
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
|
|
@ -270,7 +270,6 @@ public class Path {
|
|||
* Compute the length of this path (in meters).
|
||||
*
|
||||
* @return Total length of the path (in meters).
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public float getLength() {
|
||||
float l = 0;
|
||||
|
|
@ -280,7 +279,6 @@ public class Path {
|
|||
// TODO:
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the time required to travel this path if moving at the given speed.
|
||||
*
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in a new issue