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
|
||||
*.path
|
||||
*.tgz
|
||||
/.metadata/
|
||||
|
|
|
|||
|
|
@ -1,14 +1,5 @@
|
|||
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 DijkstraAlgorithm(ShortestPathData data) {
|
||||
|
|
@ -21,64 +12,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// retrieve data from the input problem (getInputData() is inherited from the
|
||||
// parent class ShortestPathAlgorithm)
|
||||
final ShortestPathData data = getInputData();
|
||||
Graph graph = data.getGraph();
|
||||
|
||||
final int nbNodes = graph.size();
|
||||
|
||||
// variable that will contain the solution of the shortest path problem
|
||||
ShortestPathSolution solution = null;
|
||||
|
||||
// Initialize array of labels.
|
||||
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);
|
||||
// TODO: implement the Dijkstra algorithm
|
||||
|
||||
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
|
||||
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
|
||||
public void remove(E x) throws ElementNotFoundException {
|
||||
int index = this.array.indexOf(x);
|
||||
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);
|
||||
// TODO:
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import org.insa.graphs.gui.drawing.components.BasicDrawing;
|
|||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.model.Path;
|
||||
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.PathReader;
|
||||
|
||||
|
|
@ -58,26 +57,23 @@ public class Launch {
|
|||
try (final GraphReader reader = new BinaryGraphReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(mapName))))) {
|
||||
|
||||
// DONE: read the graph
|
||||
graph = reader.read();
|
||||
// TODO: read the graph
|
||||
graph = null;
|
||||
}
|
||||
|
||||
// create the drawing
|
||||
final Drawing drawing = createDrawing();
|
||||
|
||||
// DONE: draw the graph on the drawing
|
||||
drawing.drawGraph(graph);
|
||||
// TODO: draw the graph on the drawing
|
||||
|
||||
// DONE: create a path reader
|
||||
try (final PathReader pathReader = new BinaryPathReader(new DataInputStream(
|
||||
new BufferedInputStream(new FileInputStream(pathName))))) {
|
||||
// TODO: create a path reader
|
||||
try (final PathReader pathReader = null) {
|
||||
|
||||
// DONE: read the path
|
||||
path = pathReader.readPath(graph);
|
||||
// TODO: read the path
|
||||
path = null;
|
||||
}
|
||||
|
||||
// DONE: draw the path on the drawing
|
||||
drawing.drawPath(path);
|
||||
// TODO: draw the path on the drawing
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ package org.insa.graphs.model;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
|
@ -27,32 +25,12 @@ public class Path {
|
|||
* @return A path that goes through the given list of nodes.
|
||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||
* 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)
|
||||
throws IllegalArgumentException {
|
||||
List<Arc> arcs = new ArrayList<Arc>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
|
||||
|
|
@ -65,20 +43,12 @@ public class Path {
|
|||
* @return A path that goes through the given list of nodes.
|
||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||
* 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)
|
||||
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>();
|
||||
for (int i=0; i<nodes.size()-1; i++) {
|
||||
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")));
|
||||
}
|
||||
// TODO:
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
|
||||
|
|
@ -214,34 +184,22 @@ public class Path {
|
|||
* </ul>
|
||||
*
|
||||
* @return true if the path is valid, false otherwise.
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
|
||||
if (this.isEmpty()) { return true; }
|
||||
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;
|
||||
// TODO:
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the length of this path (in meters).
|
||||
*
|
||||
* @return Total length of the path (in meters).
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public float getLength() {
|
||||
float result = 0f;
|
||||
for (Arc arc : arcs) {
|
||||
result += arc.getLength();
|
||||
}
|
||||
return result;
|
||||
// TODO:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -250,13 +208,11 @@ public class Path {
|
|||
* @param speed Speed to compute the travel time.
|
||||
* @return Time (in seconds) required to travel this path at the given speed (in
|
||||
* kilometers-per-hour).
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public double getTravelTime(double speed) {
|
||||
double ttime = 0;
|
||||
for (Arc thisArc : arcs) {
|
||||
ttime += thisArc.getTravelTime(speed);
|
||||
}
|
||||
return ttime;
|
||||
// TODO:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -264,15 +220,11 @@ public class Path {
|
|||
* every arc.
|
||||
*
|
||||
* @return Minimum travel time to travel this path (in seconds).
|
||||
* @deprecated Need to be implemented.
|
||||
*/
|
||||
public double getMinimumTravelTime() {
|
||||
double ttime = 0;
|
||||
int maxSpeed;
|
||||
for (Arc thisArc : arcs) {
|
||||
maxSpeed = thisArc.getRoadInformation().getMaximumSpeed();
|
||||
ttime += thisArc.getTravelTime(maxSpeed);
|
||||
}
|
||||
return ttime;
|
||||
// TODO:
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue