Various improvements

This commit is contained in:
Yohan Simard 2020-05-30 19:42:08 +02:00
parent 539ea33752
commit 25e08fc7f0
7 changed files with 37 additions and 41 deletions

3
.gitignore vendored
View file

@ -17,4 +17,5 @@ doc
*.mapfg
*.mapgr
*.path
*.tgz
*.tgz
/rapport/

View file

@ -57,9 +57,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Node destination = data.getDestination();
Node currentNode = null;
/*
// Debug / Tests
double previousCost = 0;
int iterationCounter = 0;
*/
while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
// Get the node with the minimum cost
@ -69,11 +71,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
/*
// Debug / Tests
assert(((BinaryHeap<Label>) heap).isValid());
assert(currentLabel.getTotalCost() >= previousCost);
assert (((BinaryHeap<Label>) heap).isValid());
assert (currentLabel.getTotalCost() >= previousCost);
previousCost = currentLabel.getCost();
*/
iterationCounter++;
*/
for (Arc arc : currentNode.getSuccessors()) {
Node dest = arc.getDestination();
@ -85,14 +87,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
double newCost = currentLabel.getCost() + arcCost;
double oldCost = destLabel.getCost();
if (newCost < oldCost) {
try {
if (Double.isFinite(oldCost)) { // if already visited, remove from the heap before updating
heap.remove(destLabel);
} catch (ElementNotFoundException ignored) {
} else { // else, notify observers
notifyNodeReached(dest);
}
destLabel.updateCost(newCost, arc);
heap.insert(destLabel);
if (Double.isInfinite(oldCost))
notifyNodeReached(dest);
}
}
}
@ -115,7 +116,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
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);

View file

@ -65,10 +65,11 @@ public class Label implements Comparable<Label> {
@Override
public int compareTo(Label o) {
int comp = Double.compare(getTotalCost(), o.getTotalCost());
if (comp == 0 && this.getClass() == LabelStar.class && o.getClass() == LabelStar.class)
return Double.compare(((LabelStar)this).getEstimatedCost(), ((LabelStar)o).getEstimatedCost());
else
return comp;
int cmp = Double.compare(this.getTotalCost(), o.getTotalCost());
if (cmp == 0) {
cmp = Double.compare(o.getCost(), this.getCost());
}
return cmp;
}
}

View file

@ -1,7 +1,6 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
public class LabelStar extends Label {

View file

@ -66,16 +66,16 @@ public abstract class ShortestPathTest {
assertTrue(sol.isFeasible());
Path path = sol.getPath();
assertTrue(path.isValid());
// check the optimality locally
if (i == 0)
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
if (i == 1)
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
// Check result against Bellman Ford algorithm (except for the square map
// where there may be several paths with the same cost)
if (i != 0)
// skip these tests for the square map where there may be several arcs with the same cost
if (i != 0) {
// check the optimality locally
if (arcInspector == arcInspectors[0])
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
if (arcInspector == arcInspectors[1])
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
// Check result against Bellman Ford algorithm
assertEquals(sol.getPath(), oracle.getPath());
System.out.println();
}
}
}

View file

@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/maps" />
<excludeFolder url="file://$MODULE_DIR$/rapport" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

View file

@ -22,9 +22,8 @@ public class Launch {
/**
* Create a new Drawing inside a JFrame an return it.
*
*
* @return The created drawing.
*
* @throws Exception if something wrong happens when creating the graph.
*/
public static Drawing createDrawing() throws Exception {
@ -45,28 +44,22 @@ public class Launch {
}
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 pathName = "maps/path_fr31insa_rangueil_insa.path";
// Create a graph reader.
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
try (final GraphReader reader = new BinaryGraphReader(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:
final Drawing drawing = createDrawing();
// Create the drawing:
final Drawing drawing = createDrawing();
drawing.drawGraph(graph);
drawing.drawGraph(graph);
final PathReader pathReader = new BinaryPathReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(pathName))));
final Path path = pathReader.readPath(graph);
drawing.drawPath(path);
final Path path = pathReader.readPath(graph);
drawing.drawPath(path);
}
}
}