Compare commits

...

2 commits

Author SHA1 Message Date
0e576e534b From parentnode to parentarc 2024-05-21 19:08:11 +02:00
bed0f37cfa Before big modif 2024-05-21 19:08:11 +02:00
3 changed files with 77 additions and 38 deletions

View file

@ -66,7 +66,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
double arc_cost = 0;
for (Arc successorArc : x.getNode().getSuccessors()) {
Label successor = labels.get(successorArc.getDestination().getId());
arc_cost = Double.MAX_VALUE;
if (!successor.isMarked() && data.isAllowed(successorArc)) {
// This loop serves to get the length of the arc as
// we know its origin and destination
@ -76,7 +76,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// data.getcost(arc) returns a cost considering the mode chosen:
// TIME or LENGTH
// Similar to using getLength / getMinimumTravelTime
arc_cost = data.getCost(arc);
arc_cost = Math.min(data.getCost(arc), arc_cost);
}
}
@ -84,7 +84,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
if (successor.getCost() > possible_path_cost) {
// Mise à jour du label
successor.setPathCost(possible_path_cost);
successor.setParentNode(x.getNode());
successor.setParentArc(successorArc);
// Si le noeud n'a pas déjà été rajouté au tas, on le rajoute
// isReached permet de vérifier en complexité O(1)
// C'est un léger coût en mémoire pour un gain en vitesse
@ -101,7 +101,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
}
if (labels.get(data.getDestination().getId()).getParentNode() == null) {
if (labels.get(data.getDestination().getId()).getParentArc() == null) {
this.pathCost = 0;
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
@ -109,32 +109,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// Create the path ...
ArrayList<Arc> arcs_path = new ArrayList<>();
Arc arc = labels.get(data.getDestination().getId()).getParentArc();
// We will find the path using the parent nodes, from the destination to the
// origin
Label current_label = labels.get(data.getDestination().getId());
Label parent_label = current_label;
while(current_label != null && current_label.getNode().getId() != data.getOrigin().getId())
{
// Find the label matching the parent node
parent_label = labels.get(current_label.getParentNode().getId());
// Knowing the parent node, get the arc between the parent and
// current node and add it to the path
if (parent_label != null) {
double minCost = Double.MAX_VALUE;
Arc minCostArc = null;
for (Arc arc : parent_label.getNode().getSuccessors()) {
if (arc.getDestination().getId() == current_label.getNode().getId()
&& data.isAllowed(arc)
&& data.getCost(arc) < minCost) {
minCost = data.getCost(arc);
minCostArc = arc;
}
}
arcs_path.add(minCostArc);
}
current_label = parent_label;
while (arc != null && arc.getDestination().getId() != data.getOrigin().getId()) {
arcs_path.add(arc);
arc = labels.get(arc.getOrigin().getId()).getParentArc();
}
notifyDestinationReached(data.getDestination());

View file

@ -1,6 +1,7 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
public class Label implements Comparable<Label> {
@ -8,7 +9,7 @@ public class Label implements Comparable<Label> {
boolean marked;
boolean reached;
double pathCost;
Node parentNode;
Arc parentArc;
public Label(Node node) {
@ -16,7 +17,7 @@ public class Label implements Comparable<Label> {
this.marked = false;
this.marked = false;
this.pathCost = Double.MAX_VALUE;
this.parentNode = null;
this.parentArc = null;
}
public Node getNode() { return this.node; }
@ -26,8 +27,8 @@ public class Label implements Comparable<Label> {
public void markReached() { this.reached = true; }
public double getPathCost() { return this.pathCost; }
public void setPathCost(double newCost) { this.pathCost = newCost; }
public Node getParentNode() { return this.parentNode; }
public void setParentNode(Node parentNode) { this.parentNode = parentNode; }
public Arc getParentArc() { return this.parentArc; }
public void setParentArc(Arc parentArc) { this.parentArc = parentArc; }
public double getCost() {
// function will be modified later

View file

@ -9,9 +9,11 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
@ -89,10 +91,32 @@ public abstract class ShortestPathAlgorithmTest {
final BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
final ShortestPathSolution bell_path = bellman.doRun();
assert(path.getPath().isValid());
assert(path.isFeasible());
assert(Math.abs(algo.getCostPath() - path.getPath().getLength()) < 1.0);
assert(Math.abs(path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
if (bell_path.getPath() != null) {
if (bell_path.getPath().isValid()) {
assert(path.getPath().isValid());
assert(path.isFeasible());
if (data.getMode() == Mode.LENGTH) {
if (Math.abs( algo.getCostPath() - (double) path.getPath().getLength()) !=0) {
System.out.println(data.getOrigin().getId());
System.out.println(data.getDestination().getId());
System.out.println(algo.getCostPath() + " " + path.getPath().getLength());
}
assert(Math.abs((float) algo.getCostPath() - path.getPath().getLength()) < 1);
assert(Math.abs(path.getPath().getLength() - bell_path.getPath().getLength()) < 1);
}
else {
assert(Math.abs(algo.getCostPath() - path.getPath().getMinimumTravelTime()) == 0);
assert(Math.abs(path.getPath().getMinimumTravelTime() - bell_path.getPath().getMinimumTravelTime()) == 0);
}
}
else {
assert(!path.getPath().isValid());
assert(!path.isFeasible());
}
}
else {
assert(!path.isFeasible());
}
}
/*
@ -103,13 +127,13 @@ public abstract class ShortestPathAlgorithmTest {
final ShortestPathAlgorithm algo = initAlgo(data);
final ShortestPathSolution path = algo.doRun();
assert(!path.isFeasible());
assert(path.getPath() == null);
}
@Test
/*
/*
* Map: carre.mapgr
* Chemin: 19 --> 4
* Tous chemins permis
@ -119,6 +143,7 @@ public abstract class ShortestPathAlgorithmTest {
public void chemin_court_CARRE_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs
Graph myGraph = graph.get(0);
Node origin = myGraph.get(19);
Node destination = myGraph.get(4);
@ -401,4 +426,36 @@ public abstract class ShortestPathAlgorithmTest {
// On peut aussi supposer que le nombre d'arcs empruntés est très grand.
assert(path.getPath().getArcs().size() > 1000);
}
@Test
/*
* Chemin long sur la carte de Toulouse.
* Même si Bellman est de plus long à faire long à faire, ce test prend moins
* de 3s, on estime que ce n'est pas trop et on va utiliser Bellman.
* Par contre, dans le test sur la région midi_pyrenees qui arrive après, on va
* être obligé de trouver une autre solution.
* Origine: 16644
* Destination: 39229
* Mode: LENGTH
* PATH UTILISE : ../Paths/custom_paths/long_path_tls.path
*/
public void cheminsToulouse() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0);
Graph myGraph = graph.get(2);
Node origin = myGraph.get(16644);
Node destination = myGraph.get(39229);
Random rand = new Random();
int size_graph = myGraph.getNodes().size();
for (int i = 0 ; i < 50 ; i++) {
origin = myGraph.get(Math.abs(rand.nextInt()) % size_graph);
destination = myGraph.get(Math.abs(rand.nextInt()) % size_graph);
assertBellmanHasSameResult(myGraph, origin, destination, arcInspector);
}
}
}