合作乒乓

../../../_images/butterfly_cooperative_pong.gif

此环境是 butterfly 环境的一部分。请先阅读该页面以获取一般信息。

导入

from pettingzoo.butterfly import cooperative_pong_v6

动作

离散

Parallel API

手动控制

智能体

agents= ['paddle_0', 'paddle_1']

智能体

2

动作空间形状

Discrete(3)

动作值

[0, 1, 2]

观测空间形状

(280, 480, 3)

观测值

[0, 255]

状态空间形状

(280, 480, 3)

状态值

(0, 255)

合作乒乓是一款简单的乒乓球游戏,目标是让球尽可能长时间地留在场内。当球从屏幕左边缘或右边缘出界时,游戏结束。游戏中有两个智能体(球拍),一个沿屏幕左边缘移动,另一个沿右边缘移动。所有球的碰撞都是弹性碰撞。每次重置时,球总是从屏幕中心以随机方向开始移动。为了使学习更具挑战性,默认情况下,右侧球拍呈分层蛋糕状。每个智能体的观测空间是整个屏幕。智能体有三种可能的动作(*向上/向下移动* 或 什么也不做)。如果球留在界内,每个智能体在每个时间步都会获得 max_reward / max_cycles(默认为 0.11)的奖励。否则,每个智能体都会获得 off_screen_penalty(默认为 -10)的惩罚,游戏结束。

手动控制

使用“W”和“S”键移动左侧球拍。使用“上”和“下”方向键移动右侧球拍。

参数

cooperative_pong_v6.env(
    ball_speed = 9,
    left_paddle_speed = 12,
    right_paddle_speed = 12,
    cake_paddle = True,
    max_cycles = 900,
    bounce_randomness = False,
    max_reward = 100,
    off_screen_penalty = -10,
    render_mode = None,
    render_ratio = 2,
    render_fps = 15,
)

ball_speed: 球的速度(像素)。请注意,如果球速设置过高,球可能会穿过球拍并出界。

left_paddle_speed: 左侧球拍速度(像素)

right_paddle_speed: 右侧球拍速度(像素)

cake_paddle: 如果为 True,右侧球拍将呈四层婚礼蛋糕状

max_cycles: 经过 max_cycles 步后,所有智能体都将返回 done

bounce_randomness: 如果为 True,球与球拍的每次碰撞都会给球的方向增加一个小的随机角度,球速保持不变。

max_reward: 在 max_cycles 时间步内给予每个智能体的总奖励

off_screen_penalty: 如果球出界,给予每个智能体的负奖励惩罚

render_mode: 环境的渲染模式(None,“human” 或 “rgb_array”)

render_ratio: 屏幕渲染的缩放比例(控制显示尺寸,值越大屏幕越小)

render_fps: 游戏运行速度(帧每秒,值越高游戏越快)

版本历史

  • v6: 修正了错误的终止条件和随机反弹行为 (1.25.5)

  • v5: 修正了球传送错误

  • v4: 添加了 max_reward 和 off_screen_penalty 参数并更改了默认值,修正了球偶尔会传送的故障,奖励重新设计 (1.14.0)

  • v3: 将观测空间更改为包含整个屏幕 (1.10.0)

  • v2: 杂项修复 (1.4.0)

  • v1: 修正了 dones 计算方式中的错误 (1.3.1)

  • v0: 初版发布 (1.0.0)

用法

AEC

from pettingzoo.butterfly import cooperative_pong_v6

env = cooperative_pong_v6.env(render_mode="human")
env.reset(seed=42)

for agent in env.agent_iter():
    observation, reward, termination, truncation, info = env.last()

    if termination or truncation:
        action = None
    else:
        # this is where you would insert your policy
        action = env.action_space(agent).sample()

    env.step(action)
env.close()

Parallel

from pettingzoo.butterfly import cooperative_pong_v6

env = cooperative_pong_v6.parallel_env(render_mode="human")
observations, infos = env.reset()

while env.agents:
    # this is where you would insert your policy
    actions = {agent: env.action_space(agent).sample() for agent in env.agents}

    observations, rewards, terminations, truncations, infos = env.step(actions)
env.close()

API

class pettingzoo.butterfly.cooperative_pong.cooperative_pong.env(**kwargs: Any)[source]

创建包装后的环境。

class pettingzoo.butterfly.cooperative_pong.cooperative_pong.raw_env(**kwargs: Any)[source]

CooperativePong AEC 环境。

初始化环境。

action_space(agent: AgentID) Space[Any][source]

返回给定智能体的动作空间。

close() None[source]

关闭渲染器。

observation_space(agent: AgentID) Space[Any][source]

返回给定智能体的观测空间。

observe(agent: AgentID) ObsType[source]

返回给定智能体的观测。

render() ndarray[tuple[int, ...], dtype[integer]] | None[source]

渲染环境的当前状态。

reset(seed: int | None = None, options: dict[str, Any] | None = None) None[source]

重置环境。

state() StateType[source]

返回环境的状态。

step(action: Literal[0, 1, 2]) None[source]

执行环境的一步。