forked from lebotlan/BE-Graphes
Compare commits
No commits in common. "main" and "main" have entirely different histories.
6 changed files with 26 additions and 206 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -19,4 +19,3 @@ doc
|
||||||
*.mapgr
|
*.mapgr
|
||||||
*.path
|
*.path
|
||||||
*.tgz
|
*.tgz
|
||||||
/.metadata/
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,5 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
|
||||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
|
||||||
import org.insa.graphs.model.Arc;
|
|
||||||
import org.insa.graphs.model.Graph;
|
|
||||||
import org.insa.graphs.model.Path;
|
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
public DijkstraAlgorithm(ShortestPathData data) {
|
public DijkstraAlgorithm(ShortestPathData data) {
|
||||||
|
|
@ -21,63 +12,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
// retrieve data from the input problem (getInputData() is inherited from the
|
// retrieve data from the input problem (getInputData() is inherited from the
|
||||||
// parent class ShortestPathAlgorithm)
|
// parent class ShortestPathAlgorithm)
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
Graph graph = data.getGraph();
|
|
||||||
|
|
||||||
final int nbNodes = graph.size();
|
|
||||||
|
|
||||||
// variable that will contain the solution of the shortest path problem
|
// variable that will contain the solution of the shortest path problem
|
||||||
ShortestPathSolution solution = null;
|
ShortestPathSolution solution = null;
|
||||||
|
|
||||||
// Initialize array of labels.
|
// TODO: implement the Dijkstra algorithm
|
||||||
ArrayList<Label> labelsList = new ArrayList<Label>();
|
|
||||||
for (int i=0; i<nbNodes; i++) {
|
|
||||||
labelsList.add(new Label(graph.get(i)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Set cost of origin to zero
|
|
||||||
Label destinationNodeLabel = labelsList.get(data.getDestination().getId());
|
|
||||||
Label concurentNodeLabel = labelsList.get(data.getOrigin().getId());
|
|
||||||
concurentNodeLabel.setCoutRealise(0);
|
|
||||||
|
|
||||||
notifyOriginProcessed(data.getOrigin());
|
|
||||||
|
|
||||||
BinaryHeap<Label> labelsHeap = new BinaryHeap<Label>();
|
|
||||||
labelsHeap.insert(concurentNodeLabel);
|
|
||||||
|
|
||||||
boolean found = false;
|
|
||||||
while (!found && !labelsHeap.isEmpty()) {
|
|
||||||
concurentNodeLabel = labelsHeap.deleteMin();
|
|
||||||
if (!concurentNodeLabel.getMarque()) {
|
|
||||||
notifyNodeReached(concurentNodeLabel.getSommetCourant());
|
|
||||||
concurentNodeLabel.setMarque();
|
|
||||||
for (Arc arc : concurentNodeLabel.getSommetCourant().getSuccessors()) {
|
|
||||||
Label successorLabel = labelsList.get(arc.getDestination().getId());
|
|
||||||
Float newCost = arc.getLength() + concurentNodeLabel.getCost();
|
|
||||||
if (newCost < successorLabel.getCost()) {
|
|
||||||
successorLabel.setPere(arc);
|
|
||||||
successorLabel.setCoutRealise(newCost);
|
|
||||||
labelsHeap.insert(successorLabel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (destinationNodeLabel.getMarque()) {
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
notifyDestinationReached(data.getDestination());
|
|
||||||
|
|
||||||
ArrayList<Arc> pathArcs = new ArrayList<Arc>();
|
|
||||||
concurentNodeLabel = destinationNodeLabel;
|
|
||||||
while(concurentNodeLabel.getPere() != null) {
|
|
||||||
pathArcs.add(0, concurentNodeLabel.getPere());
|
|
||||||
concurentNodeLabel = labelsList.get(concurentNodeLabel.getPere().getOrigin().getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collections.reverse(pathArcs);
|
|
||||||
|
|
||||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, pathArcs));
|
|
||||||
|
|
||||||
// when the algorithm terminates, return the solution that has been found
|
// when the algorithm terminates, return the solution that has been found
|
||||||
return solution;
|
return solution;
|
||||||
|
|
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
|
||||||
|
|
||||||
import org.insa.graphs.model.Arc;
|
|
||||||
import org.insa.graphs.model.Node;
|
|
||||||
|
|
||||||
public class Label implements Comparable<Label> {
|
|
||||||
|
|
||||||
private Node sommetCourant;
|
|
||||||
|
|
||||||
private Boolean marque;
|
|
||||||
|
|
||||||
private float coutRealise;
|
|
||||||
|
|
||||||
private Arc pere;
|
|
||||||
|
|
||||||
public Label(Node sommetCourant) {
|
|
||||||
this.sommetCourant = sommetCourant;
|
|
||||||
this.marque = false;
|
|
||||||
this.coutRealise = Float.POSITIVE_INFINITY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node getSommetCourant() {
|
|
||||||
return sommetCourant;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean getMarque() {
|
|
||||||
return marque;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMarque() {
|
|
||||||
this.marque = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Arc getPere() {
|
|
||||||
return pere;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPere(Arc pere) {
|
|
||||||
this.pere = pere;
|
|
||||||
}
|
|
||||||
|
|
||||||
// public double getCoutRealise() {
|
|
||||||
// return coutRealise;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void setCoutRealise(float nouveauCout) {
|
|
||||||
this.coutRealise = nouveauCout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getCost() {
|
|
||||||
return coutRealise;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(Label o) {
|
|
||||||
return Double.compare(this.coutRealise, o.getCost()) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -134,13 +134,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(E x) throws ElementNotFoundException {
|
public void remove(E x) throws ElementNotFoundException {
|
||||||
int index = this.array.indexOf(x);
|
// TODO:
|
||||||
if (index == -1 || index >= this.currentSize) { throw new ElementNotFoundException(x); }
|
|
||||||
E lastItem = this.array.get(--this.currentSize);
|
|
||||||
this.arraySet(index, lastItem);
|
|
||||||
this.percolateUp(index);
|
|
||||||
index = this.array.indexOf(lastItem);
|
|
||||||
this.percolateDown(index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ import org.insa.graphs.gui.drawing.components.BasicDrawing;
|
||||||
import org.insa.graphs.model.Graph;
|
import org.insa.graphs.model.Graph;
|
||||||
import org.insa.graphs.model.Path;
|
import org.insa.graphs.model.Path;
|
||||||
import org.insa.graphs.model.io.BinaryGraphReader;
|
import org.insa.graphs.model.io.BinaryGraphReader;
|
||||||
import org.insa.graphs.model.io.BinaryPathReader;
|
|
||||||
import org.insa.graphs.model.io.GraphReader;
|
import org.insa.graphs.model.io.GraphReader;
|
||||||
import org.insa.graphs.model.io.PathReader;
|
import org.insa.graphs.model.io.PathReader;
|
||||||
|
|
||||||
|
|
@ -58,26 +57,23 @@ public class Launch {
|
||||||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||||
new BufferedInputStream(new FileInputStream(mapName))))) {
|
new BufferedInputStream(new FileInputStream(mapName))))) {
|
||||||
|
|
||||||
// DONE: read the graph
|
// TODO: read the graph
|
||||||
graph = reader.read();
|
graph = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the drawing
|
// create the drawing
|
||||||
final Drawing drawing = createDrawing();
|
final Drawing drawing = createDrawing();
|
||||||
|
|
||||||
// DONE: draw the graph on the drawing
|
// TODO: draw the graph on the drawing
|
||||||
drawing.drawGraph(graph);
|
|
||||||
|
|
||||||
// DONE: create a path reader
|
// TODO: create a path reader
|
||||||
try (final PathReader pathReader = new BinaryPathReader(new DataInputStream(
|
try (final PathReader pathReader = null) {
|
||||||
new BufferedInputStream(new FileInputStream(pathName))))) {
|
|
||||||
|
|
||||||
// DONE: read the path
|
// TODO: read the path
|
||||||
path = pathReader.readPath(graph);
|
path = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DONE: draw the path on the drawing
|
// TODO: draw the path on the drawing
|
||||||
drawing.drawPath(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,7 @@ package org.insa.graphs.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
|
@ -27,32 +25,12 @@ public class Path {
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||||
* consecutive nodes in the list are not connected in the graph.
|
* consecutive nodes in the list are not connected in the graph.
|
||||||
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
|
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
List<Arc> arcs = new ArrayList<Arc>();
|
List<Arc> arcs = new ArrayList<Arc>();
|
||||||
|
// TODO:
|
||||||
if (nodes.size() == 1) { return new Path(graph, nodes.get(0)); }
|
|
||||||
|
|
||||||
for (int i = 0; i < nodes.size() - 1; i++) {
|
|
||||||
Arc bestArc = null;
|
|
||||||
double bestSpeed = Double.MAX_VALUE;
|
|
||||||
for (Arc arcsucc : nodes.get(i).getSuccessors()) {
|
|
||||||
if (arcsucc.getDestination().equals(nodes.get(i+1))) {
|
|
||||||
int succspeed = arcsucc.getRoadInformation().getMaximumSpeed();
|
|
||||||
if (arcsucc.getTravelTime(succspeed) < bestSpeed) {
|
|
||||||
bestArc = arcsucc;
|
|
||||||
bestSpeed = arcsucc.getTravelTime(succspeed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (bestArc == null) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
} else {
|
|
||||||
arcs.add(bestArc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,20 +43,12 @@ public class Path {
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||||
* consecutive nodes in the list are not connected in the graph.
|
* consecutive nodes in the list are not connected in the graph.
|
||||||
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
|
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
if (nodes.size() == 0) {
|
|
||||||
return new Path(graph);
|
|
||||||
}
|
|
||||||
if (nodes.size() == 1) {
|
|
||||||
return new Path(graph, nodes.getFirst());
|
|
||||||
}
|
|
||||||
List<Arc> arcs = new ArrayList<Arc>();
|
List<Arc> arcs = new ArrayList<Arc>();
|
||||||
for (int i=0; i<nodes.size()-1; i++) {
|
// TODO:
|
||||||
int j = i;
|
|
||||||
arcs.add(nodes.get(i).getSuccessors().stream().filter(a -> a.getDestination().equals(nodes.get(j+1))).min(Comparator.comparing(Arc::getLength)).orElseThrow(() -> new IllegalArgumentException("Invalid list of nodes")));
|
|
||||||
}
|
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,34 +184,22 @@ public class Path {
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @return true if the path is valid, false otherwise.
|
* @return true if the path is valid, false otherwise.
|
||||||
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
|
// TODO:
|
||||||
if (this.isEmpty()) { return true; }
|
return false;
|
||||||
if (this.size() == 1 && this.arcs.isEmpty()) { return true; }
|
|
||||||
if (!this.arcs.get(0).getOrigin().equals(this.origin)) { return false; }
|
|
||||||
|
|
||||||
Boolean good = true;
|
|
||||||
for (int i = 0; i < this.arcs.size() - 1; i++) {
|
|
||||||
if (!this.arcs.get(i).getDestination().equals(this.arcs.get(i+1).getOrigin())) {
|
|
||||||
good = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return good;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the length of this path (in meters).
|
* Compute the length of this path (in meters).
|
||||||
*
|
*
|
||||||
* @return Total length of the path (in meters).
|
* @return Total length of the path (in meters).
|
||||||
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public float getLength() {
|
public float getLength() {
|
||||||
float result = 0f;
|
// TODO:
|
||||||
for (Arc arc : arcs) {
|
return 0;
|
||||||
result += arc.getLength();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -250,13 +208,11 @@ public class Path {
|
||||||
* @param speed Speed to compute the travel time.
|
* @param speed Speed to compute the travel time.
|
||||||
* @return Time (in seconds) required to travel this path at the given speed (in
|
* @return Time (in seconds) required to travel this path at the given speed (in
|
||||||
* kilometers-per-hour).
|
* kilometers-per-hour).
|
||||||
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public double getTravelTime(double speed) {
|
public double getTravelTime(double speed) {
|
||||||
double ttime = 0;
|
// TODO:
|
||||||
for (Arc thisArc : arcs) {
|
return 0;
|
||||||
ttime += thisArc.getTravelTime(speed);
|
|
||||||
}
|
|
||||||
return ttime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -264,15 +220,11 @@ public class Path {
|
||||||
* every arc.
|
* every arc.
|
||||||
*
|
*
|
||||||
* @return Minimum travel time to travel this path (in seconds).
|
* @return Minimum travel time to travel this path (in seconds).
|
||||||
|
* @deprecated Need to be implemented.
|
||||||
*/
|
*/
|
||||||
public double getMinimumTravelTime() {
|
public double getMinimumTravelTime() {
|
||||||
double ttime = 0;
|
// TODO:
|
||||||
int maxSpeed;
|
return 0;
|
||||||
for (Arc thisArc : arcs) {
|
|
||||||
maxSpeed = thisArc.getRoadInformation().getMaximumSpeed();
|
|
||||||
ttime += thisArc.getTravelTime(maxSpeed);
|
|
||||||
}
|
|
||||||
return ttime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue