Multiple impulses: planet to planet transfer

Multiple impulses: planet to planet transfer#

In this tutorial, we use the pykep.trajopt.pl2pl_N_impulses to study a single direct transfer between planets.

The encoding is original with pykep and makes use of a decision vector (chromosome) in the form:

\[ \mathbf x = [t_0,T] + \underbrace{[\alpha_0,u_0,v_0,\Delta V_0] + [\alpha_1,u_1,v_1,\Delta V_1] + ...}_{\text{propagations}} + \underbrace{[\alpha_{N-1}]}_{\text{Lambert}} + \underbrace{([t_f])}_{\text{Optional phase term}} \]

to define an \(N\) impulse trajectory starting at \(t_0\) at the departure planet with a \(\Delta V_0\) and arriving at the target planet with a \(\Delta V_{N-1}\) and applying the remaining \(\Delta V_i\) at epochs encoded using the \(\alpha\)-encoding for times (see Multiple Gravity Assist (MGA)).

This optimization problem is a nice tool to visualize and study the famous “How many impulses?” question which has a very complicated answer in general.

We start, as often, with some fundamental imports:

import pykep as pk
import numpy as np
import pygmo as pg

We define the problem as an Earth-Venus transfer. We set the transfer time as fixed as well as the departure epoch.

start = pk.planet(pk.udpla.jpl_lp("earth"))
target = pk.planet(pk.udpla.jpl_lp("venus"))

tof_bounds = [360.0, 360.0]
DV_max_bounds = [0.0, 4]
phase_free = False
multi_objective = False
t0_bounds = [140, 140.]

udp3 = pk.trajopt.pl2pl_N_impulses(
    start=start,
    target=target,
    N_max=3,
    tof_bounds=tof_bounds,
    phase_free=phase_free,
    multi_objective=multi_objective,
    t0_bounds=t0_bounds,
    DV_max_bounds=DV_max_bounds
)
def solve(udp, N=20):
    # We use CMA-ES 
    uda = pg.cmaes(4500, force_bounds=True, sigma0=0.5, ftol=1e-4)
    # But if you prefer a self adaptive version of differential evolution thats also OK.
    #uda = pg.sade(2500, ftol=1e-4, xtol=1e-4)
    algo = pg.algorithm(uda)

    print("Multi-start:")

    res = list()
    for i in range(N):
        pop = pg.population(udp, 20)
        pop = algo.evolve(pop)
        res.append([pop.champion_f, pop.champion_x])
        print(i, pop.champion_f[0], end= '\r')
        

    best_x = sorted(res, key =  lambda x: x[0][0])[0][1]

    print(f"\nThe best solution found has a DV of {udp.fitness(best_x)[0]/1000:.5e} km/s")
    return best_x

best_x_3 = solve(udp3)
Multi-start:
0 6015.3320758
1 5986.38945671
2 5986.38945956
3 6015.3320758
4 6312.58212984
5 6312.58212984
6 6015.33207582
7 6312.58212984
8 6015.33207582
9 6312.58212984
10 6015.3320758
11 6015.33207581
12 6015.33207582
13 6015.3320758
14 5986.3894644
15 5986.38945956
16 5986.38945885
17 5986.38945995
18 5986.38946176
19 6015.3320758
The best solution found has a DV of 5.98639e+00 km/s

We can easily visualize the solution:

def visualise(udp, best_x):
    import matplotlib.pyplot as plt
    import numpy as np

    # Create the figure and define the grid
    fig = plt.figure(figsize=(10, 10))
    gs = fig.add_gridspec(
        2, 2, width_ratios=[2, 1], height_ratios=[1, 1], wspace=0.01, hspace=0.01
    )

    # Top view (spans rows and columns)
    ax1 = fig.add_subplot(gs[:, 0], projection="3d")
    udp.plot(best_x, ax=ax1)
    ax1.view_init(90, 0)
    ax1.axis("off")

    # Ecliptic view 1
    ax1 = fig.add_subplot(gs[0, 1], projection="3d")
    udp.plot(best_x, ax=ax1)
    ax1.view_init(0, 0)
    ax1.axis("off")

    # Ecliptic view 2
    ax1 = fig.add_subplot(gs[1, 1], projection="3d")
    udp.plot(best_x, ax=ax1)
    ax1.view_init(0, 90)
    ax1.axis("off")
    return fig


visualise(udp3, best_x_3);
../_images/25c1c2aace020e91ccd64c5f0a5749594925e4df5bdb478f1d2d7cf135eaf36a.png

… and quickly investigate a human readable format for the solution found.

udp3.pretty(best_x_3)
Total DV (m/s):  5986.389456707614
Dvs (m/s):  [219.51681069983704, 2899.334330153613, 2867.5383158541645]
Total DT (days):  360.0
Tofs (days):  [205.94321825773307, 154.0567817422669]

Are more impulses needed?#

In the previous design we allowed maximum 3 impulses, but would more impulse be needed? We can quickly answer this question for this specific case by looking at the primer vector.

ax, (grid, p) = udp3.plot_primer_vector(best_x_3)
../_images/37851ee01523ac8e7ceb67623fdf1b7d89063e798566cebece765729988c91e6.png

… the primer vector goes above one this trajevtory IS NOT OPTIMAL. We need more impulses. Lets add one.

udp4 = pk.trajopt.pl2pl_N_impulses(
    start=start,
    target=target,
    N_max=4,
    tof_bounds=tof_bounds,
    phase_free=phase_free,
    multi_objective=multi_objective,
    t0_bounds=t0_bounds,
    DV_max_bounds=DV_max_bounds
)
best_x_4 = solve(udp4, N=20)
Multi-start:
0 6015.3320758
1 6015.33207588
2 5986.38945625
3 5986.38945627
4 5986.38945627
5 6015.33207581
6 6015.4406176
7 6015.33207596
8 6015.41546839
9 5937.92739667
10 5986.44329516
11 5986.38945628
12 6312.58212984
13 5986.38945626
14 5937.927397
15 5986.44302325
16 5980.12401956
17 5986.38945626
18 5986.44294974
19 5986.38945625
The best solution found has a DV of 5.93793e+00 km/s
visualise(udp4, best_x_4);
../_images/f17c73275399a799af9f8a8adf8f5b05085f8c1bba32b03424d8467e37149380.png
udp4.pretty(best_x_4)
Total DV (m/s):  5937.927396671449
Dvs (m/s):  [197.6796331776498, 2732.2306988919145, 230.9797003141963, 2777.037364287689]
Total DT (days):  360.0
Tofs (days):  [209.6646718354926, 102.48729970491561, 47.848028459591795]

We found a better solution! Four impulses are better than 4 in this case! This is quite rare and interesting as a case and its here found as the optimization problem was defined to have a fixed duration and starting date. Typically, when freeing these degrees of freedom, three impulses are enough.

Try to find other interesting cases where the number of impulses matter, and do not be fooled by artefacts. Some times impulses with zero \(\Delta V\) are added to move around the starting date outside of the bounds. Whats the maximum number of impulses you can find to be optimal?

We conclude showing the primer vector for this case:

ax, (grid, p) = udp4.plot_primer_vector(best_x_4)
../_images/f93c38b78ea0d22dd60e86009d5318b7b0de5699a7e751c56034ee3a025172c6.png

The very same trajectory is also studied in the notebook dedicated to the primer vector where the whole theory is also developed and coded as to show its inner workings.