mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-28 23:07:11 +00:00
remove test code createDijkstra2()
This commit is contained in:
1 parent
9cc1e69843
commit
28b683abf6
1 file changed
-125
-125
@@ -358,131 +358,6 @@ public class SailingSimulatorImpl implements SailingSimulator {
|
||||
|
||||
return new PathImpl(lst, windField);
|
||||
}
|
||||
|
||||
|
||||
private Path createDjikstra2() {
|
||||
//retrieve simulation parameters
|
||||
Boundary boundary = new RectangularBoundary(simulationParameters.getCourse().get(0),simulationParameters.getCourse().get(1));//simulationParameters.getBoundaries();
|
||||
WindField windField = simulationParameters.getWindField();
|
||||
PolarDiagram polarDiagram = simulationParameters.getBoatPolarDiagram();
|
||||
Position start = simulationParameters.getCourse().get(0);
|
||||
Position end = simulationParameters.getCourse().get(1);
|
||||
TimePoint startTime = new MillisecondsTimePoint(0);
|
||||
|
||||
//the solution path
|
||||
LinkedList<TimedPositionWithSpeed> lst = new LinkedList<TimedPositionWithSpeed>();
|
||||
|
||||
//initiate grid
|
||||
int gridv = simulationParameters.getProperty("Djikstra.gridv[int]").intValue(); // number of vertical grid steps
|
||||
int gridh = simulationParameters.getProperty("Djikstra.gridh[int]").intValue(); // number of horizontal grid steps
|
||||
Position[][] sailGrid = boundary.extractGrid(gridh, gridv);
|
||||
|
||||
//create adjacency graph including start and end
|
||||
Map<Position,List<Position>> graph = new HashMap<Position, List<Position>>();
|
||||
graph.put(start, Arrays.asList(sailGrid[1]));
|
||||
for(int i = 1; i < gridv-2; i++) {
|
||||
for(Position p : sailGrid[i]) {
|
||||
graph.put(p, Arrays.asList(sailGrid[i+1]));
|
||||
}
|
||||
}
|
||||
for(Position p : sailGrid[gridv-2]) {
|
||||
graph.put(p, Arrays.asList(end));
|
||||
}
|
||||
|
||||
//create backwards adjacency graph, required to reconstruct the optimal path
|
||||
Map<Position, List<Position>> backGraph = new HashMap<Position, List<Position>>();
|
||||
backGraph.put(end, Arrays.asList(sailGrid[gridv-2]));
|
||||
for(int i = gridv-2; i>1; i--) {
|
||||
for(Position p: sailGrid[i]) {
|
||||
backGraph.put(p, Arrays.asList(sailGrid[i-1]));
|
||||
}
|
||||
}
|
||||
for(Position p : sailGrid[1]) {
|
||||
backGraph.put(p, Arrays.asList(start));
|
||||
}
|
||||
|
||||
//create tentative distance matrix
|
||||
Map<Position, Long> tentativeDistances = new HashMap<Position, Long>();
|
||||
for(Position p : graph.keySet()) {
|
||||
tentativeDistances.put(p, Long.MAX_VALUE);
|
||||
}
|
||||
tentativeDistances.put(start, 0L);
|
||||
tentativeDistances.put(end, Long.MAX_VALUE);
|
||||
|
||||
//create set of unvisited nodes
|
||||
List<Position> unvisited = new ArrayList<Position>(graph.keySet());
|
||||
unvisited.add(end);
|
||||
|
||||
//set the initial node as current
|
||||
Position currentPosition = start;
|
||||
TimePoint currentTime = startTime;
|
||||
|
||||
//search loop
|
||||
//ends when the end is visited
|
||||
while(currentPosition != end) {
|
||||
//set the polar diagram to the wind at the current position and time
|
||||
TimedPosition currentTimedPosition = new TimedPositionImpl(currentTime, currentPosition);
|
||||
SpeedWithBearing currentWind = windField.getWind(currentTimedPosition);
|
||||
polarDiagram.setWind(currentWind);
|
||||
|
||||
//compute the tentative distance to all the unvisited neighbours of the current node
|
||||
//and replace it in the matrix if is smaller than the previous one
|
||||
List<Position> unvisitedNeighbours = new LinkedList<Position>(graph.get(currentPosition));
|
||||
unvisitedNeighbours.retainAll(unvisited);
|
||||
for(Position p : unvisitedNeighbours) {
|
||||
Bearing bearingToP = currentPosition.getBearingGreatCircle(p);
|
||||
Distance distanceToP = currentPosition.getDistance(p);
|
||||
Speed speedToP = polarDiagram.getSpeedAtBearing(bearingToP);
|
||||
//multiplied by 1000 to have milliseconds
|
||||
Long timeToP = (long) (1000 * (distanceToP.getMeters() / speedToP.getMetersPerSecond()));
|
||||
Long tentativeDistanceToP = currentTime.asMillis() + timeToP;
|
||||
if (tentativeDistanceToP < tentativeDistances.get(p)) {
|
||||
tentativeDistances.put(p, tentativeDistanceToP);
|
||||
}
|
||||
}
|
||||
|
||||
//mark current node as visited
|
||||
unvisited.remove(currentPosition);
|
||||
|
||||
//select the unvisited node with the smallest tentative distance
|
||||
//and set it as current
|
||||
Long minTentativeDistance = Long.MAX_VALUE;
|
||||
for (Position p : unvisited) {
|
||||
if( tentativeDistances.get(p) < minTentativeDistance ) {
|
||||
currentPosition = p;
|
||||
minTentativeDistance = tentativeDistances.get(p);
|
||||
currentTime = new MillisecondsTimePoint(minTentativeDistance);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//I need to add the end point to the distances matrix
|
||||
tentativeDistances.put(end,currentTime.asMillis());
|
||||
|
||||
//at this point currentPosition = end
|
||||
//currentTime = total duration of the course
|
||||
|
||||
//reconstruct the optimal path by going from start to end
|
||||
while(currentPosition != start) {
|
||||
TimedPositionWithSpeed currentTimedPositionWithSpeed = new TimedPositionWithSpeedImpl(currentTime, currentPosition, null );
|
||||
lst.addFirst(currentTimedPositionWithSpeed);
|
||||
List<Position> currentPredecessors = backGraph.get(currentPosition);
|
||||
Long minTime = Long.MAX_VALUE;
|
||||
for(Position p : currentPredecessors) {
|
||||
if(tentativeDistances.get(p) < minTime) {
|
||||
minTime = tentativeDistances.get(p);
|
||||
currentPosition = p;
|
||||
currentTime = new MillisecondsTimePoint(minTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
//I need to add the first point to the path
|
||||
lst.addFirst(new TimedPositionWithSpeedImpl(startTime, start, null));
|
||||
|
||||
|
||||
return new PathImpl(lst, windField);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Path> getAllPaths() {
|
||||
|
||||
Reference in new issue
Block a user