【强化学习玩转超级马里奥】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()
版权申明:欢迎转载,但请注明出处
一些博文中有一些参考内容因时间久远找不到来源了没有注明,如果侵权请联系我删除。
在校每年国奖、每年专业第一,加拿大留学,先后工作于华东师范大学和香港教育大学。
2024-10-30:27岁,宅加太忙,特此在网上找女朋友,坐标上海,非诚勿扰,vx:fan404006308
AI交流资料群:753014672