Skip to content

Instantly share code, notes, and snippets.

@FriedGil
Last active April 15, 2024 14:57
Show Gist options
  • Save FriedGil/732deafe99b4f5747267a5232e0360a8 to your computer and use it in GitHub Desktop.
Save FriedGil/732deafe99b4f5747267a5232e0360a8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install mlagents\n",
"%pip install mlagents_envs\n",
"%pip install numpy\n",
"\n",
"#Run the below line and restart your kernel if there's a protobuf error\n",
"%pip install --upgrade protobuf==3.19.0 \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy \n",
"\n",
"try:\n",
" import mlagents\n",
" print(\"ml-agents already installed\")\n",
"except ImportError:\n",
" print(\"Something is messed up\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"environments closed\n"
]
}
],
"source": [
"# Run this to make sure environments are closed\n",
"\n",
"try:\n",
" env.close()\n",
" print(\"environments closed\")\n",
"except:\n",
" print(\"nothing closed\")\n",
" pass\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from mlagents_envs.envs import ThreeDBall # or StrikersVsGoalie, Basic, ThreeDBall, (see docs)\n",
"\n",
"env = ThreeDBall.env()\n",
"num_cycles = 1000\n",
"\n",
"env.reset()\n",
"for agent in env.agent_iter(env.num_agents * num_cycles):\n",
" prev_observe, reward, done, info = env.last()\n",
" if isinstance(prev_observe, dict) and 'action_mask' in prev_observe:\n",
" action_mask = prev_observe['action_mask']\n",
" if done:\n",
" action = None\n",
" else:\n",
" action = env.action_spaces[agent].sample() # randomly choose an action for example\n",
" env.step(action)\n",
"env.close()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from mlagents_envs.environment import UnityEnvironment\n",
" \n",
"env = UnityEnvironment(file_name=r\"\"\"\"C:\\Users\\friedmang\\Downloads\\dodgeball\\ml-agents-dodgeball-env-develop\\UnityEnvironment\"\"\", seed=1, worker_id=3, side_channels=[]) #change to your pc\n",
"env.reset()\n",
"num_cycles = 1000\n",
"\n",
"for agent in env.agent_iter(env.num_agents * num_cycles):\n",
" prev_observe, reward, done, info = env.last()\n",
" if isinstance(prev_observe, dict) and 'action_mask' in prev_observe:\n",
" action_mask = prev_observe['action_mask']\n",
" if done:\n",
" action = None\n",
" else:\n",
" action = env.action_spaces[agent].sample() # randomly choose an action for example\n",
" env.step(action)\n",
"exit()"
]
}
],
"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.8.19"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment