Hexamind/iPADS
1
1import time2import streamlit as st3from params import *4from stable_baselines3 import SAC5import pandas as pd6import pydeck as pdk7from TwoDimEnv import TwoDimEnv8 9import gymnasium as gym10 11def run_episode(env, model, lat_tg, lon_tg, rho_init=RHO_INIT, theta_init=THETA_INIT, zed=Z_INIT):12 '''13 runs the episode given an observation init ... and an env and a model14 :param env: TwoDimEnv15 :param model:16 :param lat_tg: lattitude de la target (0,0)17 :param lon_tg: longitude de la target (0,0)18 :param rho_init:19 :param theta_init:20 :param zed:21 :return:22 '''23 obs = env.reset(training=False, rho_init=rho_init, theta_init=theta_init, z_init=zed)24 step = 025 lat, lon = rhotheta_to_latlon(MOVE_TO_METERS * rho_init, theta_init, lat_tg, lon_tg)26 path = [[lon, lat, MOVE_TO_METERS * zed]]27 traj = [lat, lon, MOVE_TO_METERS * zed]28 while step < zed:29 step += 130 action, _ = model.predict(obs, deterministic=True)31 obs, _, _, _ = env.step(action)32 rho = obs[0] * env.space_limits33 theta = obs[1] * 2 * PI34 lat, lon = rhotheta_to_latlon(MOVE_TO_METERS*rho, theta, lat_tg, lon_tg)35 traj = np.vstack((traj, [lat, lon, MOVE_TO_METERS*(zed - step)]))36 path.append([lon, lat, MOVE_TO_METERS*(zed - step)])37 df_col = pd.DataFrame(traj, columns=['lat', 'lon', 'zed'])38 df_path = pd.DataFrame([{39 'color': [0, 0, 200, 120],40 'path': path41 }])42 return df_path, df_col43 44def rhotheta_to_latlon(rho, theta, lat_tg, lon_tg):45 '''46 transforms polar coordinates into lat, lon47 :param rho:48 :param theta:49 :param lat_tg: latitude de la target (0,0)50 :param lon_tg: longitude de la target (0,0)51 :return:52 '''53 z = rho * np.exp(1j * theta)54 lat = np.imag(z)*360/(40075*1000) + lat_tg55 lon = np.real(z)*360/(40075*1000*np.cos(PI/180*lat)) + lon_tg56 return lat, lon57 58 59def get_layers(df, df_past, df_target, df_path, df_col):60 '''61 renders the layers to be displayed with df in different formats as entries62 :param df:63 :param df_past:64 :param df_target:65 :param df_path:66 :param df_col:67 :return:68 '''69 return [70 pdk.Layer(71 'ScatterplotLayer',72 data=df,73 get_position='[lon, lat]',74 get_color='[200, 30, 0, 160, 40]',75 get_radius=10,76 ),77 pdk.Layer(78 'ScatterplotLayer',79 data=df_target,80 get_position='[lon, lat]',81 get_color='[200, 30, 0]',82 get_radius=65,83 ),84 pdk.Layer(85 'ScatterplotLayer',86 data=df_target,87 get_position='[lon, lat]',88 get_color='[255, 255, 255]',89 get_radius=45,90 ),91 pdk.Layer(92 'ScatterplotLayer',93 data=df_target,94 get_position='[lon, lat]',95 get_color='[0, 0, 200]',96 get_radius=25,97 ),98 pdk.Layer(99 'ScatterplotLayer',100 data=df_past,101 get_position='[lon, lat]',102 get_color='[200, 30, 0, 40]',103 get_radius=10,104 ),105 pdk.Layer(106 type="PathLayer",107 data=df_path,108 pickable=True,109 get_color="color",110 width_scale=20,111 width_min_pixels=1,112 get_path="path",113 get_width=1,114 ),115 pdk.Layer(116 type="ColumnLayer",117 data=df_col,118 get_position=['lon', 'lat'],119 get_elevation="zed",120 elevation_scale=1,121 radius=10,122 get_fill_color=[160, 20, 0, 10],123 pickable=True,124 auto_highlight=True125 ),126 ]127 128 129def show():130 '''131 shows the i-PADS in Streamlit132 :return:133 '''134 env = TwoDimEnv()135 model = SAC.load("longModel")136 st.title('Intelligent PADS by hexamind')137 st.write('This is a quick demo of an autonomous Parachute (Precision Air Delivery System) controlled by Reinforcement learning. ')138 st.text('<- Set the starting point')139 140 st.sidebar.write("Where do you want the parachute to start from?")141 rho = st.sidebar.slider('What distance? (in m)', 0, 3000, 1500) / MOVE_TO_METERS142 theta = 2*PI/360 * st.sidebar.slider('What angle?', 0, 360, 90)143 zed = int(st.sidebar.slider('What elevation? (in m)', 0, 1200, 600) / MOVE_TO_METERS)144 145 location = st.sidebar.radio("Location", ['San Francisco', 'Paris', 'Puilaurens'])146 lat_tg = LOC[location]['lat']147 lon_tg = LOC[location]['lon']148 df_path, df_col = run_episode(env, model, lat_tg, lon_tg, rho_init=rho, theta_init=theta, zed=zed)149 st.sidebar.write(150 'If you like to play, you will probably find some starting points where the parachute is out of control :) '151 'No worries, we have plenty more efficient models at www.hexamind.ai ')152 153 df_target = pd.DataFrame({'lat': [lat_tg], 'lon': [lon_tg]})154 deck_map = st.empty()155 pitch = st.slider('pitch', 0, 100, 50)156 initial_view_state = pdk.ViewState(157 latitude=lat_tg,158 longitude=lon_tg,159 zoom=12,160 pitch=pitch161 )162 deck_map.pydeck_chart(pdk.Deck(163 map_style='mapbox://styles/mapbox/light-v9',164 initial_view_state=initial_view_state165 ))166 df_pathi = df_path.copy()167 for i in range(zed):168 df_pathi['path'][0] = df_path['path'][0][0:i+1]169 layers = get_layers(df_col[i:i+1], df_col[0:i], df_target, df_pathi, df_col[0:i+1])170 deck_map.pydeck_chart(pdk.Deck(171 map_style='mapbox://styles/mapbox/light-v9',172 initial_view_state=initial_view_state,173 layers=layers174 ))175 time.sleep(TIMESLEEP)176 177 178show()179 180# to uncomment for debug181#show_print()182 