while not ep_done: num_steps += 1 if train_params.RENDER: self.env_wrapper.render() action = self.sess.run(self.actor_net.output, {self.state_ph:np.expand_dims(state, 0)})[0] # Add batch dimension to single state input, and remove batch dimension from single action output action += (gaussian_noise() * train_params.NOISE_DECAY**num_eps) next_state, reward, terminal = self.env_wrapper.step(action) episode_reward += reward next_state = self.env_wrapper.normalise_state(next_state) reward = self.env_wrapper.normalise_reward(reward) self.exp_buffer.append((state, action, reward)) if len(self.exp_buffer) >= train_params.N_STEP_RETURNS: state_0, action_0, reward_0 = self.exp_buffer.popleft() discounted_reward = reward_0 gamma = train_params.DISCOUNT_RATE for (_, _, r_i) in self.exp_buffer: discounted_reward += r_i * gamma gamma *= train_params.DISCOUNT_RATE run_agent_event.wait() PER_memory.add(state_0, action_0, discounted_reward, next_state, terminal, gamma) state = next_state
时间: 2024-02-10 09:32:53 浏览: 35
这段代码是主循环中的一部分,其中包含了执行动作、观察环境、更新经验缓存等操作。具体来说,算法执行以下步骤:
1. 累计步数;
2. 如果需要渲染环境,则渲染环境;
3. 使用Actor网络计算当前状态的动作;
4. 对动作添加高斯噪声;
5. 执行动作并观察新状态和奖励;
6. 累计回报;
7. 对新状态进行归一化处理;
8. 对奖励进行归一化处理;
9. 将经验添加到经验缓存中;
10. 如果经验缓存已满,则计算N步回报,并将经验添加到优先经验回放缓存中;
11. 更新状态。
在这个过程中,Actor网络用于计算当前状态下的动作,而高斯噪声则用于增加探索性,以便算法能够更好地探索环境。在执行动作之后,算法会观察新状态和奖励,并将它们添加到经验缓存中。如果经验缓存已满,算法会计算N步回报,并将经验添加到优先经验回放缓存中。最后,算法会更新状态并继续执行主循环。
相关问题
def run_episode(env, agent, render=False): total_steps = 0 # 记录每个episode走了多少step total_reward = 0 obs = env.reset() # 重置环境, 重新开一局(即开始新的一个episode) while True: action = agent.sample(obs) # 根据算法选择一个动作 next_obs, reward, done, _ = env.step(action) # 与环境进行一次交互 # 训练 Q-learning算法 agent.learn(obs, action, reward, next_obs, done) obs = next_obs # 存储上一个观察值 total_reward += reward total_steps += 1 # 计算step数 if render: env.render(mode = 'human') #渲染新的一帧图形 if done: break return total_reward, total_steps
这段代码是一个运行一个episode的函数。
首先,代码初始化了total_steps和total_reward,用于记录每个episode走了多少步和获得的总奖励。
然后,通过调用env.reset()重置环境,开始新的一个episode,并获取初始观察值obs。
接下来,进入一个无限循环,直到episode结束为止。在循环中,首先调用agent.sample(obs)根据算法选择一个动作。
然后,通过调用env.step(action)与环境进行一次交互,获取下一个观察值next_obs、奖励reward、是否结束done等信息。
接着,调用agent.learn(obs, action, reward, next_obs, done)来训练Q-learning算法,更新Q表。
接下来,将next_obs赋值给obs,存储上一个观察值。
然后,更新total_reward和total_steps。
如果render参数为True,则调用env.render(mode='human')来渲染新的一帧图形。
最后,如果done为True,即episode结束,则跳出循环。
最后,返回total_reward和total_steps作为该episode的结果。
# render elements self._render_marker = True self._render_trails = True
这段代码看起来像是 Python 中的一个类的初始化函数中的代码。其中 `_render_marker` 和 `_render_trails` 是该类的两个成员变量,它们的值都被初始化为 True。根据变量名来看,这个类可能是用于渲染某种图形的,`_render_marker` 和 `_render_trails` 可能是控制是否渲染标记和轨迹的开关。但是,为了更好地理解这段代码的含义,还需要结合该类的其他代码来进行分析。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)