Eden中提供可供强化学习智能算法使用的gym接口,便于不同模型在Eden中训练。Demo采用Stable_baseline3中的PPO算法进行测试,其中策略采用"MlpPolicy",其余参数为默认。
Demo采用如下Wrapper,由于Eden支持多智能体模式,本样例中为单智能体,所以添加如下wrapper以确保环境的Observation和Action可以被ppo算法接收。Wrapper中额外添加与距离相关的reward函数。
class OneAgent(Wrapper):
def __init__(self,env: Eden):
super().__init__(env)
self.env = env
self.action_space = self.action_space[0]
shape = self.observation_space.shape[1:3]
low = self.observation_space.low.min()
high = self.observation_space.high.max()
self.observation_space = spaces.Box(low,high,shape)
def reset(self):
return self.env.reset()[0]
def add_dis_reward(self, org_r):
self.read()
if len(self.obs_dict['Brave']) != 0:
dis = self.obs_dict['Brave'][0]['being']['Ox'][0][2]
dis_r = 0.5 * (dis - 2)
new_r = [org_r[0] + dis_r]
else:
new_r = org_r
return new_r
def step(self, action):
action = [action]
obs, reward, done, info = self.env.step(action)
reward = self.add_dis_reward(reward)
return obs[0],reward[0],done[0],{}
受算力和时间限制,该模型的训练计划分多阶段进行,每个阶段给予agent不同的reward分布。 第一阶段希望agent学会合理的移动,即不向无法移动的地方移动浪费回合。因此给move动作附加了较高的reward,但move失败会有惩罚 第二阶段希望agent学会远离怪物,因此根据agent和怪物的距离给予一定的reward,距离越远reward越大。 第三阶段希望agent学会与环境中的道具交互,因此将移动的reward降为0转而增加拾取(pickup),装备(equip),消耗(consume)等动作的reward。
由于环境尺寸较大,自由度较高,训练难度大、效率低,PPO算法较难应对像该样例一样复杂的环境 根据目前PPO的训练结果,第一阶段目标基本完成,在给agent较多生命值时候,agent可以向迷宫出口逼近,完成80%的路径并合理规避带有debuff的地形。但agent二三阶段目标仍需继续训练,agent有时会主动向怪物靠近而且无法与环境中的道具合理交互。 目前训练出的ppo模型如视频所示: