This project is a solution to the minimum time path planning problem for a small electric plane as described in the file problem_statement.md
.
The plane has a limited range and can recharge at intermediate airports along its journey.
The plane charges faster at some city's airports than at others.
The plane leaves with a full charge.
This readme describes how to find the optimal solution to this problem.
To build with CMake first create a build directory:
mkdir build && cd build
Then run CMake and make from the build
directory:
cmake .. && make -j4
Running this command will create two executables: flight_planner
and flight_planner_test
.
The instructions for running flight_planner
are described in the problem_statement.md
.
The unit test can be run with the following command:
./flight_planner_test
The plane takes time doing two things: flying and charging. Finding the minimum time path requires minimizing the sum of the flying time and the charging time.
total time = flying time + charging time
This minimum time path is found by performing a graph search over a directed graph.
A directed graph is made up of vertices and directed edges. For this problem, each vertex represents an allowed state of the plane. The plane's state consists of city and the amount of flight time the battery has remaining.
state = (city, battery_remaining)
Edges represent allowed transitions between states. For this problem, there are two types of state transitions: flying and charging. For both edge types, the cost is the transition time.
edge cost = flight time (flying edges)
edge cost = charge time (charging edges)
Flying edges describe transitions from one city to another.
Flying
(city_i, battery_remaining) --> (city_j, battery_remaining - flight time)
Charging edges describe increases is battery level.
Charging
(city_i, battery_u) --> (city_i, battery_u+)
The charging time depends on
R = d(battery level) / d(charging time)
The time required to charge from battery level
charge_time = (battery_u^+ - battery_u^-) / R.
Shortest paths. The minimum time path between two states is approximately the minimum time path between the corresponding vertices in the graph. This approximate path can be found with a shortest "path" algorithm such as Djikstra's or A*. For specially constructed graphs, there is no approximation, and the shortest path is the minimum time path. I discuss two graph construction methods in the next section. The first method constructs a graph that gives an approximate solution. The second method constructs a graph that gives the exact solution.
In this approach, the range of possible battery levels is discretized into a grid of
Example: The plane starts in city A with 1.00 battery and uses 0.63 battery to fly to city B.
Discrete levels | Battery B (true) |
Battery B (discrete) |
Error | |
---|---|---|---|---|
6 | (0.00, 0.20, 0.40, ..., 1.00) | 0.37 | 0.20 | 0.17 |
11 | (0.00, 0.10, 0.20, ..., 1.00) | 0.37 | 0.30 | 0.07 |
21 | (0.00, 0.05, 0.10, ..., 1.00) | 0.37 | 0.35 | 0.02 |
The table above shows how the discretization error tends to decrease as
This path planning problem is structured in such a way that only certain city-specific battery levels can be present in the optimal solution. By constructing a graph that contains only these battery levels using the algorithm described below, the optimal solution is guaranteed.
Optimal graph construction algorithm:
The plane's flight time on a full battery is
(city
(city
For a graph constructed with this approach, the minimum time path found with a shortest path algorithm is the optimal solution. The proof for this result can be found in the proof.md file.
I implemented both graph construction methods and compared the results.
The exact graph is notably easier to implement than the approximate graph. The implementation for the approximate graph is more involved because flying transitions are inexact. Finding the discrete battery level closest to the true battery level without going over adds an extra step. The code contains an implementation of both graph construction methods.
As shown in the table below, the exact graph has 9,918 vertices and 28,542 edges.
This vertex count is similar to the approximate graph with
Graph | Vertex # | Edge # | Total Time* |
---|---|---|---|
n = 8 | 2,424 | 19,328 | 68.8800 |
n = 32 | 9,696 | 77,212 | 67.1495 |
n = 128 | 38,784 | 309,080 | 66.3071 |
n = 512 | 155,136 | 1,236,666 | 66.1738 |
Exact | 9,918 | 28,542 | 66.1095 |
* Manteca_CA to Fort_Myers_FL