MCPcopy Create free account
hub / github.com/pytorch/examples / run_episode

Method run_episode

distributed/rpc/batch/reinforce.py:80–116  ·  view source on GitHub ↗

r""" Run one episode of n_steps. Args: agent_rref (RRef): an RRef referencing the agent object. n_steps (int): number of steps in this episode

(self, agent_rref, n_steps)

Source from the content-addressed store, hash-verified

78 self.select_action = Agent.select_action_batch if batch else Agent.select_action
79
80 def run_episode(self, agent_rref, n_steps):
81 r"""
82 Run one episode of n_steps.
83
84 Args:
85 agent_rref (RRef): an RRef referencing the agent object.
86 n_steps (int): number of steps in this episode
87 """
88 state, _ = self.env.reset()
89 ep_reward = NUM_STEPS
90 rewards = torch.zeros(n_steps)
91 start_step = 0
92 for step in range(n_steps):
93 state = torch.from_numpy(state).float().unsqueeze(0)
94 # send the state to the agent to get an action
95 action = rpc.rpc_sync(
96 agent_rref.owner(),
97 self.select_action,
98 args=(agent_rref, self.id, state)
99 )
100
101 # apply the action to the environment, and get the reward
102 state, reward, terminated, truncated, _ = self.env.step(action)
103 rewards[step] = reward
104
105 if terminated or truncated or step + 1 >= n_steps:
106 curr_rewards = rewards[start_step:(step + 1)]
107 R = 0
108 for i in range(curr_rewards.numel() -1, -1, -1):
109 R = curr_rewards[i] + args.gamma * R
110 curr_rewards[i] = R
111 state, _ = self.env.reset()
112 if start_step == 0:
113 ep_reward = min(ep_reward, step - start_step + 1)
114 start_step = step + 1
115
116 return [rewards, ep_reward]
117
118
119class Agent:

Callers

nothing calls this directly

Calls 1

resetMethod · 0.80

Tested by

no test coverage detected