Compare commits

..

No commits in common. "0e576e534b1ed7c110310d42b111acebc9e3da43" and "7545364b39046a5be5df24b49eee952c24ae790f" have entirely different histories.

3 changed files with 38 additions and 77 deletions

View file

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

View file

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

View file

@ -9,11 +9,9 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random;
import org.insa.graphs.algorithm.ArcInspector; import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory; import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
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.model.Path; import org.insa.graphs.model.Path;
@ -91,32 +89,10 @@ public abstract class ShortestPathAlgorithmTest {
final BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data); final BellmanFordAlgorithm bellman = new BellmanFordAlgorithm(data);
final ShortestPathSolution bell_path = bellman.doRun(); final ShortestPathSolution bell_path = bellman.doRun();
if (bell_path.getPath() != null) { assert(path.getPath().isValid());
if (bell_path.getPath().isValid()) { assert(path.isFeasible());
assert(path.getPath().isValid()); assert(Math.abs(algo.getCostPath() - path.getPath().getLength()) < 1.0);
assert(path.isFeasible()); assert(Math.abs(path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
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());
}
} }
/* /*
@ -127,13 +103,13 @@ public abstract class ShortestPathAlgorithmTest {
final ShortestPathAlgorithm algo = initAlgo(data); final ShortestPathAlgorithm algo = initAlgo(data);
final ShortestPathSolution path = algo.doRun(); final ShortestPathSolution path = algo.doRun();
assert(!path.isFeasible()); assert(!path.isFeasible());
assert(path.getPath() == null); assert(path.getPath() == null);
} }
@Test @Test
/* /*
* Map: carre.mapgr * Map: carre.mapgr
* Chemin: 19 --> 4 * Chemin: 19 --> 4
* Tous chemins permis * Tous chemins permis
@ -143,7 +119,6 @@ public abstract class ShortestPathAlgorithmTest {
public void chemin_court_CARRE_length() { public void chemin_court_CARRE_length() {
ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs ArcInspector arcInspector = ArcInspectorFactory.getAllFilters().get(0); // all arcs
Graph myGraph = graph.get(0); Graph myGraph = graph.get(0);
Node origin = myGraph.get(19); Node origin = myGraph.get(19);
Node destination = myGraph.get(4); Node destination = myGraph.get(4);
@ -426,36 +401,4 @@ public abstract class ShortestPathAlgorithmTest {
// On peut aussi supposer que le nombre d'arcs empruntés est très grand. // On peut aussi supposer que le nombre d'arcs empruntés est très grand.
assert(path.getPath().getArcs().size() > 1000); 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);
}
}
} }