Mars Uniform Dataset


In [1]:
# Get everything in line before we start
import sys
sys.path.append('../../')
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from Optimisation import HSS
from Trajectory import *
import pandas as pdNo
#%matplotlib inline
#%config InlineBackend.figure_format = 'svg'


/usr/lib/python2.7/dist-packages/matplotlib/__init__.py:874: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

In [36]:
# Assemble each core's dataset into one
'''
sets = [1,2,3,4]
ds = []
for i in sets:
    path = '../../Data/Mars/HSS_10_Alpha_' + str(i) + '.npy'
    z = np.load(path)
    ds.append(z)
ds = np.vstack(ds)
np.save('../../Data/Mars/HSS_10_Alpha_Full.npy', ds)
'''
ds = np.load('../../Data/Mars/HSS_10_Alpha_Full.npy')
ds.shape


Out[36]:
(8000, 148)

In [4]:
# Decode each trajectory
plt.close('all')
model = Point_Lander_Drag()
prob = HSS(model, nsegs=10)
dpts = []
for traj in ds:
    tf, sb, cb, s, c = prob.Decode(traj)
    plt.plot(s[:,0], s[:,1])
    plt.axes().set_aspect('equal', 'datalim')
    s = np.vstack((s, sb[1:,:]))
    c = np.vstack((c, cb[1:,:]))
    dp = np.hstack((s, c))
    dpts.append(dp)
traj = np.vstack(dpts)
traj.shape

plt.xlabel('Cross-Range [m]')
plt.ylabel('Altitude [m]')
plt.title('Database of Optimal Control Trajectories')
#plt.savefig('../../Data/Figs/New/Traj.pdf', format='pdf',
        #transparent=True, bbox_inches='tight')
plt.show()



In [6]:
dpts = array(dpts)
dpts


Out[6]:
array([[[    0.        ,  3000.        ,     0.        , ...,
          7500.        ,     0.        ,     0.89024803],
        [   -0.        ,  2730.65313186,    -0.        , ...,
          7500.        ,     0.        ,     0.        ],
        [   -0.        ,  2403.56020309,    -0.        , ...,
          7500.        ,     0.        ,     0.10739033],
        ..., 
        [   -0.00001968,   303.97896008,     0.00000155, ...,
          7172.81473954,     1.        ,     1.57079626],
        [   -0.00000775,   110.24286023,     0.00000412, ...,
          7058.30407739,     1.        ,     1.5707963 ],
        [    0.00000053,    12.34785184,    -0.00000014, ...,
          6943.79341524,     1.        ,     1.57079633]],

       [[    1.43082354,  2984.9355836 ,    -2.10936435, ...,
          7502.35286093,     0.        ,     2.35634253],
        [   -6.98330019,  2717.67947159,    -2.09832096, ...,
          7502.35286093,     0.        ,     3.14159265],
        [  -15.34863803,  2392.80886967,    -2.08497689, ...,
          7502.35286093,     0.        ,     0.10739033],
        ..., 
        [  -13.91101227,   302.73640421,     2.13345547, ...,
          7175.74026691,     1.        ,     1.57152228],
        [   -5.9185812 ,   109.76890563,     1.75122978, ...,
          7061.38043528,     1.        ,     1.58912931],
        [   -0.75446652,    12.29161713,     0.7305427 , ...,
          6947.02060366,     1.        ,     1.60438226]],

       [[   -2.50753377,  2975.19269698,    -4.0385272 , ...,
          7489.59056719,     0.        ,     2.54755568],
        [  -18.28727901,  2714.46589555,    -4.01783643, ...,
          7489.59056719,     0.        ,     3.14159265],
        [  -33.97765382,  2398.4502586 ,    -3.99291504, ...,
          7489.59056719,     0.        ,     0.13398393],
        ..., 
        [  -39.03421582,   289.26508854,     6.12669665, ...,
          7165.55467075,     1.        ,     1.57305988],
        [  -16.57317858,   104.71065493,     5.01726591, ...,
          7053.54116118,     1.        ,     1.62487159],
        [   -2.10680312,    11.70210534,     2.08410976, ...,
          6941.52765161,     1.        ,     1.66894146]],

       ..., 
       [[   48.84196277,  3085.38807286,    50.56629068, ...,
          7434.56286319,     1.        ,     2.90079384],
        [  183.51304713,  2831.14282863,    12.96426762, ...,
          7312.87656282,     1.        ,     2.75542515],
        [  197.21362743,  2557.15209301,    -0.38862523, ...,
          7267.92079564,     0.        ,     0.40687083],
        ..., 
        [   99.06799745,   348.78331662,   -14.27758186, ...,
          7002.09446017,     1.        ,     1.57049278],
        [   42.03463163,   125.56765335,   -11.73816476, ...,
          6880.4081598 ,     1.        ,     1.45676659],
        [    5.32508861,    13.94457823,    -4.85319592, ...,
          6758.72185944,     1.        ,     1.36406984]],

       [[   59.32330925,  3071.99658289,    53.96202807, ...,
          7442.33758824,     1.        ,     2.89790249],
        [  208.43801039,  2811.30897894,    16.46048523, ...,
          7320.81987269,     1.        ,     2.75211364],
        [  226.64276811,  2536.69498061,    -1.77435693, ...,
          7259.05676717,     0.        ,     0.40687083],
        ..., 
        [  101.78602702,   347.69934761,   -14.82240373, ...,
          6996.87781075,     1.        ,     1.56512855],
        [   42.99237998,   125.14432262,   -12.05988836, ...,
          6875.3600952 ,     1.        ,     1.45145924],
        [    5.42918483,    13.89424313,    -4.95846171, ...,
          6753.84237965,     1.        ,     1.35886785]],

       [[   64.91364526,  3053.08441421,    58.41893062, ...,
          7445.1134557 ,     1.        ,     2.90367791],
        [  233.51158743,  2793.47114828,    20.52081645, ...,
          7322.58665714,     1.        ,     2.75709053],
        [  257.01417355,  2524.90294009,    -3.36512749, ...,
          7241.72283503,     0.        ,     0.40687083],
        ..., 
        [  104.47319209,   354.1008801 ,   -15.13657589, ...,
          6989.78404001,     1.        ,     1.56326704],
        [   44.05406114,   127.44900894,   -12.27032355, ...,
          6867.25724145,     1.        ,     1.44975801],
        [    5.55652453,    14.15075402,    -5.03434087, ...,
          6744.73044289,     1.        ,     1.3574727 ]]])

In [8]:



Out[8]:
(8000, 21, 7)

In [9]:
shape = (8000, 11, 8)
ntraj, nnodes, nfeat = shape

trajs = np.empty(shape)
for i in range(ntraj):
    tf, sb, cb, s, c = prob.Decode(ds[i])
    t = np.linspace(0, tf, nnodes).reshape(nnodes,1)
    trajdp = hstack((s,c,t))
    trajs[i,:,:] = trajdp
np.save('../../Data/Mars/HSS_10_Traj', trajs)

In [19]:
trajs[:,0,1]


Out[19]:
array([ 3000.        ,  2984.9355836 ,  2975.19269698, ...,  3085.38807286,
        3071.99658289,  3053.08441421])

In [ ]:
plt.close('all')
plt.plot(trajs[:,:,0], trajs[:,:,1],'.')
plt.show()

In [ ]:
np.save('../../Data/Mars/HSS_10_Alpha_Full_Tensor', dpts)

In [ ]:
dpts = np.load('../../Data/Mars/HSS_10_Alpha_Full_Tensor.npy')
dpts[0,:,4]

In [ ]:
dptss = np.empty(shape=(0,7))
for traj in dpts:
    dptss = np.vstack((dptss,traj))
dptss

In [ ]:
np.savetxt('../../Data/Mars/HSS_10_Alpha_Full_Tensor_Text',dptss)

In [ ]:
plt.close('all')
for traj in dpts:
    plt.subplot(121)
    plt.plot(traj[:,0], traj[:,1],'.')
    plt.axes().set_aspect('equal', 'datalim')
    plt.subplot(122)
    plt.plot(traj[:,2], traj[:,3],'.')
plt.show()

In [25]:
from ML import MLP
net = MLP('../../Data/Mars/Nets/Mars_Regular_20x4')

In [34]:
data = np.load('../../Data/Mars/HSS_10_Alpha_dps.npy')


Out[34]:
0.0

In [27]:
iin = [0,1,2,3,4]
iout = [5,6]
layers = [20]*4

In [28]:
net.build(data, iin, iout, layers)

In [ ]:
import tensorflow as tf
sess = tf.Session()
dfp = pd.DataFrame(net.predict(data[:,iin], sess), columns = ['$u_p$', '$\\theta_p$'])

In [ ]:
df = pd.DataFrame(data, columns = ['$x$', '$y$', '$v_x$', '$v_y$', '$m$', '$u$', '$\\theta$'])

In [ ]:
from Trajectory import Point_Lander_Drag

Controlled Trajectory


In [35]:
# Random state samples
test_si = data[]
test_si = data[np.random.choice(range(len(data)), 10),0:5]
si = test_si[0]


  File "<ipython-input-35-fd1d0c6b1fbe>", line 2
    test_si = data[]
                   ^
SyntaxError: invalid syntax

In [ ]:
model = Point_Lander_Drag(si)
model.controller = Neural(model, 'HSS_10_Alpha', (20,4)).Control

In [ ]:
# Time should not matter much
tf = 50
# The resolution of the integration
nnodes = 200

In [ ]:
# We now propagate the model with the trained neural network
sl, cl = [], []
for si in test_si:
    s, c = model.Propagate.Neural(si, tf, nnodes, False)
    sl.append(s)
    cl.append(c)

In [ ]:
plt.close('all')
for s,c in zip(sl, cl):
    gi = s[:,1]>0
    plt.figure(1)
    plt.plot(s[gi,0], s[gi,1])
    plt.axes().set_aspect('equal', 'datalim')
    plt.xlim(model.silb[0], model.siub[0])
    plt.ylim(model.slb[1], model.siub[1])
    plt.xlabel('Cross-Range [m]')
    plt.ylabel('Altitude [m]')
    plt.title('Neuro-Controlled Trajectories (20x4 Network)')
    #plt.savefig('../../Data/Figs/New/20x4_Traj.pdf', format='pdf',transparent=True, bbox_inches='tight')
    plt.figure(2)
    plt.plot(s[s[:,4]>6000,4])
    plt.ylabel('Mass [kg]')
    plt.xlabel('Node Index')
    plt.title('Fuel Expenditure (20x4 Network)')
    #plt.savefig('../../Data/Figs/New/20x4_Fuel.pdf', format='pdf',transparent=True, bbox_inches='tight')
    plt.figure(3)
    plt.plot(c[:,0],'.')
    plt.xlabel('Node Index')
    plt.ylabel('Thrust Throttle')
    plt.title('Control Profile (20x4 Network)')
    #plt.savefig('../../Data/Figs/New/20x4_Throttle.pdf', format='pdf',transparent=True, bbox_inches='tight')
    plt.figure(4)
    #plt.plot(s[:,2])
    plt.plot(s[:,3])
    plt.xlabel('Node Index')
    plt.ylabel('Velocity [m/s]')
    plt.title('Velocity Profile (20x4 Network)')
    #plt.savefig('../../Data/Figs/New/20x4_Throttle.pdf', format='pdf',transparent=True, bbox_inches='tight')
    
plt.show()

In [ ]:
data.shape

In [ ]: