dijkstra tests

This commit is contained in:
Bensouda Idriss 2023-05-18 17:57:49 +02:00
parent 0d6cf379f4
commit 85f52cdb6a
9 changed files with 23 additions and 10 deletions

View file

@ -22,7 +22,7 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
ShortestPathSolution solution = null; ShortestPathSolution solution = null;
// TODO: // TODO:
ArrayList<Label> List_Label = new ArrayList<Label>(data.getGraph().size()); //Liste de labels ArrayList<LabelStar> List_Label = new ArrayList<LabelStar>(data.getGraph().size()); //Liste de labels
BinaryHeap<Label> Tas = new BinaryHeap<Label>(); BinaryHeap<Label> Tas = new BinaryHeap<Label>();
ArrayList<Arc> Arcs = new ArrayList<Arc>(); ArrayList<Arc> Arcs = new ArrayList<Arc>();
@ -46,22 +46,24 @@ public class AStarAlgorithm extends DijkstraAlgorithm {
x = Tas.findMin(); x = Tas.findMin();
x.setMarque(true); x.setMarque(true);
Tas.deleteMin(); Tas.deleteMin();
System.out.println("test");
for(Arc suivant : x.getSommet().getSuccessors()){ for(Arc suivant : x.getSommet().getSuccessors()){
// Small test to check allowed roads... // Small test to check allowed roads...
if (!data.isAllowed(suivant)) { if (!data.isAllowed(suivant)) {
continue; continue;
} }
System.out.println("test2");
Label l=List_Label.get(suivant.getDestination().getId()); LabelStar l=List_Label.get(suivant.getDestination().getId());
if(!l.isMarque()){ if(!l.isMarque()){
Boolean changé = false; Boolean changé = false;
if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){ if (x.getTotalCost()+data.getCost(suivant) < l.getTotalCost()){
changé = true; changé = true;
} }
System.out.println("test3");
if(changé){ if(changé){
System.out.println("test4");
if (l.getTotalCost() != Double.MAX_VALUE){ if (l.getTotalCost() != Double.MAX_VALUE){
Tas.remove(l); Tas.remove(l);
} }
l.setCost(x.getTotalCost()+data.getCost(suivant)); l.setCost(x.getTotalCost()+data.getCost(suivant));

View file

@ -43,7 +43,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
Tas.insert(List_Label.get(data.getOrigin().getId())); Tas.insert(List_Label.get(data.getOrigin().getId()));
Label x; Label x;
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){ while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
x = Tas.findMin(); x = Tas.findMin();
x.setMarque(true); x.setMarque(true);
@ -86,8 +85,18 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Collections.reverse(Arcs); Collections.reverse(Arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs)); solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
return solution; ArrayList<Node> solutionNodes = new ArrayList<Node>();
for (Arc a : Arcs){
solutionNodes.add(a.getOrigin());
}
solutionNodes.add(data.getDestination());
Path p = Path.createShortestPathFromNodes(data.getGraph(), solutionNodes);
Path p2 = Path.createFastestPathFromNodes(data.getGraph(), solutionNodes);
System.out.println("shortest path : " + p.getLength());
if (p.getLength()-solution.getPath().getLength() < 1.00){
System.out.println("le chemin Dijkstra est le shortest");
} }
return solution;
}
} }

View file

@ -11,6 +11,7 @@ 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.shortestpath.ShortestPathAlgorithm;
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;
@ -70,7 +71,7 @@ public class Launch {
// TODO: Read the path. // TODO: Read the path.
final Path path = pathReader.readPath(graph); final Path path = pathReader.readPath(graph);
System.out.println();
// TODO: Draw the path. // TODO: Draw the path.
drawing.drawPath(path,Color.green); drawing.drawPath(path,Color.green);

View file

@ -59,6 +59,7 @@ public class Path {
} }
arcs.add(a); arcs.add(a);
} }
System.out.println(new Path(graph, arcs));
return new Path(graph, arcs); return new Path(graph, arcs);
} }