设置 Mountain Car 环境
最常见的 Gym 环境之一是 Mountain Car。目标是驾驶一辆动力不足的汽车爬上陡坡。由于发动机功率不够,无法一次性爬上去,所以需要通过来回折返来积累动量。您的任务是创建并初始化这个环境。

本练习是课程的一部分
Python 中的 Gymnasium 强化学习
练习说明
- 将
gymnasium库导入为gym。 - 使用 Gym 库创建一个 Mountain Car 环境,环境 ID 设为
MountainCar,render_mode设为'rgb_array'。 - 使用
seed为42重置环境,并获取initial_state,它包含两个值:小车的位置和速度。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the gymnasium library
____
# Create the environment
env = ____
# Get the initial state
initial_state, info = ____
position = initial_state[0]
velocity = initial_state[1]
print(f"The position of the car along the x-axis is {position} (m)")
print(f"The velocity of the car is {velocity} (m/s)")