Compare commits
2 commits
7545364b39
...
0e576e534b
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e576e534b | |||
| bed0f37cfa |
3 changed files with 77 additions and 38 deletions
|
|
@ -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 = 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) {
|
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.setParentNode(x.getNode());
|
successor.setParentArc(successorArc);
|
||||||
// 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()).getParentNode() == null) {
|
if (labels.get(data.getDestination().getId()).getParentArc() == null) {
|
||||||
this.pathCost = 0;
|
this.pathCost = 0;
|
||||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||||
}
|
}
|
||||||
|
|
@ -109,32 +109,13 @@ 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
|
||||||
Label current_label = labels.get(data.getDestination().getId());
|
while (arc != null && arc.getDestination().getId() != data.getOrigin().getId()) {
|
||||||
Label parent_label = current_label;
|
arcs_path.add(arc);
|
||||||
|
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());
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
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> {
|
||||||
|
|
@ -8,7 +9,7 @@ public class Label implements Comparable<Label> {
|
||||||
boolean marked;
|
boolean marked;
|
||||||
boolean reached;
|
boolean reached;
|
||||||
double pathCost;
|
double pathCost;
|
||||||
Node parentNode;
|
Arc parentArc;
|
||||||
|
|
||||||
|
|
||||||
public Label(Node node) {
|
public Label(Node node) {
|
||||||
|
|
@ -16,7 +17,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.parentNode = null;
|
this.parentArc = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getNode() { return this.node; }
|
public Node getNode() { return this.node; }
|
||||||
|
|
@ -26,8 +27,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 Node getParentNode() { return this.parentNode; }
|
public Arc getParentArc() { return this.parentArc; }
|
||||||
public void setParentNode(Node parentNode) { this.parentNode = parentNode; }
|
public void setParentArc(Arc parentArc) { this.parentArc = parentArc; }
|
||||||
|
|
||||||
public double getCost() {
|
public double getCost() {
|
||||||
// function will be modified later
|
// function will be modified later
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,11 @@ 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;
|
||||||
|
|
@ -89,10 +91,32 @@ 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) {
|
||||||
|
if (bell_path.getPath().isValid()) {
|
||||||
assert(path.getPath().isValid());
|
assert(path.getPath().isValid());
|
||||||
assert(path.isFeasible());
|
assert(path.isFeasible());
|
||||||
assert(Math.abs(algo.getCostPath() - path.getPath().getLength()) < 1.0);
|
if (data.getMode() == Mode.LENGTH) {
|
||||||
assert(Math.abs(path.getPath().getLength() - bell_path.getPath().getLength()) < 1.0);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -119,6 +143,7 @@ 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);
|
||||||
|
|
||||||
|
|
@ -401,4 +426,36 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue