<jupyter_start><jupyter_text>Synchronize homework filesColab starts a new virtual machine for each jupyter notebook that the user opens up, and the virtual machine session ends after a few hours of idle time, so be aware that **we cannot save files on the virtual machine permanently**! To get around this issue, this cell saves your homework files in your own Google Drive and creates shortcuts in virtual machine that link to the files in Google Drive.<jupyter_code># Synchronize homework files (link to google drive's hw files; if hw files don't exist in google drive, download files from github) 
! rm -rf sample_data; curl -s https://raw.githubusercontent.com/mposa/MEAM517/master/sync_hw.py > sync_hw.py
from sync_hw import sync_hw
files = ["quadrotor.py", "quad_sim.py", "trajectories.py", "create_animation.py", "grid_world.py", "value_iteration.py", "discretize_system.py", "double_integrator_value_iteration.py"]
sync_hw(2, files)<jupyter_output><empty_output><jupyter_text>Problem 2 -- Trajectory TrackingThis cell simulates a quadrotor with the given controller (inside quadrotor.py) and visualizes the resultant trajectory.<jupyter_code>%matplotlib inline

"""
Simulate quadrotor
"""

import numpy as np
from math import sin, cos, pi
from scipy.integrate import solve_ivp
from trajectories import *
import matplotlib.pyplot as plt
import importlib

from quad_sim import simulate_quadrotor

# Need to reload the module to use the latest code
import quadrotor
importlib.reload(quadrotor)
from quadrotor import Quadrotor

# Weights of LQR cost
R = np.eye(2);
Q = np.diag([10, 10, 1, 1, 1, 1]);
Qf = Q;

# End time of the simulation
tf = 2*pi;

# Construct our quadrotor controller 
quadrotor = Quadrotor(Q, R, Qf, tf);

# Set quadrotor's initial state and simulate
x0 = 0.5 * np.ones((6,)) + x_d(0.0)
x, u, t = simulate_quadrotor(x0, tf, quadrotor)

"""
Create animation 
"""
import create_animation
importlib.reload(create_animation)
from create_animation import create_animation

# Number of poses to visualize 
# TODO: set this value to 60 for the final plots
n_frame = 30

anim, fig = create_animation(x, x_d, tf, n_frame)
plt.close()
anim<jupyter_output><empty_output><jupyter_text>Save the final trajectory to the colab workspaceThe figure should show up in the files browser on the left panel of Colab. If you don't see it, hit the Refresh button.<jupyter_code># Save the trajectory figure to workspace
# Remember to download the figure of either stable/unstable trajectory before overwriting the previous one
fig.savefig("quadrotor_trajectory.png", dpi=240)<jupyter_output><empty_output><jupyter_text>Problem 3 -- Gridworld Value Iteration<jupyter_code>%matplotlib inline
import importlib

import grid_world
importlib.reload(grid_world)
from grid_world import GridWorld

# Need to reload the module to use the latest code
import value_iteration
importlib.reload(value_iteration)
from value_iteration import value_iteration

world = GridWorld() 
threshold = 0.0001
gamma = 0.9

# Run value iteration and save the plot
V, u_opt = value_iteration(world, threshold, gamma, plotting = True)<jupyter_output><empty_output><jupyter_text>Problem 4 -- Quadratic Cost Value IterationThe function `run_iteration()` automatically saves the files required for submission. (Look for dV.png and du.png in the file browser on the left panel of Colab)<jupyter_code>%matplotlib inline
import importlib
import matplotlib.pyplot as plt
import double_integrator_value_iteration

# Need to reload the module to use the latest code
importlib.reload(double_integrator_value_iteration)
from double_integrator_value_iteration import run_iteration

# Run value iteration and record the following plots:
#   anim1: V_tilde_star (i.e. value function from value iteration)
#   anim2: u_tilde_star (i.e. optimal policy from value iteration)
#   anim3: V_tilde_star - V_star
#   anim4: u_tilde_star - u_star
anim1, anim2, anim3, anim4 = run_iteration()
# Visualize V_tilde_star
anim1
# Visualize u_tilde_star
anim2
# Visualize V_tilde_star - V_star
anim3
# Visualize u_tilde_star - u_star
anim4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07301046483329277, 'fraction_numerical': 0.012168410805548795, 'mean_word_length': 5.737704918032787, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '469038', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1271, 'n_words': 468, 'path': 'hw2/hw2.ipynb', 'repo_name': 'mposa/MEAM517', 'size': '4109'}