forked from udacity/deep-reinforcement-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexis Cook
committed
Aug 8, 2018
1 parent
b23879a
commit a8cb50c
Showing
4 changed files
with
485 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Continuous Control\n", | ||
"\n", | ||
"---\n", | ||
"\n", | ||
"In this notebook, you will learn how to use the Unity ML-Agents environment for the second project of the [Deep Reinforcement Learning Nanodegree](https://www.udacity.com/course/deep-reinforcement-learning-nanodegree--nd893) program.\n", | ||
"\n", | ||
"### 1. Start the Environment\n", | ||
"\n", | ||
"We begin by importing the necessary packages. If the code cell below returns an error, please revisit the project instructions to double-check that you have installed [Unity ML-Agents](https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Installation.md) and [NumPy](http://www.numpy.org/)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from unityagents import UnityEnvironment\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Next, we will start the environment! **_Before running the code cell below_**, change the `file_name` parameter to match the location of the Unity environment that you downloaded.\n", | ||
"\n", | ||
"- **Mac**: `\"path/to/Reacher.app\"`\n", | ||
"- **Windows** (x86): `\"path/to/Reacher_Windows_x86/Reacher.exe\"`\n", | ||
"- **Windows** (x86_64): `\"path/to/Reacher_Windows_x86_64/Reacher.exe\"`\n", | ||
"- **Linux** (x86): `\"path/to/Reacher_Linux/Reacher.x86\"`\n", | ||
"- **Linux** (x86_64): `\"path/to/Reacher_Linux/Reacher.x86_64\"`\n", | ||
"- **Linux** (x86, headless): `\"path/to/Reacher_Linux_NoVis/Reacher.x86\"`\n", | ||
"- **Linux** (x86_64, headless): `\"path/to/Reacher_Linux_NoVis/Reacher.x86_64\"`\n", | ||
"\n", | ||
"For instance, if you are using a Mac, then you downloaded `Reacher.app`. If this file is in the same folder as the notebook, then the line below should appear as follows:\n", | ||
"```\n", | ||
"env = UnityEnvironment(file_name=\"Reacher.app\")\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"env = UnityEnvironment(file_name='...')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Environments contain **_brains_** which are responsible for deciding the actions of their associated agents. Here we check for the first brain available, and set it as the default brain we will be controlling from Python." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# get the default brain\n", | ||
"brain_name = env.brain_names[0]\n", | ||
"brain = env.brains[brain_name]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 2. Examine the State and Action Spaces\n", | ||
"\n", | ||
"In this environment, a double-jointed arm can move to target locations. A reward of `+0.1` is provided for each step that the agent's hand is in the goal location. Thus, the goal of your agent is to maintain its position at the target location for as many time steps as possible.\n", | ||
"\n", | ||
"The observation space consists of `33` variables corresponding to position, rotation, velocity, and angular velocities of the arm. Each action is a vector with four numbers, corresponding to torque applicable to two joints. Every entry in the action vector must be a number between `-1` and `1`.\n", | ||
"\n", | ||
"Run the code cell below to print some information about the environment." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# reset the environment\n", | ||
"env_info = env.reset(train_mode=True)[brain_name]\n", | ||
"\n", | ||
"# number of agents\n", | ||
"num_agents = len(env_info.agents)\n", | ||
"print('Number of agents:', num_agents)\n", | ||
"\n", | ||
"# size of each action\n", | ||
"action_size = brain.vector_action_space_size\n", | ||
"print('Size of each action:', action_size)\n", | ||
"\n", | ||
"# examine the state space \n", | ||
"states = env_info.vector_observations\n", | ||
"state_size = states.shape[1]\n", | ||
"print('There are {} agents. Each observes a state with length: {}'.format(states.shape[0], state_size))\n", | ||
"print('The state for the first agent looks like:', states[0])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 3. Take Random Actions in the Environment\n", | ||
"\n", | ||
"In the next code cell, you will learn how to use the Python API to control the agent and receive feedback from the environment.\n", | ||
"\n", | ||
"Once this cell is executed, you will watch the agent's performance, if it selects an action at random with each time step. A window should pop up that allows you to observe the agent, as it moves through the environment. \n", | ||
"\n", | ||
"Of course, as part of the project, you'll have to change the code so that the agent is able to use its experience to gradually choose better actions when interacting with the environment!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"env_info = env.reset(train_mode=False)[brain_name] # reset the environment \n", | ||
"states = env_info.vector_observations # get the current state (for each agent)\n", | ||
"scores = np.zeros(num_agents) # initialize the score (for each agent)\n", | ||
"while True:\n", | ||
" actions = np.random.randn(num_agents, action_size) # select an action (for each agent)\n", | ||
" actions = np.clip(actions, -1, 1) # all actions between -1 and 1\n", | ||
" env_info = env.step(actions)[brain_name] # send all actions to tne environment\n", | ||
" next_states = env_info.vector_observations # get next state (for each agent)\n", | ||
" rewards = env_info.rewards # get reward (for each agent)\n", | ||
" dones = env_info.local_done # see if episode finished\n", | ||
" scores += env_info.rewards # update the score (for each agent)\n", | ||
" states = next_states # roll over states to next time step\n", | ||
" if np.any(dones): # exit loop if episode finished\n", | ||
" break\n", | ||
"print('Total score (averaged over agents) this episode: {}'.format(np.mean(scores)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"When finished, you can close the environment." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"env.close()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 4. It's Your Turn!\n", | ||
"\n", | ||
"Now it's your turn to train your own agent to solve the environment! When training the environment, set `train_mode=True`, so that the line for resetting the environment looks like the following:\n", | ||
"```python\n", | ||
"env_info = env.reset(train_mode=True)[brain_name]\n", | ||
"```" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.