/RL_Quadcopter

Quadcopter control based on Reinforcement Learning

Primary LanguageHTML

Project: Train a Quadcopter How to Fly¶

Design an agent to fly a quadcopter, and then train it using a reinforcement learning algorithm of your choice!

Try to apply the techniques you have learnt, but also feel free to come up with innovative ideas and test them.

Instructions

Take a look at the files in the directory to better understand the structure of the project.

  • task.py: Define your task (environment) in this file.
  • agents/: Folder containing reinforcement learning agents.
    • policy_search.py: A sample agent has been provided here.
    • agent.py: Develop your agent here.
  • physics_sim.py: This file contains the simulator for the quadcopter. DO NOT MODIFY THIS FILE.

For this project, you will define your own task in task.py. Although we have provided a example task to get you started, you are encouraged to change it. Later in this notebook, you will learn more about how to amend this file.

You will also design a reinforcement learning agent in agent.py to complete your chosen task.

You are welcome to create any additional files to help you to organize your code. For instance, you may find it useful to define a model.py file defining any needed neural network architectures.

Controlling the Quadcopter

We provide a sample agent in the code cell below to show you how to use the sim to control the quadcopter. This agent is even simpler than the sample agent that you'll examine (in agents/policy_search.py) later in this notebook!

The agent controls the quadcopter by setting the revolutions per second on each of its four rotors. The provided agent in the Basic_Agent class below always selects a random action for each of the four rotors. These four speeds are returned by the act method as a list of four floating-point numbers.

For this project, the agent that you will implement in agents/agent.py will have a far more intelligent method for selecting actions!

In [3]:
import numpy as np
np.random.seed(0)
In [51]:
import random

class Basic_Agent(): def init(self, task): self.task = task

<span class="k">def</span> <span class="nf">act</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="n">new_thrust</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">gauss</span><span class="p">(</span><span class="mf">450.</span><span class="p">,</span> <span class="mf">25.</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">new_thrust</span> <span class="o">+</span> <span class="n">random</span><span class="o">.</span><span class="n">gauss</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)]</span>

Run the code cell below to have the agent select actions to control the quadcopter.

Feel free to change the provided values of runtime, init_pose, init_velocities, and init_angle_velocities below to change the starting conditions of the quadcopter.

The labels list below annotates statistics that are saved while running the simulation. All of this information is saved in a text file data.txt and stored in the dictionary results.

In [52]:
%load_ext autoreload
%autoreload 2

import csv from tasks.takeoff import Task from agents.policy_search import PolicySearch_Agent

# Modify the values below to give the quadcopter a different starting position. runtime = 5000. # time limit of the episode init_pose = np.array([0., 0., 10., 0., 0., 0.]) # initial pose init_velocities = np.array([0., 0., 0.]) # initial velocities init_angle_velocities = np.array([0., 0., 0.]) # initial angle velocities file_output = 'data.txt' # file name for saved results

# Setup task = Task(init_pose, init_velocities, init_angle_velocities, runtime) agent = PolicySearch_Agent(task) done = False labels = ['time', 'x', 'y', 'z', 'phi', 'theta', 'psi', 'x_velocity', 'y_velocity', 'z_velocity', 'phi_velocity', 'theta_velocity', 'psi_velocity', 'rotor_speed1', 'rotor_speed2', 'rotor_speed3', 'rotor_speed4'] results = {x : [] for x in labels}

# Run the simulation, and save the results. with open(file_output, 'w') as csvfile: writer = csv.writer(csvfile) writer.writerow(labels) state = agent.reset_episode() total_reward = 0 while True: rotor_speeds = agent.act(state) next_state, reward, done = task.step(rotor_speeds) to_write = [task.sim.time] + list(task.sim.pose) + list(task.sim.v) + list(task.sim.angular_v) + list(rotor_speeds) for ii in range(len(labels)): results[labels[ii]].append(to_write[ii]) writer.writerow(to_write) total_reward += reward state = next_state if done: print("Total episode reward : {}".format(total_reward)) total_reward = 0 break

The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
Setting default init pose
Total episode reward : 181.00680565712662

Run the code cell below to visualize how the position of the quadcopter evolved during the simulation.

In [53]:
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(results['time'], results['x'], label='x') plt.plot(results['time'], results['y'], label='y') plt.plot(results['time'], results['z'], label='z') plt.legend() _ = plt.ylim()

The next code cell visualizes the velocity of the quadcopter.

In [54]:
plt.plot(results['time'], results['x_velocity'], label='x_hat')
plt.plot(results['time'], results['y_velocity'], label='y_hat')
plt.plot(results['time'], results['z_velocity'], label='z_hat')
plt.legend()
_ = plt.ylim()

Next, you can plot the Euler angles (the rotation of the quadcopter over the $x$-, $y$-, and $z$-axes),

In [55]:
plt.plot(results['time'], results['phi'], label='phi')
plt.plot(results['time'], results['theta'], label='theta')
plt.plot(results['time'], results['psi'], label='psi')
plt.legend()
_ = plt.ylim()

before plotting the velocities (in radians per second) corresponding to each of the Euler angles.

In [56]:
plt.plot(results['time'], results['phi_velocity'], label='phi_velocity')
plt.plot(results['time'], results['theta_velocity'], label='theta_velocity')
plt.plot(results['time'], results['psi_velocity'], label='psi_velocity')
plt.legend()
_ = plt.ylim()

Finally, you can use the code cell below to print the agent's choice of actions.

In [57]:
plt.plot(results['time'], results['rotor_speed1'], label='Rotor 1 revolutions / second')
plt.plot(results['time'], results['rotor_speed2'], label='Rotor 2 revolutions / second')
plt.plot(results['time'], results['rotor_speed3'], label='Rotor 3 revolutions / second')
plt.plot(results['time'], results['rotor_speed4'], label='Rotor 4 revolutions / second')
plt.legend()
_ = plt.ylim()

When specifying a task, you will derive the environment state from the simulator. Run the code cell below to print the values of the following variables at the end of the simulation:

  • task.sim.pose (the position of the quadcopter in ($x,y,z$) dimensions and the Euler angles),
  • task.sim.v (the velocity of the quadcopter in ($x,y,z$) dimensions), and
  • task.sim.angular_v (radians/second for each of the three Euler angles).
In [58]:
# the pose, velocity, and angular velocity of the quadcopter at the end of the episode
print(task.sim.pose)
print(task.sim.v)
print(task.sim.angular_v)
[-0.06412176 -0.56879505  0.          3.34902336  3.07354712  0.        ]
[  0.65015094  -0.33416084 -13.98148737]
[7.69588962 3.31683265 0.        ]

In the sample task in task.py, we use the 6-dimensional pose of the quadcopter to construct the state of the environment at each timestep. However, when amending the task for your purposes, you are welcome to expand the size of the state vector by including the velocity information. You can use any combination of the pose, velocity, and angular velocity - feel free to tinker here, and construct the state to suit your task.

The Task

A sample task has been provided for you in task.py. Open this file in a new window now.

The __init__() method is used to initialize several variables that are needed to specify the task.

  • The simulator is initialized as an instance of the PhysicsSim class (from physics_sim.py).
  • Inspired by the methodology in the original DDPG paper, we make use of action repeats. For each timestep of the agent, we step the simulation action_repeats timesteps. If you are not familiar with action repeats, please read the Results section in the DDPG paper.
  • We set the number of elements in the state vector. For the sample task, we only work with the 6-dimensional pose information. To set the size of the state (state_size), we must take action repeats into account.
  • The environment will always have a 4-dimensional action space, with one entry for each rotor (action_size=4). You can set the minimum (action_low) and maximum (action_high) values of each entry here.
  • The sample task in this provided file is for the agent to reach a target position. We specify that target position as a variable.

The reset() method resets the simulator. The agent should call this method every time the episode ends. You can see an example of this in the code cell below.

The step() method is perhaps the most important. It accepts the agent's choice of action rotor_speeds, which is used to prepare the next state to pass on to the agent. Then, the reward is computed from get_reward(). The episode is considered done if the time limit has been exceeded, or the quadcopter has travelled outside of the bounds of the simulation.

In the next section, you will learn how to test the performance of an agent on this task.

The Agent

The sample agent given in agents/policy_search.py uses a very simplistic linear policy to directly compute the action vector as a dot product of the state vector and a matrix of weights. Then, it randomly perturbs the parameters by adding some Gaussian noise, to produce a different policy. Based on the average reward obtained in each episode (score), it keeps track of the best set of parameters found so far, how the score is changing, and accordingly tweaks a scaling factor to widen or tighten the noise.

Run the code cell below to see how the agent performs on the sample task.

In [59]:
import sys
import pandas as pd
from agents.policy_search import PolicySearch_Agent
from tasks.takeoff import Task

num_episodes = 500 target_pos = np.array([0., 0., 500.]) task = Task(target_pos=target_pos) agent = PolicySearch_Agent(task)

for i_episode in range(1, num_episodes+1): state = agent.reset_episode() # start a new episode while True: action = agent.act(state) next_state, reward, done = task.step(action) agent.step(reward, done) state = next_state if done: print("\rEpisode = {:4d}, score = {:7.3f} (best = {:7.3f}), noise_scale = {}".format( i_episode, agent.score, agent.best_score, agent.noise_scale), end="") # [debug] break sys.stdout.flush()

Episode =  500, score =   3.628 (best =   4.588), noise_scale = 3.255

This agent should perform very poorly on this task. And that's where you come in!

Define the Task, Design the Agent, and Train Your Agent!

Amend task.py to specify a task of your choosing. If you're unsure what kind of task to specify, you may like to teach your quadcopter to takeoff, hover in place, land softly, or reach a target pose.

After specifying your task, use the sample agent in agents/policy_search.py as a template to define your own agent in agents/agent.py. You can borrow whatever you need from the sample agent, including ideas on how you might modularize your code (using helper methods like act(), learn(), reset_episode(), etc.).

Note that it is highly unlikely that the first agent and task that you specify will learn well. You will likely have to tweak various hyperparameters and the reward function for your task until you arrive at reasonably good behavior.

As you develop your agent, it's important to keep an eye on how it's performing. Use the code above as inspiration to build in a mechanism to log/save the total rewards obtained in each episode to file. If the episode rewards are gradually increasing, this is an indication that your agent is learning.

DDPG Actor Critic Agent trained on TakeOff Task

In [4]:
import sys
import pandas as pd
from agents.agent import DDPG
from tasks.takeoff import Task
import csv

num_episodes = 500 target_pos = np.array([0., 0., 100.]) task = Task(target_pos=target_pos) agent = DDPG(task) worst_score = 1000000 best_score = -1000000. reward_log = "reward.txt"

reward_labels = ['episode', 'reward'] reward_results = {x : [] for x in reward_labels}

/opt/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
In [5]:
for i_episode in range(1, num_episodes+1):
    state = agent.reset_episode() # start a new episode
    score = 0
    while True:
        action = agent.act(state) 
        next_state, reward, done = task.step(action)
        agent.step(action, reward, next_state, done)
        state = next_state
        score += reward
        best_score = max(best_score , score)
        worst_score = min(worst_score , score)
        if done:
            print("\rEpisode = {:4d}, score = {:7.3f} (best = {:7.3f} , worst = {:7.3f})".format(
               i_episode, score, best_score, worst_score), end="")
            break
    reward_results['episode'].append(i_episode)
    reward_results['reward'].append(score)
    sys.stdout.flush()
Episode =  500, score = 568.070 (best = 574.061 , worst =   6.439)

Plot the Rewards

Once you are satisfied with your performance, plot the episode rewards, either from a single run, or averaged over multiple runs.

In [7]:
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(reward_results['episode'], reward_results['reward'], label='reward/episode') plt.legend() _ = plt.ylim()

Performance Demonstration

Lets now see how well the agent actually performs in reference to the above reward graph. For this, we will let the agent perform for one episode in the environment without learning anything new.

In [42]:
%load_ext autoreload
%autoreload 2

import csv from tasks.takeoff import Task

# Modify the values below to give the quadcopter a different starting position. runtime = 5000. # time limit of the episode init_pose = np.array([0., 0., 10., 0., 0., 0.]) # initial pose init_velocities = np.array([0., 0., 0.]) # initial velocities init_angle_velocities = np.array([0., 0., 0.]) # initial angle velocities file_output = 'data.txt' # file name for saved results

# Setup task = Task(init_pose, init_velocities, init_angle_velocities, runtime) done = False labels = ['time', 'x', 'y', 'z', 'phi', 'theta', 'psi', 'x_velocity', 'y_velocity', 'z_velocity', 'phi_velocity', 'theta_velocity', 'psi_velocity', 'rotor_speed1', 'rotor_speed2', 'rotor_speed3', 'rotor_speed4'] results = {x : [] for x in labels}

# Run the simulation, and save the results. state = agent.reset_episode() total_reward = 0 while True: rotor_speeds = agent.act(state) next_state, reward, done = task.step(rotor_speeds) to_write = [task.sim.time] + list(task.sim.pose) + list(task.sim.v) + list(task.sim.angular_v) + list(rotor_speeds) for ii in range(len(labels)): results[labels[ii]].append(to_write[ii]) total_reward += reward state = next_state if done: print("Total episode reward : {}".format(total_reward)) total_reward = 0 break

The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
Setting default init pose
Total episode reward : 1889.9449876359026

Run the code cell below to visualize how the position of the quadcopter evolved during the simulation.

In [43]:
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(results['time'], results['x'], label='x') plt.plot(results['time'], results['y'], label='y') plt.plot(results['time'], results['z'], label='z') plt.legend() _ = plt.ylim()

Run the code cell below to print the values of the following variables at the end of the simulation:

  • task.sim.pose (the position of the quadcopter in ($x,y,z$) dimensions and the Euler angles),
  • task.sim.v (the velocity of the quadcopter in ($x,y,z$) dimensions), and
  • task.sim.angular_v (radians/second for each of the three Euler angles).
In [44]:
# the pose, velocity, and angular velocity of the quadcopter at the end of the episode
print(task.sim.pose)
print(task.sim.v)
print(task.sim.angular_v)
[  1.61073679 115.53386858 300.           5.7041602    6.27933218
   0.        ]
[ 0.32647135 -1.43134965 18.26275314]
[-4.86588757e-01 -1.08460304e-04  0.00000000e+00]

Reflections

Question 1: Describe the task that you specified in task.py. How did you design the reward function?

Answer: I selected a fairly easier takeoff task to train the agent . For reward , I was initially using signed difference between sim.pose & target.pose which as expected lead to very poor / almost no learning as the variance of the reward very high & unbounded.
Later I moved to a non linear function 'tanh' which bounded the reward to range [-1,1] . For this to work , some scalling was done to make values meaningful for tanh function .
The final approach used was a hybrid of both approaches where I designed to calculate the absolute difference between the pose & then pass it through tanh function so that the gradient explosion problems is no more a concern.

Final Reward Fn :

reward = np.tanh(1 - 0.003*(abs(self.sim.pose[:3] - self.target_pos))).sum()

Question 2: Discuss your agent briefly, using the following questions as a guide:

  • What learning algorithm(s) did you try? What worked best for you?
  • What was your final choice of hyperparameters (such as $\alpha$, $\gamma$, $\epsilon$, etc.)?
  • What neural network architecture did you use (if any)? Specify layers, sizes, activation functions, etc.

Answer:

  • I used Deep Deterministic Policy Gradients (DDPG) for the task with same architecture and hyperparams initially. One of the well suited RL algo for continuous action space. Though the initial implementation didn't proved so promising for me, the agent was not learning at all . Later I found out even after so much hyperparameter tweaking, the main culprit was the inefficiency of the Neural Net arch which I used for both Actor & Critic .

  • The final choice of the hyperparameters are :

    • Size of minibatch from experience replay memory = 64
    • Tau (soft target update rate) = 0.001
    • Learning rate for the actor = 0.0001
    • Learning rate for the critic = 0.001
    • Gamma = 0.99
    • Capacity of experience replay memory = 1000000
  • Yes the final agent uses Neural Net arch.

    • Actor :

      • Dense(units=400) + BatchNorm + L2 Regularisation + ReLu Activation
      • Dense(units=300) + BatchNorm + L2 Regularisation + ReLu Activation
      • Dense( RandomUniform Weight initialisation ) + Sigmoid Activation
    • Crtic :

      • Same as actor for the state pathway
      • Action Pathway :
        • Dense(units=300) + L2 Regularisation + ReLu Activation
      • Combining : Add with ReLu Activation

    This stripped down arch of layers with large units actually helped the agent to learn more quickly & effectively . Also the training time decreased significantly.

Question 3: Using the episode rewards plot, discuss how the agent learned over time.

  • Was it an easy task to learn or hard?
  • Was there a gradual learning curve, or an aha moment?
  • How good was the final performance of the agent? (e.g. mean rewards over the last 10 episodes)

Answer:

  • I would consider it relatively simple control task as can be seen , initially the agent is trying all possibilities for a high reward & hence the instability & as later it has got some experience in the env, it gradually become consistent in taking actions with higher rewards.

  • Well it was kinda both. Initially it was a total frustration as I didn't have that much intuition. Made some silly arch mistakes then gradually matured it to a fairly performing agent. An idea of the above mentioned NN made this to the aha moment.....

  • Final Performance :

In [ ]:
print
plt.plot(reward_results['episode'], reward_results['reward'], label='reward/episode')
plt.legend()
_ = plt.ylim()
In [50]:
# Final Performance 
print("Final Performance (Mean Reward over last 10 episodes): {}".format(np.sum(reward_results['reward'][-10:])/10))
Final Performance (Mean Reward over last 10 episodes): 572.1351718061658

Question 4: Briefly summarize your experience working on this project. You can use the following prompts for ideas.

  • What was the hardest part of the project? (e.g. getting started, plotting, specifying the task, etc.)
  • Did you find anything interesting in how the quadcopter or your agent behaved?

Answer:

  • The hardest part for me was getting started with the concepts with continuous control task. The key concepts of Policy Gradient were obviously challenging to visualise & understand at first but later I found them more & more intuitive with each revisit.
  • One thing that caught me the most was the implications of a reward function. I was quite amazed how the inference of a simple function can vary greatly wrt to random env initialisations.