Various improvements
This commit is contained in:
parent
539ea33752
commit
25e08fc7f0
7 changed files with 37 additions and 41 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -18,3 +18,4 @@ doc
|
||||||
*.mapgr
|
*.mapgr
|
||||||
*.path
|
*.path
|
||||||
*.tgz
|
*.tgz
|
||||||
|
/rapport/
|
||||||
|
|
|
@ -57,9 +57,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Node destination = data.getDestination();
|
Node destination = data.getDestination();
|
||||||
Node currentNode = null;
|
Node currentNode = null;
|
||||||
|
|
||||||
|
/*
|
||||||
// Debug / Tests
|
// Debug / Tests
|
||||||
double previousCost = 0;
|
double previousCost = 0;
|
||||||
int iterationCounter = 0;
|
int iterationCounter = 0;
|
||||||
|
*/
|
||||||
|
|
||||||
while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
|
while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
|
||||||
// Get the node with the minimum cost
|
// Get the node with the minimum cost
|
||||||
|
@ -69,11 +71,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Debug / Tests
|
// Debug / Tests
|
||||||
assert(((BinaryHeap<Label>) heap).isValid());
|
assert (((BinaryHeap<Label>) heap).isValid());
|
||||||
assert(currentLabel.getTotalCost() >= previousCost);
|
assert (currentLabel.getTotalCost() >= previousCost);
|
||||||
previousCost = currentLabel.getCost();
|
previousCost = currentLabel.getCost();
|
||||||
*/
|
|
||||||
iterationCounter++;
|
iterationCounter++;
|
||||||
|
*/
|
||||||
|
|
||||||
for (Arc arc : currentNode.getSuccessors()) {
|
for (Arc arc : currentNode.getSuccessors()) {
|
||||||
Node dest = arc.getDestination();
|
Node dest = arc.getDestination();
|
||||||
|
@ -85,14 +87,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
double newCost = currentLabel.getCost() + arcCost;
|
double newCost = currentLabel.getCost() + arcCost;
|
||||||
double oldCost = destLabel.getCost();
|
double oldCost = destLabel.getCost();
|
||||||
if (newCost < oldCost) {
|
if (newCost < oldCost) {
|
||||||
try {
|
if (Double.isFinite(oldCost)) { // if already visited, remove from the heap before updating
|
||||||
heap.remove(destLabel);
|
heap.remove(destLabel);
|
||||||
} catch (ElementNotFoundException ignored) {
|
} else { // else, notify observers
|
||||||
|
notifyNodeReached(dest);
|
||||||
}
|
}
|
||||||
destLabel.updateCost(newCost, arc);
|
destLabel.updateCost(newCost, arc);
|
||||||
heap.insert(destLabel);
|
heap.insert(destLabel);
|
||||||
if (Double.isInfinite(oldCost))
|
|
||||||
notifyNodeReached(dest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,7 +116,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
currentLabel = labels.get(arc.getOrigin().getId());
|
currentLabel = labels.get(arc.getOrigin().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
|
// System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
|
||||||
|
|
||||||
Collections.reverse(path);
|
Collections.reverse(path);
|
||||||
|
|
||||||
|
|
|
@ -65,10 +65,11 @@ public class Label implements Comparable<Label> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Label o) {
|
public int compareTo(Label o) {
|
||||||
int comp = Double.compare(getTotalCost(), o.getTotalCost());
|
int cmp = Double.compare(this.getTotalCost(), o.getTotalCost());
|
||||||
if (comp == 0 && this.getClass() == LabelStar.class && o.getClass() == LabelStar.class)
|
if (cmp == 0) {
|
||||||
return Double.compare(((LabelStar)this).getEstimatedCost(), ((LabelStar)o).getEstimatedCost());
|
cmp = Double.compare(o.getCost(), this.getCost());
|
||||||
else
|
}
|
||||||
return comp;
|
return cmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.Point;
|
|
||||||
|
|
||||||
public class LabelStar extends Label {
|
public class LabelStar extends Label {
|
||||||
|
|
||||||
|
|
|
@ -66,16 +66,16 @@ public abstract class ShortestPathTest {
|
||||||
assertTrue(sol.isFeasible());
|
assertTrue(sol.isFeasible());
|
||||||
Path path = sol.getPath();
|
Path path = sol.getPath();
|
||||||
assertTrue(path.isValid());
|
assertTrue(path.isValid());
|
||||||
// check the optimality locally
|
// skip these tests for the square map where there may be several arcs with the same cost
|
||||||
if (i == 0)
|
if (i != 0) {
|
||||||
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
|
// check the optimality locally
|
||||||
if (i == 1)
|
if (arcInspector == arcInspectors[0])
|
||||||
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
|
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
|
||||||
// Check result against Bellman Ford algorithm (except for the square map
|
if (arcInspector == arcInspectors[1])
|
||||||
// where there may be several paths with the same cost)
|
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
|
||||||
if (i != 0)
|
// Check result against Bellman Ford algorithm
|
||||||
assertEquals(sol.getPath(), oracle.getPath());
|
assertEquals(sol.getPath(), oracle.getPath());
|
||||||
System.out.println();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<excludeFolder url="file://$MODULE_DIR$/.idea" />
|
<excludeFolder url="file://$MODULE_DIR$/.idea" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/maps" />
|
<excludeFolder url="file://$MODULE_DIR$/maps" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/rapport" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
|
|
@ -24,7 +24,6 @@ public class Launch {
|
||||||
* Create a new Drawing inside a JFrame an return it.
|
* Create a new Drawing inside a JFrame an return it.
|
||||||
*
|
*
|
||||||
* @return The created drawing.
|
* @return The created drawing.
|
||||||
*
|
|
||||||
* @throws Exception if something wrong happens when creating the graph.
|
* @throws Exception if something wrong happens when creating the graph.
|
||||||
*/
|
*/
|
||||||
public static Drawing createDrawing() throws Exception {
|
public static Drawing createDrawing() throws Exception {
|
||||||
|
@ -45,28 +44,22 @@ public class Launch {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
// Visit these directory to see the list of available files on Commetud.
|
|
||||||
final String mapName = "maps/insa.mapgr";
|
final String mapName = "maps/insa.mapgr";
|
||||||
final String pathName = "maps/path_fr31insa_rangueil_insa.path";
|
final String pathName = "maps/path_fr31insa_rangueil_insa.path";
|
||||||
|
|
||||||
// Create a graph reader.
|
// Create a graph reader.
|
||||||
final GraphReader reader = new BinaryGraphReader(
|
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
|
||||||
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
|
final PathReader pathReader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))))) {
|
||||||
|
|
||||||
final Graph graph = reader.read();
|
final Graph graph = reader.read();
|
||||||
|
|
||||||
// Create the drawing:
|
// Create the drawing:
|
||||||
final Drawing drawing = createDrawing();
|
final Drawing drawing = createDrawing();
|
||||||
|
drawing.drawGraph(graph);
|
||||||
|
|
||||||
drawing.drawGraph(graph);
|
final Path path = pathReader.readPath(graph);
|
||||||
|
drawing.drawPath(path);
|
||||||
final PathReader pathReader = new BinaryPathReader(
|
}
|
||||||
new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))));
|
|
||||||
|
|
||||||
final Path path = pathReader.readPath(graph);
|
|
||||||
|
|
||||||
drawing.drawPath(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue