Skip to content

Commit

Permalink
Merge pull request #316 from yandexdataschool/master-week6-fix-cartpo…
Browse files Browse the repository at this point in the history
…le-timelimit

[week6] Fix accidental TimeLimit in CartPole
  • Loading branch information
justheuristic authored Feb 4, 2020
2 parents 4819885 + 2520bd7 commit ff1f81e
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions week06_policy_based/reinforce_tensorflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@
"source": [
"import gym\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"env = gym.make(\"CartPole-v0\")\n",
"\n",
"# gym compatibility: unwrap TimeLimit\n",
"if hasattr(env, 'env'):\n",
"if hasattr(env, '_max_episode_steps'):\n",
" env = env.env\n",
"\n",
"env.reset()\n",
Expand Down Expand Up @@ -260,7 +259,7 @@
"metadata": {},
"outputs": [],
"source": [
"def generate_session(t_max=1000):\n",
"def generate_session(env, t_max=1000):\n",
" \"\"\"play env with REINFORCE agent and train at the session end\"\"\"\n",
"\n",
" # arrays to record session\n",
Expand Down Expand Up @@ -302,7 +301,7 @@
"\n",
"for i in range(100):\n",
"\n",
" rewards = [generate_session() for _ in range(100)] # generate new sessions\n",
" rewards = [generate_session(env) for _ in range(100)] # generate new sessions\n",
"\n",
" print(\"mean reward:%.3f\" % (np.mean(rewards)))\n",
"\n",
Expand All @@ -326,10 +325,9 @@
"source": [
"# record sessions\n",
"import gym.wrappers\n",
"env = gym.wrappers.Monitor(gym.make(\"CartPole-v0\"),\n",
" directory=\"videos\", force=True)\n",
"sessions = [generate_session() for _ in range(100)]\n",
"env.close()"
"monitor_env = gym.wrappers.Monitor(gym.make(\"CartPole-v0\"), directory=\"videos\", force=True)\n",
"sessions = [generate_session(monitor_env) for _ in range(100)]\n",
"monitor_env.close()"
]
},
{
Expand Down

0 comments on commit ff1f81e

Please sign in to comment.