【问题标题】:gym retro reward for moving forward前进的健身房复古奖励
【发布时间】:2021-02-03 15:20:40
【问题描述】:

如何奖励特工在《超级马里奥兄弟》这样的游戏中继续前进?我拥有的唯一数据是分数和生命,但有没有办法获得代理的坐标?我正在使用 NEAT 来训练我的代理,这里是代码。我目前正在奖励它以获得尽可能高的分数,并且奖励它按右键将不起作用,因为它只会推入墙壁并获得奖励,直到计时器用完。

import retro
import numpy as np
import cv2
import neat
import pickle

env = retro.make('SuperMarioWorld-Snes', 'Start.state')

imgarray = []

xpos_end = 0


def eval_genomes(genomes, config):
    for genome_id, genome in genomes:
        ob = env.reset()
        ac = env.action_space.sample()

        inx, iny, inc = env.observation_space.shape

        inx = int(inx / 8)
        iny = int(iny / 8)

        net = neat.nn.recurrent.RecurrentNetwork.create(genome, config)

        current_max_fitness = 0
        fitness_current = 0
        frame = 0
        counter = 0
        xpos = 0
        xpos_max = 0

        done = False
        # cv2.namedWindow("main", cv2.WINDOW_NORMAL)

        while not done:

            env.render()
            frame += 1
            # scaledimg = cv2.cvtColor(ob, cv2.COLOR_BGR2RGB)
            # scaledimg = cv2.resize(scaledimg, (iny, inx))
            ob = cv2.resize(ob, (inx, iny))
            ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)
            ob = np.reshape(ob, (inx, iny))
            # cv2.imshow('main', scaledimg)
            # cv2.waitKey(1)

            imgarray = np.ndarray.flatten(ob)

            nnOutput = net.activate(imgarray)
            for i in  range(len(nnOutput)):
                nnOutput[i] = int(nnOutput[i])
                if nnOutput[i] < 0:
                    nnOutput[i] = 0


            ob, rew, done, info = env.step(nnOutput)

            # xpos = info['x']
            # xpos_end = info['screen_x_end']

            # if xpos > xpos_max:
            # fitness_current += 1
            # xpos_max = xpos

            # if xpos == xpos_end and xpos > 500:
            # fitness_current += 100000
            # done = True




            fitness_current += rew
            print(env.statename)
            if fitness_current > current_max_fitness:
                current_max_fitness = fitness_current
                counter = 0
            else:
                counter += 1

            if done or counter == 250:
                done = True
                print(genome_id, fitness_current)

            genome.fitness = fitness_current


config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                     neat.DefaultSpeciesSet, neat.DefaultStagnation,
                     'config.txt')

p = neat.Population(config)

p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(10))

winner = p.run(eval_genomes)

with open('winner.pkl', 'wb') as output:
    pickle.dump(winner, output, 1)



【问题讨论】:

  • 我不知道它是否可以给出位置,但您可以尝试使用cv2 将当前帧与前一帧进行比较 - 如果它不移动,那么两个帧上的许多像素都是相同的- 然后你可以计算相同的像素。如果相同像素的数量大于某个值,那么您可以假设它不会移动。这是一种原始方法,只需要减去两个帧(为相同的像素获取零)并将所有值相加以获得可以与一些预定义值进行比较的单个值。对于使用 numpy 的 cv2,它可能是 sum(grey_frame_previous - grey_frame_current) &gt; some_value
  • 我认为会有多个移动敌人的问题
  • 最好在下面查看我的答案 - 当您替换 data.json 时,有一种方法可以获取 x,y

标签: python artificial-intelligence reinforcement-learning openai-gym


【解决方案1】:

使用 print( retro.__file__ ) 我找到了带有模块 retro 的文件夹并检查了我找到的带有 SuperMarioWorld 的文件夹的所有子文件夹

在我的 Linux 上是

/usr/local/lib/python3.8/dist-packages/retro/data/stable/SuperMarioWorld-Snes

有一个文件data.json 定义了retro 如何在ROM 中找到scorelives

OpenAI-Retro-SuperMarioWorld-SNES 中,我找到了data.json,其中还有xy 等的信息。

如果我替换data.json,那么我可以在代码中得到info["x"]

但我不确定此文件是否适用于 SuperMario 的每个版本。

我用我发现的Super Mario World (Europe) (Rev 1) 进行了测试

https://ia800201.us.archive.org/view_archive.php?archive=/7/items/No-Intro-Collection_2016-01-03_Fixed/Nintendo%20-%20Super%20Nintendo%20Entertainment%20System.zip

但还有其他版本 - 欧洲、美国、日本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2020-10-01
    • 1970-01-01
    • 2019-02-05
    • 2019-10-16
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多