python - 合并和拆分来自 TF-agents 的时间和动作步骤
问题描述
我正在尝试在一个简单的多代理非合作并行游戏中使用 TF 代理。为简化起见,我有两个用 TF 代理定义的代理。我定义了一个自定义的健身房环境,它将代理的组合动作作为输入并返回一个观察结果。代理的策略不应将全部观察作为输入,而应仅将其一部分作为输入。所以我需要做两件事:
- 拆分
time_step
由 TF-agents 环境包装器返回的实例,以独立提供给代理的策略 - 合并
action_step
来自代理策略的实例以提供环境。
如果agent1_policy
并且agent2_policy
是两个 TF-agents 策略并且environment
是 TF-agents 环境,我希望能够这样做来收集步骤:
from tf_agents.trajectories import trajectory
time_step = environment.current_time_step()
# Split the time_step to have partial observability
time_step1, time_step2 = split(time_step)
# Get action from each agent
action_step1 = agent1_policy.action(time_step1)
action_step2 = agent2_policy.action(time_step2)
# Merge the independent actions
action_merged = merge(action_step1, action_step2)
# Use the merged actions to have the next step
next_time_step = environment.step(action_merged)
# Split the next step too
next_time_step1, next_time_step2 = split(next_time_step)
# Build two distinct trajectories
traj1 = trajectory.from_transition(time_step1, action_step1, next_time_step1)
traj2 = trajectory.from_transition(time_step2, action_step2, next_time_step2)
traj1
然后traj2
被添加到用于训练两个代理的缓冲区中。
我应该如何定义函数merge
以及split
在这个例子中?
解决方案
这可以通过在环境类中定义适当的action_spec
和来完成。有关生成作为张量字典的观察的示例,observation_spec
请参阅此文档。类似的方法可用于接受作为字典或元组的动作。
推荐阅读
- python - pip安装后Python找不到包
- javascript - 脚本不按画布上的坐标绘制线条
- google-apps-script - 使用 Google 脚本函数作为公式 - 错误异常:您无权调用 setActiveSheet
- spring - Spring Data 反应式存储库页面计数和总计数
- javascript - 在 React 中更改搜索文本的颜色
- encryption - Win10 GnuPG 自动批量解密使用 echo 和 --passphrase-fd 0
- javascript - 使用嵌套对象获取 React 中的 Graphql 问题 - 对象未定义
- ios - 迁移 RxSwift 以结合
- c - 释放二叉树并计算空闲节点的数量 -> 没有全局变量
- reactjs - 如何自动升级 package.json 或 react 库?