-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
136 lines (123 loc) · 4.53 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# SPDX-License-Identifier: BSD-3-Clause
# flake8: noqa F401
from collections.abc import Callable
import numpy as np
from vendeeglobe import (
Checkpoint,
Heading,
Instructions,
Location,
Vector,
config,
)
from vendeeglobe.utils import distance_on_surface
class Bot:
"""
This is the ship-controlling bot that will be instantiated for the competition.
"""
def __init__(self):
self.team = "TeamName" # This is your team name
# This is the course that the ship has to follow
self.course = [
Checkpoint(latitude=43.797109, longitude=-11.264905, radius=50),
Checkpoint(longitude=-29.908577, latitude=17.999811, radius=50),
Checkpoint(latitude=-11.441808, longitude=-29.660252, radius=50),
Checkpoint(longitude=-63.240264, latitude=-61.025125, radius=50),
Checkpoint(latitude=2.806318, longitude=-168.943864, radius=1990.0),
Checkpoint(latitude=-62.052286, longitude=169.214572, radius=50.0),
Checkpoint(latitude=-15.668984, longitude=77.674694, radius=1190.0),
Checkpoint(latitude=-39.438937, longitude=19.836265, radius=50.0),
Checkpoint(latitude=14.881699, longitude=-21.024326, radius=50.0),
Checkpoint(latitude=44.076538, longitude=-18.292936, radius=50.0),
Checkpoint(
latitude=config.start.latitude,
longitude=config.start.longitude,
radius=5,
),
]
def run(
self,
t: float,
dt: float,
longitude: float,
latitude: float,
heading: float,
speed: float,
vector: np.ndarray,
forecast: Callable,
world_map: Callable,
) -> Instructions:
"""
This is the method that will be called at every time step to get the
instructions for the ship.
Parameters
----------
t:
The current time in hours.
dt:
The time step in hours.
longitude:
The current longitude of the ship.
latitude:
The current latitude of the ship.
heading:
The current heading of the ship.
speed:
The current speed of the ship.
vector:
The current heading of the ship, expressed as a vector.
forecast:
Method to query the weather forecast for the next 5 days.
Example:
current_position_forecast = forecast(
latitudes=latitude, longitudes=longitude, times=0
)
world_map:
Method to query map of the world: 1 for sea, 0 for land.
Example:
current_position_terrain = world_map(
latitudes=latitude, longitudes=longitude
)
Returns
-------
instructions:
A set of instructions for the ship. This can be:
- a Location to go to
- a Heading to point to
- a Vector to follow
- a number of degrees to turn Left
- a number of degrees to turn Right
Optionally, a sail value between 0 and 1 can be set.
"""
# Initialize the instructions
instructions = Instructions()
# TODO: Remove this, it's only for testing =================
current_position_forecast = forecast(
latitudes=latitude, longitudes=longitude, times=0
)
current_position_terrain = world_map(latitudes=latitude, longitudes=longitude)
# ===========================================================
# Go through all checkpoints and find the next one to reach
for ch in self.course:
# Compute the distance to the checkpoint
dist = distance_on_surface(
longitude1=longitude,
latitude1=latitude,
longitude2=ch.longitude,
latitude2=ch.latitude,
)
# Consider slowing down if the checkpoint is close
jump = dt * np.linalg.norm(speed)
if dist < 2.0 * ch.radius + jump:
instructions.sail = min(ch.radius / jump, 1)
else:
instructions.sail = 1.0
# Check if the checkpoint has been reached
if dist < ch.radius:
ch.reached = True
if not ch.reached:
instructions.location = Location(
longitude=ch.longitude, latitude=ch.latitude
)
break
return instructions