From b6fe61062c716529f354857a3770ca4e3618d493 Mon Sep 17 00:00:00 2001 From: d036654 Date: Tue, 12 Jun 2012 15:31:03 +0200 Subject: [PATCH 1/5] testing dijkstra --- .../simulator/impl/SailingSimulatorImpl.java | 130 +++++++++++++++++- 1 file changed, 128 insertions(+), 2 deletions(-) diff --git a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java index 4889f974902..9effc3df005 100644 --- a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java +++ b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java @@ -333,12 +333,138 @@ 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 lst = new LinkedList(); + + //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> graph = new HashMap>(); + 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> backGraph = new HashMap>(); + 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 tentativeDistances = new HashMap(); + 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 unvisited = new ArrayList(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 unvisitedNeighbours = new LinkedList(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 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 getAllPaths() { Map allPaths = new HashMap(); //allPaths.put("Dummy", createDummy()); - allPaths.put("Heuristic", createHeuristic()); - allPaths.put("Djikstra", createDjikstra()); + //allPaths.put("Opportunistic", createHeuristic()); + //allPaths.put("Djikstra", createDjikstra()); + allPaths.put("Djikstra2", createDjikstra2()); return allPaths; } From 81ee2f5e56bb42da8503f54a9c72974b5806f961 Mon Sep 17 00:00:00 2001 From: d036654 Date: Wed, 13 Jun 2012 13:43:31 +0200 Subject: [PATCH 2/5] fix in polar diagram --- .../sailing/simulator/impl/PolarDiagram49.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/PolarDiagram49.java b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/PolarDiagram49.java index bcc604ff0f3..bf4046c1913 100644 --- a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/PolarDiagram49.java +++ b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/PolarDiagram49.java @@ -99,7 +99,7 @@ public class PolarDiagram49 implements PolarDiagram { else { ceilingSpeed = ceilingSpeed1.getKnots() + (relativeBearing.getRadians() - ceilingBearing1.getRadians()) - * (ceilingSpeed2.getKnots() - ceilingSpeed2.getKnots()) + * (ceilingSpeed2.getKnots() - ceilingSpeed1.getKnots()) / (ceilingBearing2.getRadians() - ceilingBearing1.getRadians()); } @@ -305,15 +305,16 @@ public class PolarDiagram49 implements PolarDiagram { beatAngles.put(new KnotSpeedImpl(16), new DegreeBearingImpl(36.3)); beatAngles.put(new KnotSpeedImpl(20), new DegreeBearingImpl(36.9)); + double beatScale = 4.0; NavigableMap beatSOG = new TreeMap(); beatSOG.put(new KnotSpeedImpl(0), new KnotSpeedImpl(0)); - beatSOG.put(new KnotSpeedImpl(6), new KnotSpeedImpl(5.85)); - beatSOG.put(new KnotSpeedImpl(8), new KnotSpeedImpl(6.98)); - beatSOG.put(new KnotSpeedImpl(10), new KnotSpeedImpl(7.39)); - beatSOG.put(new KnotSpeedImpl(12), new KnotSpeedImpl(7.68)); - beatSOG.put(new KnotSpeedImpl(14), new KnotSpeedImpl(7.89)); - beatSOG.put(new KnotSpeedImpl(16), new KnotSpeedImpl(7.92)); - beatSOG.put(new KnotSpeedImpl(20), new KnotSpeedImpl(7.94)); + beatSOG.put(new KnotSpeedImpl(6), new KnotSpeedImpl(5.85*beatScale)); + beatSOG.put(new KnotSpeedImpl(8), new KnotSpeedImpl(6.98*beatScale)); + beatSOG.put(new KnotSpeedImpl(10), new KnotSpeedImpl(7.39*beatScale)); + beatSOG.put(new KnotSpeedImpl(12), new KnotSpeedImpl(7.68*beatScale)); + beatSOG.put(new KnotSpeedImpl(14), new KnotSpeedImpl(7.89*beatScale)); + beatSOG.put(new KnotSpeedImpl(16), new KnotSpeedImpl(7.92*beatScale)); + beatSOG.put(new KnotSpeedImpl(20), new KnotSpeedImpl(7.94*beatScale)); NavigableMap gybeAngles = new TreeMap(); gybeAngles.put(new KnotSpeedImpl(0), new DegreeBearingImpl(120)); From d4033cc8b6b0afe6df5dd335d7ad74640944e1e4 Mon Sep 17 00:00:00 2001 From: d036654 Date: Wed, 13 Jun 2012 13:44:54 +0200 Subject: [PATCH 3/5] remove log messages from grid --- .../sailing/simulator/impl/RectangularBoundary.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/RectangularBoundary.java b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/RectangularBoundary.java index 1733ffb9891..050f3ebcb18 100644 --- a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/RectangularBoundary.java +++ b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/RectangularBoundary.java @@ -175,7 +175,7 @@ public class RectangularBoundary implements Boundary { Position[][] grid = new Position[vPoints][hPoints]; Position pv; for (int i = 0; i < vPoints; i++) { - System.out.print("nrow "+i+": "); + //System.out.print("nrow "+i+": "); if (i == 0) { pv = rcStart; @@ -189,25 +189,24 @@ public class RectangularBoundary implements Boundary { // left side while (j < hPoints/2) { grid[i][j] = new DegreePosition(pv.getLatDeg() - ((hPoints-1)/2.-j)/(hPoints-1)*nHor[0]*2.*lscVrt, pv.getLngDeg() - ((hPoints-1)/2.-j)/(hPoints-1)*nHor[1]*2.*lscVrt); - System.out.print(""+grid[i][j]+"("+((hPoints-1)/2.-j)+"), "); + //System.out.print(""+grid[i][j]+"("+((hPoints-1)/2.-j)+"), "); j++; } // middle if (hPoints%2 == 1) { grid[i][j] = pv; - System.out.print(""+grid[i][j]+", "); + //System.out.print(""+grid[i][j]+", "); j++; } // right side while (j < hPoints) { grid[i][j] = new DegreePosition(pv.getLatDeg() + (j-(hPoints-1)/2.)/(hPoints-1)*nHor[0]*2.*lscVrt, pv.getLngDeg() + (j-(hPoints-1)/2.)/(hPoints-1)*nHor[1]*2.*lscVrt); - //grid[i][j] = pv.translateGreatCircle(getEast(), hStep.scale(j-(hPoints-1)/2.)); - System.out.print(""+grid[i][j]+"("+(j-(hPoints-1)/2.)+"), "); + //System.out.print(""+grid[i][j]+"("+(j-(hPoints-1)/2.)+"), "); j++; } - System.out.println(); + //System.out.println(); } /*System.out.println("Grid Index Test:"); From 1b04878c6a4ba0a180458b3e492ab96466e7f02e Mon Sep 17 00:00:00 2001 From: d036654 Date: Wed, 13 Jun 2012 13:45:34 +0200 Subject: [PATCH 4/5] reduce value range for average wind direction --- .../shared/windpattern/impl/WindPatternDisplayManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/shared/windpattern/impl/WindPatternDisplayManagerImpl.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/shared/windpattern/impl/WindPatternDisplayManagerImpl.java index caa2fc65bdb..410f43b44b6 100644 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/shared/windpattern/impl/WindPatternDisplayManagerImpl.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/shared/windpattern/impl/WindPatternDisplayManagerImpl.java @@ -66,7 +66,7 @@ public class WindPatternDisplayManagerImpl implements WindPatternDisplayManager display.addSetting(windSpeedSetting); WindPatternSetting baseWindBearing = new WindPatternSettingSliderBar("baseWindBearing", - "Average Wind Direction (Degrees)", -180, 180, 0); + "Average Wind Direction (Degrees)", -20, 20, 0); display.addSetting(baseWindBearing); WindPatternSetting frequency = new WindPatternSettingSliderBar("frequency", "Oscillation Frequency (per hr)", 0, 60, 30); From 28b683abf61926de1dcef9517c806c2baaea0dd5 Mon Sep 17 00:00:00 2001 From: d036654 Date: Wed, 13 Jun 2012 14:23:27 +0200 Subject: [PATCH 5/5] remove test code createDijkstra2() --- .../simulator/impl/SailingSimulatorImpl.java | 125 ------------------ 1 file changed, 125 deletions(-) diff --git a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java index b78deb33545..81a5f67f0e7 100644 --- a/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java +++ b/java/com.sap.sailing.simulator/src/com/sap/sailing/simulator/impl/SailingSimulatorImpl.java @@ -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 lst = new LinkedList(); - - //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> graph = new HashMap>(); - 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> backGraph = new HashMap>(); - 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 tentativeDistances = new HashMap(); - 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 unvisited = new ArrayList(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 unvisitedNeighbours = new LinkedList(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 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 getAllPaths() {