Skip to content

Instantly share code, notes, and snippets.

@islamelnabarawy
Last active November 10, 2017 05:36
Show Gist options
  • Save islamelnabarawy/3466b6aadb5002782e0de3ac6a90ae29 to your computer and use it in GitHub Desktop.
Save islamelnabarawy/3466b6aadb5002782e0de3ac6a90ae29 to your computer and use it in GitHub Desktop.
A jupyter notebook showing an issue with using minimap for camera control
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Minimap camera control issue"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook shows how using the minimap for camera control can be problematic."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let's get past the initialization and whatnot..."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import logging\n",
"import sys\n",
"\n",
"import gym\n",
"import numpy as np\n",
"\n",
"from absl import flags\n",
"\n",
"import sc2gym.envs\n",
"\n",
"from pysc2.lib import actions\n",
"from pysc2.lib import features"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"FLAGS = flags.FLAGS\n",
"_ = FLAGS(['jupyter'])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"_MINIMAP_CAMERA = features.MINIMAP_FEATURES.camera.index\n",
"_MOVE_CAMERA = actions.FUNCTIONS.move_camera.id"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"_ENV_NAME = 'SC2Game-v0'\n",
"_MAP_NAME = 'AscensiontoAiur'\n",
"_VISUALIZE = False"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"env = gym.make(_ENV_NAME)\n",
"\n",
"env.settings['map_name'] = _MAP_NAME\n",
"env.settings['visualize'] = _VISUALIZE"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"I1028 19:08:52.146734 13316 sc2_game.py:49] Episode 1 starting...\n"
]
}
],
"source": [
"obs_0 = env.reset()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Here's the actual demonstration:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have an initial observation, we can calcuate the center of the camera from the minimap camera feature:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"y, x = obs_0.observation['minimap'][_MINIMAP_CAMERA].nonzero()\n",
"center = [x.mean().astype(int), y.mean().astype(int)]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Boundaries: (50, 43), (57, 50)\n",
"Means: (53.5, 46.5)\n",
"Center: (53, 46)\n"
]
}
],
"source": [
"print('Boundaries: ({}, {}), ({}, {})'.format(x.min(), y.min(), x.max(), y.max()))\n",
"print('Means: ({}, {})'.format(x.mean(), y.mean()))\n",
"print('Center: ({}, {})'.format(*center))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try to move the camera to that point. Theoretically, nothing should happen since we're trying to move to the same point."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"obs_1, _, _, _ = env.step([_MOVE_CAMERA, center])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"y, x = obs_1.observation['minimap'][_MINIMAP_CAMERA].nonzero()\n",
"center = [x.mean().astype(int), y.mean().astype(int)]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Boundaries: (49, 42), (56, 49)\n",
"Means: (52.5, 45.5)\n",
"Center: (52, 45)\n"
]
}
],
"source": [
"print('Boundaries: ({}, {}), ({}, {})'.format(x.min(), y.min(), x.max(), y.max()))\n",
"print('Means: ({}, {})'.format(x.mean(), y.mean()))\n",
"print('Center: ({}, {})'.format(*center))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how the center shifted. If you're looking at the game, you'll notice the camera moved up at this point."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test it further by running a few more times. We'll click in the bottom right corner and start from there."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"center = [c-1 for c in env.observation_spec['minimap'][1:]]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"obs_n, _, _, _ = env.step([_MOVE_CAMERA, center])\n",
"y, x = obs_n.observation['minimap'][_MINIMAP_CAMERA].nonzero()\n",
"center = [x.mean().astype(int), y.mean().astype(int)]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Boundaries: (52, 45), (59, 52)\n",
"Means: (55.5, 48.5)\n",
"Center: (55, 48)\n"
]
}
],
"source": [
"print('Boundaries: ({}, {}), ({}, {})'.format(x.min(), y.min(), x.max(), y.max()))\n",
"print('Means: ({}, {})'.format(x.mean(), y.mean()))\n",
"print('Center: ({}, {})'.format(*center))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(54, 47) (53, 46) (52, 45) (51, 44) (50, 43)\n",
"(49, 42) (48, 41) (47, 40) (46, 39) (45, 38)\n",
"(44, 37) (43, 36) (42, 35) (41, 34) (40, 33)\n",
"(39, 32) (38, 31) (37, 30) (36, 29) (35, 28)\n",
"(34, 27) (33, 26) (32, 25) (31, 24) (30, 23)\n",
"(29, 22) (28, 21) (27, 20) (26, 19) (25, 18)\n",
"(24, 17) (23, 16) (22, 15) (21, 14) (20, 13)\n",
"(19, 12) (18, 11) (17, 10) (16, 9) (15, 8)\n",
"(14, 7) (13, 6) (12, 5) (11, 4) (10, 3)\n",
"( 9, 3) ( 8, 3) ( 7, 3) ( 6, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n",
"( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3) ( 5, 3)\n"
]
}
],
"source": [
"for n in range(100):\n",
" obs_n, _, _, _ = env.step([_MOVE_CAMERA, center])\n",
" y, x = obs_n.observation['minimap'][_MINIMAP_CAMERA].nonzero()\n",
" center = [x.mean().astype(int), y.mean().astype(int)]\n",
" print('({:2}, {:2}){}'.format(*center + [\"\\n\" if (n + 1) % 5 == 0 else \" \"]), end=\"\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"W1028 19:08:58.572007 13316 sc_process.py:183] Killing the process.\n"
]
}
],
"source": [
"env.close()\n",
"del env"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment