-
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
Showing
7 changed files
with
278 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
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,234 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 146, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"C:\\Users\\ktiju\\anaconda3\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", | ||
" _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n", | ||
"C:\\Users\\ktiju\\anaconda3\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", | ||
" _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n", | ||
"C:\\Users\\ktiju\\anaconda3\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", | ||
" _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n", | ||
"C:\\Users\\ktiju\\anaconda3\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", | ||
" _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n", | ||
"C:\\Users\\ktiju\\anaconda3\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", | ||
" _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n", | ||
"C:\\Users\\ktiju\\anaconda3\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", | ||
" np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from hashlib import md5\n", | ||
"import gym\n", | ||
"import mujoco_py\n", | ||
"from stable_baselines3.common.cmd_util import make_atari_env, make_vec_env" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 271, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"env = make_vec_env(\"Swimmer-v2\", 4)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 272, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"obs = np.array(env.reset())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 435, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([4., 4., 0., 0., 4., 0., 0., 0., 0., 1., 3., 0., 0., 0., 0., 0., 0.,\n", | ||
" 0., 0.])" | ||
] | ||
}, | ||
"execution_count": 435, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"tree.tree" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 296, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy\n", | ||
"# SumTree\n", | ||
"# a binary tree data structure where the parent’s value is the sum of its children\n", | ||
"class SumTree:\n", | ||
" write = 0\n", | ||
" def __init__(self, capacity):\n", | ||
" self.capacity = capacity\n", | ||
" self.tree = numpy.zeros(2 * capacity - 1)\n", | ||
" self.data = numpy.zeros(capacity, dtype=object)\n", | ||
" self.n_entries = 0\n", | ||
" self.pending_idx = set()\n", | ||
"\n", | ||
" # update to the root node\n", | ||
" def _propagate(self, idx, change):\n", | ||
" parent = (idx - 1) // 2\n", | ||
" self.tree[parent] += change\n", | ||
" if parent != 0:\n", | ||
" self._propagate(parent, change)\n", | ||
"\n", | ||
" # find sample on leaf node\n", | ||
" def _retrieve(self, idx, s):\n", | ||
" left = 2 * idx + 1\n", | ||
" right = left + 1\n", | ||
"\n", | ||
" if left >= len(self.tree):\n", | ||
" return idx\n", | ||
"\n", | ||
" if s <= self.tree[left]:\n", | ||
" return self._retrieve(left, s)\n", | ||
" else:\n", | ||
" return self._retrieve(right, s - self.tree[left])\n", | ||
"\n", | ||
" def total(self):\n", | ||
" return self.tree[0]\n", | ||
"\n", | ||
" # store priority and sample\n", | ||
" def add(self, p, data):\n", | ||
" idx = self.write + self.capacity - 1\n", | ||
" self.pending_idx.add(idx)\n", | ||
"\n", | ||
" self.data[self.write] = data\n", | ||
" self.update(idx, p)\n", | ||
"\n", | ||
" self.write += 1\n", | ||
" if self.write >= self.capacity:\n", | ||
" self.write = 0\n", | ||
"\n", | ||
" if self.n_entries < self.capacity:\n", | ||
" self.n_entries += 1\n", | ||
"\n", | ||
" # update priority\n", | ||
" def update(self, idx, p):\n", | ||
" if idx not in self.pending_idx:\n", | ||
" return\n", | ||
" self.pending_idx.remove(idx)\n", | ||
" change = p - self.tree[idx]\n", | ||
" self.tree[idx] = p\n", | ||
" self._propagate(idx, change)\n", | ||
"\n", | ||
" # get priority and sample\n", | ||
" def get(self, s):\n", | ||
" idx = self._retrieve(0, s)\n", | ||
" dataIdx = idx - self.capacity + 1\n", | ||
" self.pending_idx.add(idx)\n", | ||
" return (idx, self.tree[idx], dataIdx)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 408, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tree = SumTree(10)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 412, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tree.add(3, (\"s2\", \"a2\", \"r2\"))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 413, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([('s1', 'a1', 'r1'), ('s2', 'a2', 'r2'), 0, 0, 0, 0, 0, 0, 0, 0],\n", | ||
" dtype=object)" | ||
] | ||
}, | ||
"execution_count": 413, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"tree.data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 434, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"(9, 1.0, 0)" | ||
] | ||
}, | ||
"execution_count": 434, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"tree.get(1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Binary file not shown.
Binary file not shown.
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
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