openAI的比赛retro contest的一些细节设置(Detail)

2018年openAI公司搞了一个比赛retro contest,该比赛目的是为了在自家的库retro上测试迁移强化学习的性能,虽然这个比赛已经结束多年但是现在了解一些也是有一定益处的。

比赛细节介绍,原网址:

https://contest.openai.com/2018-1/details/#get-roms

===========================================================

比赛游戏的训练集:

You are free to train your agent however you'd like, but we recommend using Sonic 1, 2, and 3 & Knuckles, which are available on Steam here:

刺猬索尼克1、刺猬索尼克2、刺猬索尼克3   (这三个游戏需要在steam上购买并下载,具体参看:如何使用Python环境下的2D经典游戏仿真器(openai推出的)retro库运行游戏"刺猬索尼克" (SonicTheHedgehog-Genesis)    )

训练集(上面上个游戏中分别取不同游戏的不同状态作为训练的起始状态):

验证集(上面上个游戏中分别取不同游戏的不同状态作为训练的起始状态):

============================================================

比赛的评价环境:

 硬件环境:

6 E5-2690v3 cores, 56GB of RAM, and a single K80 GPU

训练时间:

12 hours of time you should average ~43ms per timestep to get to 1 million timesteps within the limit

一个step的耗时为43毫秒。

The environment runs at ~1000 frames per second for a single core with random agent, meaning 1ms per frame, or 4ms per timestep, leaving you 39ms for your processing.

由于在测试时也进行训练同时测试时动作选择也是需要计算的,而一个不需要计算和训练的random agent一秒钟大约会和环境交互1000次(1000帧),如果训练的话这1000帧画面就是1000/4=250 timestep,也就是4毫秒一个timestep,由此可以估算出不进行动作选择计算和训练的random agent一个timestep耗时4ms,那么进行动作选择计算和训练的时间大致为43-4=39毫秒。

这里面有个容易搞不懂的地方,那就是为什么1000帧画面是250个timestep,中间的这个4倍是哪里来的。这里起始有个frameskip的操作,就是agent给出一个动作后起始是连续用这个动作进行4次交互的而不是一次,那么4次交互的画面我们只选择第一个画面,这样的话一共和环境交互了1000个frame,而这最后我们是只抽取其中的250个frame作为训练,而这250个frame再单步滑动的4个frame组成一个timestep,因此1000个frame最后形成了250个timestep。

The environment is stochastic in that it has sticky frameskip. While normal frameskip always repeats an action n times, sticky frameskip occasionally repeats an action n+1 times. When this happens, the following action is repeated one fewer times, since it is delayed by an extra frame. For the contest, sticky frameskip repeats an action an extra time with probability 0.25.

frameskip操作本身进行动作重复选择时可以加入概率随机性,具体参看(Revisiting the Arcade Learning Environment: Evaluation Protocols and Open Problems for General Agents ):https://arxiv.org/pdf/1709.06009.pdf

Your agent is allowed to learn (adjust its weights, use a replay buffer, etc) during test time, although a separate copy of the agent will be run on each test level.

每次测试都进行训练,但是每次测试时使用的agent都是完全独立的,也就是说每次的测试agent都是训练agent的完全拷贝。

In addition you are limited to 4,500 timesteps per episode, corresponding to 18,000 frames or 5 minutes of real time at 60 fps.

不论是训练还是测试的时候对每一个回合的长度进行限制,每个回合最多timestep为4500, 也就是和环境交互18000个frame。对episode长度进行限制以防止有的episode进入无法终止的情况。

====================================================

The reward your agent receives is proportional to its progress to the predefined horizontal offset within each level, positive for getting closer, negative for getting further away. If you reach the offset, the sum of your rewards will be 9000. In addition there is a time bonus that starts at 1000 and decreases learning to 0 at the end of the time limit, so beating the level as quickly as possible is rewarded.

奖励函数的设定,奖励函数和水平位移相关,agent距离设定的位移越近reward越大,距离越远reward越小。

关于对游戏中的奖励函数设定和游戏终止条件的判断是很复杂的,尤其是在 “刺猬索尼克”这样的游戏中,奖励函数的设定对游戏性能有较大的影响。

查看contest.json内容:

路径:~/anaconda3/envs/baselines/lib/python3.7/site-packages/retro/data/stable/SonicTheHedgehog-Genesis

contest.json

{
  "done": {
    "script": "lua:contest_done"
  },
  "reward": {
    "script": "lua:contest_reward"
  },
  "scripts": [
    "script.lua"
  ]
}

可以看到游戏终结的判断函数为lua脚本script.lua中设定的contest_done函数,reward函数则是script.lua中的contest_reward函数:

script.lua

level_max_x = {
-- Green Hill Zone
    ["zone=0,act=0"] = 0x2560,
    ["zone=0,act=1"] = 0x1F60,
    ["zone=0,act=2"] = 0x292A,

-- Marble Zone
    ["zone=2,act=0"] = 0x1860,
    ["zone=2,act=1"] = 0x1860,
    ["zone=2,act=2"] = 0x1720,

-- Spring Yard Zone
    ["zone=4,act=0"] = 0x2360,
    ["zone=4,act=1"] = 0x2960,
    ["zone=4,act=2"] = 0x2B83,

-- Labyrinth Zone
    ["zone=1,act=0"] = 0x1A50,
    ["zone=1,act=1"] = 0x1150,
    ["zone=1,act=2"] = 0x1CC4,

-- Star Light Zone
    ["zone=3,act=0"] = 0x2060,
    ["zone=3,act=1"] = 0x2060,
    ["zone=3,act=2"] = 0x1F48,

-- Scrap Brain Zone
    ["zone=5,act=0"] = 0x2260,
    ["zone=5,act=1"] = 0x1EE0,
    -- ["zone=5,act=2"] = 000000, -- does not have a max x
}

function level_key()
    return string.format("zone=%d,act=%d", data.zone, data.act)
end

function clip(v, min, max)
    if v < min then
        return min
    elseif v > max then
        return max
    else
        return v
    end
end

data.prev_lives = 3

function contest_done()
    if data.lives < data.prev_lives then
        return true
    end
    data.prev_lives = data.lives

    if calc_progress(data) >= 1 then
        return true
    end

    return false
end

data.offset_x = nil
end_x = nil

function calc_progress(data)
    if data.offset_x == nil then
        data.offset_x = -data.x
        end_x = level_max_x[level_key()] - data.x
    end

    local cur_x = clip(data.x + data.offset_x, 0, end_x)
    return cur_x / end_x
end

data.prev_progress = 0
frame_limit = 18000

function contest_reward()
    local progress = calc_progress(data)
    local reward = (progress - data.prev_progress) * 9000
    data.prev_progress = progress

    -- bonus for beating level quickly
    if progress >= 1 then
        reward = reward + (1 - clip(scenario.frame / frame_limit, 0, 1)) * 1000
    end
    return reward
end

data.xpos_last_x = nil

function xpos_done()
    if data.lives < data.prev_lives then
        return true
    end
    data.prev_lives = data.lives

    if scenario.frame >= frame_limit then
        return true
    end

    return data.x > level_max_x[level_key()]
end

function xpos_rew()
    if data.xpos_last_x == nil then
        data.xpos_last_x = data.x
    end
    local result = data.x - data.xpos_last_x
    data.xpos_last_x = data.x
    return result
end

其中,level_max_x中给出不同游戏状态开始的游戏中最终的终点x轴的距离(如:0x2560 则是变量地址,该地址的变量则是游戏终点的x轴上位置)

level_max_x = {
-- Green Hill Zone
    ["zone=0,act=0"] = 0x2560,
    ["zone=0,act=1"] = 0x1F60,
    ["zone=0,act=2"] = 0x292A,

-- Marble Zone
    ["zone=2,act=0"] = 0x1860,
    ["zone=2,act=1"] = 0x1860,
    ["zone=2,act=2"] = 0x1720,

-- Spring Yard Zone
    ["zone=4,act=0"] = 0x2360,
    ["zone=4,act=1"] = 0x2960,
    ["zone=4,act=2"] = 0x2B83,

-- Labyrinth Zone
    ["zone=1,act=0"] = 0x1A50,
    ["zone=1,act=1"] = 0x1150,
    ["zone=1,act=2"] = 0x1CC4,

-- Star Light Zone
    ["zone=3,act=0"] = 0x2060,
    ["zone=3,act=1"] = 0x2060,
    ["zone=3,act=2"] = 0x1F48,

-- Scrap Brain Zone
    ["zone=5,act=0"] = 0x2260,
    ["zone=5,act=1"] = 0x1EE0,
    -- ["zone=5,act=2"] = 000000, -- does not have a max x
}

由于我们游戏的开始状态的x轴位置不为0,所以我们要记录我们游戏状态开始时的x轴位置坐标,这里设定为data.offset_x,由于游戏开始时的agent在x轴上的位置为data.x,所以我们设定data.offset_x= -data.x,   由下面的函数  calc_progress可以看到我们对data.offset_x的设置只进行一次,即游戏开始的状态,以后则不对data.offset_x进行修改。

 我们将游戏开始时的位置( -data.offset_x )作为距离规整后的0点,距离规整后的终点位置则为  level_max_x[level_key()] +data.offset_x=level_max_x[level_key()] - data.x

于是,在以后的位置计算中agent的位置则为data.x + data.offset_x, 游戏起始点位置为0,终点位置为end_x 。

agent在游戏起始点和终点之间的位置比例为: cur_x / end_x ,该数值大于等于0小于等于1。

同时需要对游戏中agent的位置进行clip,因为游戏中的agent位置可能会小于游戏起始点也可能大于游戏终止点。

function calc_progress(data)
    if data.offset_x == nil then
        data.offset_x = -data.x
        end_x = level_max_x[level_key()] - data.x
    end

    local cur_x = clip(data.x + data.offset_x, 0, end_x)
    return cur_x / end_x
end

我们得到了游戏中agent的位置在起始点和终点之间的比值就可以进入奖励函数的核心部分:

data.prev_progress = 0
frame_limit = 18000

function contest_reward()
    local progress = calc_progress(data)
    local reward = (progress - data.prev_progress) * 9000
    data.prev_progress = progress

    -- bonus for beating level quickly
    if progress >= 1 then
        reward = reward + (1 - clip(scenario.frame / frame_limit, 0, 1)) * 1000
    end
    return reward
end

 分析上面代码,data.prev_progress指的是前一个时刻agent在0和end_x之间的比例,progress表示当前时刻agent在0和end_x之间的比例,当前时刻由于agent移动所带来的reward奖励为local reward = (progress - data.prev_progress) * 9000,假设agent从游戏开始到游戏终止时到达end_x位置,那么每一步的该部分奖励和为所有步的(progress - data.prev_progress) * 9000之和,等于最后到达end_x时的progress与游戏开始时的data.prev_progress之差,等于数值1,也就是说如果agent从游戏开始的x轴位置运动到游戏终点位置x_end那么最终所有步获得的该部分奖励之和为9000。

奖励函数的第二部分:

frame_limit = 18000
    if progress >= 1 then
        reward = reward + (1 - clip(scenario.frame / frame_limit, 0, 1)) * 1000
    end

奖励的第二部分可以视为对游戏时长的惩罚,如果游戏当前状态的frame数在0到18000之间那么我们按照比例给以奖励,如果游戏运行的frame数超过18000那么则不进行奖励。

游戏终止的判断条件:

function xpos_done()
    if data.lives < data.prev_lives then
        return true
    end
    data.prev_lives = data.lives

    if scenario.frame >= frame_limit then
        return true
    end

    return data.x > level_max_x[level_key()]
end

每一步都判断游戏lives变量是否变化,如果有则说明游戏回合结束。

    if data.lives < data.prev_lives then
        return true
    end

如果游戏帧超过设定则设定为游戏回合终止。对游戏的时长上限进行设定。

    if scenario.frame >= frame_limit then
        return true
    end

对游戏中agent的x轴最大值设定上限,如果agent的x轴位置大于设定的游戏最大x轴位置则判定为回合结束。

data.x > level_max_x[level_key()]
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注者,如有侵权请与博主联系。
原文地址:https://www.cnblogs.com/devilmaycry812839668/p/15291662.html