【強化學習玩轉超級馬里奧】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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM