微信扫一扫打赏支持

【强化学习玩转超级马里奥】03-马里奥环境代码说明

【强化学习玩转超级马里奥】03-马里奥环境代码说明

一、代码分析

from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
import time
from matplotlib import pyplot as plt
env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)

二、分析动作

1、使用 JoypadSpace

env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)
env.action_space
env.action_space.sample()

2、查看动作具体是什么

SIMPLE_MOVEMENT
SIMPLE_MOVEMENT[1]

3、不使用JoypadSpace的情况

env = gym_super_mario_bros.make('SuperMarioBros-v0')
env.action_space

4、使用固定动作效果

比如只让马里奥向右走

from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
import time

env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)

done = True
for step in range(5000):
    if done:
        state = env.reset()
    state, reward, done, info = env.step(6)
    time.sleep(0.01)
    env.render()

env.close()
env.close()

三、分析state

state = env.reset()
state.shape
plt.imshow(state)
state, reward, done, info = env.step(2)
plt.imshow(state)

四、查看奖励

from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
import time

env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)

done = True
for step in range(5000):
    if done:
        state = env.reset()
    state, reward, done, info = env.step(1)
    print(reward)
    time.sleep(0.04)
    env.render()

env.close()

五、查看info

from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
import time

env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)

state = env.reset()
state, reward, done, info = env.step(1)
print(info)

六、换关卡

from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
import time

env = gym_super_mario_bros.make('SuperMarioBros-4-2-v1')
env = JoypadSpace(env, SIMPLE_MOVEMENT)

done = True
for step in range(5000):
    if done:
        state = env.reset()
    state, reward, done, info = env.step(env.action_space.sample())
    time.sleep(0.01)
    env.render()

env.close()
posted @ 2022-03-18 14:06  范仁义  阅读(1236)  评论(0编辑  收藏  举报