使用openai gym(21点)制作ai

2024-09-30 08:17:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用openai gym为21点制作一个AI。在

但我不擅长python和gym,所以我想知道如何完成代码。在

我一直在尝试写一个简单的代码,用Q-learning来制作人工智能。在

但我对开放式人工智能健身房和python还不够熟悉。在

我不知道如何检查一个州的大小(环境观测空间.n不起作用。。只有环境行动空间.n显示它的“2”) 我的代码有点像其他体育游戏的例子(frozenlake)

帮我完成这个简单的代码,这样我就可以像DQN一样自己改进了。在

import gym
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

env=gym.make('Blackjack-v0')

Q=np.zeros([400,env.action_space.n])

num_episodes=10000
dis=0.99
rList=[]

for i in range(num_episodes):
    state = env.reset()
    rALL = 0
    done = False

    while not done: 
        action=np.argmax(Q[state,:]+np.random.randn(1
        ,env.action_space.n)/(i+1))


        new_state,reward,done,_=env.step(action)

        Q[state, action] = reward + dis * np.max(Q[new_state, :])
        print(rList)
        rALL += reward
        state = new_state

    rList.append(rALL)\


print(Q)

我想看到奖励列表(rList)不断上升(如果我的算法有效)

同时也想知道如何使用健身模块。在


Tags: 代码importenvnew环境asnpaction
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:22

我将更新此回复,因为我完全理解您的需求

  • 对于评论中的第一个问题,您可以通过使用env.observation_space.n来获得操作的数量,如果您使用的是最新版本的gym,则可以通过env.unwrapped.get_action_meanings()获得操作的含义。在
  • 对于第三个问题,您可以使用env.render()来可视化游戏。在

下面是一个使用python3和最新版本的gym版本'0.10.9'呈现游戏的最小工作示例(您可以通过gym.__version__获得您的gym版本):

import time
import gym

# Create a breakout environment
env = gym.make('SpaceInvaders-v4')

# Reset it, returns the starting frame
frame = env.reset()

# Render
env.render()

is_done = False

while not is_done:
    # Perform a random action, returns the new frame, reward and whether the game is over
    frame, reward, is_done, _ = env.step(env.action_space.sample())
    # Render
    env.render()

    time.sleep(0.01)
    if is_done:
        env.close()
        break

相关问题 更多 >

    热门问题