Skip to content

Commit

Permalink
Add custom env practice
Browse files Browse the repository at this point in the history
  • Loading branch information
mhurtado13 committed May 14, 2024
1 parent 57755f8 commit 44933fe
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13,937 deletions.
86 changes: 4 additions & 82 deletions Maze_custom_env.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,9 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/marcelo.hurtado/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:174: UserWarning: \u001b[33mWARN: Future gym versions will require that `Env.reset` can be passed a `seed` instead of using `Env.seed` for resetting the environment random number generator.\u001b[0m\n",
" logger.warn(\n",
"/home/marcelo.hurtado/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:187: UserWarning: \u001b[33mWARN: Future gym versions will require that `Env.reset` can be passed `options` to allow the environment initialisation to be passed additional information.\u001b[0m\n",
" logger.warn(\n",
"/home/marcelo.hurtado/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:149: UserWarning: \u001b[33mWARN: The obs returned by the `reset()` method was expecting a tuple, actual type: <class 'numpy.ndarray'>\u001b[0m\n",
" logger.warn(f\"{pre} was expecting a tuple, actual type: {type(obs)}\")\n"
]
},
{
"ename": "AssertionError",
"evalue": "The obs returned by the `reset()` method length is not same as the observation space length, obs length: 1, space length: 2",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[6], line 9\u001b[0m\n\u001b[1;32m 1\u001b[0m maze \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m 2\u001b[0m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mS\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[1;32m 3\u001b[0m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m#\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m#\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[1;32m 4\u001b[0m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[1;32m 5\u001b[0m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m#\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m#\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mG\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[1;32m 6\u001b[0m ]\n\u001b[1;32m 8\u001b[0m env \u001b[38;5;241m=\u001b[39m gym\u001b[38;5;241m.\u001b[39mmake(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMazeGameEnv-v0\u001b[39m\u001b[38;5;124m'\u001b[39m, maze \u001b[38;5;241m=\u001b[39m maze)\n\u001b[0;32m----> 9\u001b[0m env\u001b[38;5;241m.\u001b[39mreset()\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(env\u001b[38;5;241m.\u001b[39maction_space\u001b[38;5;241m.\u001b[39msample())\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/wrappers/order_enforcing.py:42\u001b[0m, in \u001b[0;36mOrderEnforcing.reset\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Resets the environment with `kwargs`.\"\"\"\u001b[39;00m\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_reset \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m---> 42\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menv\u001b[38;5;241m.\u001b[39mreset(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/wrappers/env_checker.py:45\u001b[0m, in \u001b[0;36mPassiveEnvChecker.reset\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchecked_reset \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchecked_reset \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m env_reset_passive_checker(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menv, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 47\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menv\u001b[38;5;241m.\u001b[39mreset(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:204\u001b[0m, in \u001b[0;36menv_reset_passive_checker\u001b[0;34m(env, **kwargs)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 203\u001b[0m obs, info \u001b[38;5;241m=\u001b[39m result\n\u001b[0;32m--> 204\u001b[0m check_obs(obs, env\u001b[38;5;241m.\u001b[39mobservation_space, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreset\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 205\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\n\u001b[1;32m 206\u001b[0m info, \u001b[38;5;28mdict\u001b[39m\n\u001b[1;32m 207\u001b[0m ), \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe second element returned by `env.reset()` was not a dictionary, actual type: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(info)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 208\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:150\u001b[0m, in \u001b[0;36mcheck_obs\u001b[0;34m(obs, observation_space, method_name)\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(obs, \u001b[38;5;28mtuple\u001b[39m):\n\u001b[1;32m 149\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarn(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpre\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m was expecting a tuple, actual type: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(obs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 150\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(obs) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mlen\u001b[39m(\n\u001b[1;32m 151\u001b[0m observation_space\u001b[38;5;241m.\u001b[39mspaces\n\u001b[1;32m 152\u001b[0m ), \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpre\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m length is not same as the observation space length, obs length: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(obs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, space length: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(observation_space\u001b[38;5;241m.\u001b[39mspaces)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m sub_obs, sub_space \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(obs, observation_space\u001b[38;5;241m.\u001b[39mspaces):\n\u001b[1;32m 154\u001b[0m check_obs(sub_obs, sub_space, method_name)\n",
"\u001b[0;31mAssertionError\u001b[0m: The obs returned by the `reset()` method length is not same as the observation space length, obs length: 1, space length: 2"
]
},
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
"\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
"\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"outputs": [],
"source": [
"maze = [\n",
" ['S', '', '.','.'],\n",
Expand All @@ -84,48 +45,9 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/marcelo.hurtado/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:174: UserWarning: \u001b[33mWARN: Future gym versions will require that `Env.reset` can be passed a `seed` instead of using `Env.seed` for resetting the environment random number generator.\u001b[0m\n",
" logger.warn(\n",
"/home/marcelo.hurtado/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:187: UserWarning: \u001b[33mWARN: Future gym versions will require that `Env.reset` can be passed `options` to allow the environment initialisation to be passed additional information.\u001b[0m\n",
" logger.warn(\n",
"/home/marcelo.hurtado/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:149: UserWarning: \u001b[33mWARN: The obs returned by the `reset()` method was expecting a tuple, actual type: <class 'numpy.ndarray'>\u001b[0m\n",
" logger.warn(f\"{pre} was expecting a tuple, actual type: {type(obs)}\")\n"
]
},
{
"ename": "AssertionError",
"evalue": "The obs returned by the `reset()` method length is not same as the observation space length, obs length: 1, space length: 2",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[24], line 10\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Test custom environment \u001b[39;00m\n\u001b[1;32m 9\u001b[0m env \u001b[38;5;241m=\u001b[39m gym\u001b[38;5;241m.\u001b[39mmake(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMazeGameEnv-v0\u001b[39m\u001b[38;5;124m'\u001b[39m, maze \u001b[38;5;241m=\u001b[39m maze)\n\u001b[0;32m---> 10\u001b[0m obs \u001b[38;5;241m=\u001b[39m env\u001b[38;5;241m.\u001b[39mreset()\n\u001b[1;32m 11\u001b[0m env\u001b[38;5;241m.\u001b[39mrender()\n\u001b[1;32m 13\u001b[0m done \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/wrappers/order_enforcing.py:42\u001b[0m, in \u001b[0;36mOrderEnforcing.reset\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Resets the environment with `kwargs`.\"\"\"\u001b[39;00m\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_reset \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m---> 42\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menv\u001b[38;5;241m.\u001b[39mreset(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/wrappers/env_checker.py:45\u001b[0m, in \u001b[0;36mPassiveEnvChecker.reset\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchecked_reset \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchecked_reset \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m env_reset_passive_checker(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menv, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 47\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menv\u001b[38;5;241m.\u001b[39mreset(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:204\u001b[0m, in \u001b[0;36menv_reset_passive_checker\u001b[0;34m(env, **kwargs)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 203\u001b[0m obs, info \u001b[38;5;241m=\u001b[39m result\n\u001b[0;32m--> 204\u001b[0m check_obs(obs, env\u001b[38;5;241m.\u001b[39mobservation_space, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreset\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 205\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\n\u001b[1;32m 206\u001b[0m info, \u001b[38;5;28mdict\u001b[39m\n\u001b[1;32m 207\u001b[0m ), \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe second element returned by `env.reset()` was not a dictionary, actual type: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(info)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 208\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
"File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:150\u001b[0m, in \u001b[0;36mcheck_obs\u001b[0;34m(obs, observation_space, method_name)\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(obs, \u001b[38;5;28mtuple\u001b[39m):\n\u001b[1;32m 149\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarn(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpre\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m was expecting a tuple, actual type: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(obs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 150\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(obs) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mlen\u001b[39m(\n\u001b[1;32m 151\u001b[0m observation_space\u001b[38;5;241m.\u001b[39mspaces\n\u001b[1;32m 152\u001b[0m ), \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpre\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m length is not same as the observation space length, obs length: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(obs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, space length: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(observation_space\u001b[38;5;241m.\u001b[39mspaces)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m sub_obs, sub_space \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(obs, observation_space\u001b[38;5;241m.\u001b[39mspaces):\n\u001b[1;32m 154\u001b[0m check_obs(sub_obs, sub_space, method_name)\n",
"\u001b[0;31mAssertionError\u001b[0m: The obs returned by the `reset()` method length is not same as the observation space length, obs length: 1, space length: 2"
]
},
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
"\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
"\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"outputs": [],
"source": [
"maze = [\n",
" ['S', '', '.','.'],\n",
Expand Down
Loading

0 comments on commit 44933fe

Please sign in to comment.