Run or train the agent on an episode. Parameters ---------- max_steps : int The maximum number of steps to run the episode. render : bool Whether to render the episode during training. update : bool Whether to perf
(self, max_steps, render, update=True)
| 990 | return total_rwd, n_steps |
| 991 | |
| 992 | def _episode(self, max_steps, render, update=True): |
| 993 | """ |
| 994 | Run or train the agent on an episode. |
| 995 | |
| 996 | Parameters |
| 997 | ---------- |
| 998 | max_steps : int |
| 999 | The maximum number of steps to run the episode. |
| 1000 | render : bool |
| 1001 | Whether to render the episode during training. |
| 1002 | update : bool |
| 1003 | Whether to perform the Q function backups after each step. Default |
| 1004 | is True. |
| 1005 | |
| 1006 | Returns |
| 1007 | ------- |
| 1008 | reward : float |
| 1009 | The total reward on the episode. |
| 1010 | steps : float |
| 1011 | The number of steps taken on the episode. |
| 1012 | """ |
| 1013 | self.flush_history() |
| 1014 | |
| 1015 | obs = self.env.reset() |
| 1016 | HS = self.episode_history |
| 1017 | |
| 1018 | action = self.act(obs) |
| 1019 | s = self._obs2num[obs] |
| 1020 | a = self._action2num[action] |
| 1021 | |
| 1022 | # store initial (state, action) tuple |
| 1023 | HS["state_actions"].append((s, a)) |
| 1024 | |
| 1025 | total_reward, n_steps = 0.0, 0 |
| 1026 | for i in range(max_steps): |
| 1027 | if render: |
| 1028 | self.env.render() |
| 1029 | |
| 1030 | # take action |
| 1031 | obs, reward, done, info = self.env.step(action) |
| 1032 | n_steps += 1 |
| 1033 | |
| 1034 | # record rewards |
| 1035 | HS["rewards"].append(reward) |
| 1036 | total_reward += reward |
| 1037 | |
| 1038 | # generate next state and action |
| 1039 | action = self.act(obs) |
| 1040 | s_ = self._obs2num[obs] if not done else None |
| 1041 | a_ = self._action2num[action] |
| 1042 | |
| 1043 | # store next (state, action) tuple |
| 1044 | HS["state_actions"].append((s_, a_)) |
| 1045 | |
| 1046 | if update: |
| 1047 | self.update() |
| 1048 | |
| 1049 | if done: |
no test coverage detected