gym强化学习环境搭建

1、安装依赖库

通过测试发现除了gym以外,pygame也是是必须的
pip install tensorflow
pip install keras
pip install keras-rl
pip install PyOpenGL
pip install pyglet1.5.11
pip install gym0.19.0 #测试0.19.0可以正常运行,0.26.0,fit input错误
pip install pygame

2、测试代码

gym.make()必须指定渲染模式如 env = gym.make(“CartPole-v1”, render_mode = “human”),测试发现 render_mode = “human” 会正常显示游戏画面,而 render_mode=”rgb_array” 不会显示游戏画面
env.step的返回值有之前的4个变为了5个
observation, reward, done, info,_ = env.step(action)

import gym

env = gym.make("CartPole-v1", render_mode = "human")    # 创建环境
for i_episode in range(20):
    observation = env.reset()   # 保存环境初始状态
    for t in range(100):
        env.render()
        #print(observation)     # [位置,加速度,角度,角加速度]
        action = env.action_space.sample()  # 获取一个动作
        observation, reward, done, info,_ = env.step(action)    # 获取执行上面动作的结果
        if done:
            print("Episode finished after {} timesteps".format(t + 1))
            break

3、源码

import gym
import tensorflow as tf
import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Flatten
from tensorflow.keras.optimizers import Adam
from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory

def build_agent(nb_actions,model):
    memory = SequentialMemory(limit=50000, window_length=1

Original: https://blog.csdn.net/haodawei123/article/details/127956164
Author: haodawei123
Title: gym强化学习环境搭建

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/780277/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球