如何定义我的网格世界环境的状态?

我想要解决的问题实际上并不简单,但这是一个帮助我解决更大问题的玩具游戏。

所以我有一个5×5的矩阵,所有的值都等于0:

structure = np.zeros(25).reshape(5, 5)

目标是让智能体将所有值变为1,因此我有:

goal_structure = np.ones(25).reshape(5, 5)

我创建了一个Player类,包含5个动作,分别是向左、向右、向上、向下或翻转(将值从0变为1或从1变为0)。对于奖励,如果智能体将值从0变为1,它会获得+1的奖励。如果它将1变为0,它会得到一个负奖励(我尝试了从-1到0甚至-0.1的许多值)。如果它只是向左、向右、向上或向下移动,它会获得0奖励。

因为我想将状态输入到我的神经网络中,我将状态重新整形如下:

reshaped_structure = np.reshape(structure, (1, 25))

然后我在这个数组的末尾添加了智能体的归一化位置(因为我认为智能体应该知道自己在哪里):

reshaped_state = np.append(reshaped_structure, (np.float64(self.x/4), np.float64(self.y/4)))state = reshaped_state

但我没有得到任何好的结果!看起来就像是随机的!我尝试了不同的奖励函数,不同的优化算法,比如经验回放、目标网络、双重DQN、对决网络,但它们似乎都不起作用!我猜问题出在定义状态上。有人可以帮助我定义一个好的状态吗?

非常感谢!

附:这是我的步进函数:

class Player:def __init__(self):    self.x = 0    self.y = 0    self.max_time_step = 50    self.time_step = 0    self.reward_list = []    self.sum_reward_list = []    self.sum_rewards = []    self.gather_positions = []    # self.dict = {}    self.action_space = spaces.Discrete(5)    self.observation_space = 27def get_done(self, time_step):    if time_step == self.max_time_step:        done = True    else:        done = False    return donedef flip_pixel(self):    if structure[self.x][self.y] == 1:        structure[self.x][self.y] = 0.0    elif structure[self.x][self.y] == 0:        structure[self.x][self.y] = 1def step(self, action, time_step):    reward = 0    if action == right:        if self.y < y_threshold:            self.y = self.y + 1        else:            self.y = y_threshold    if action == left:        if self.y > y_min:            self.y = self.y - 1        else:            self.y = y_min    if action == up:        if self.x > x_min:            self.x = self.x - 1        else:            self.x = x_min    if action == down:        if self.x < x_threshold:            self.x = self.x + 1        else:            self.x = x_threshold    if action == flip:        self.flip_pixel()        if structure[self.x][self.y] == 1:            reward = 1        else:            reward = -0.1    self.reward_list.append(reward)    done = self.get_done(time_step)    reshaped_structure = np.reshape(structure, (1, 25))    reshaped_state = np.append(reshaped_structure, (np.float64(self.x/4), np.float64(self.y/4)))    state = reshaped_state    return state, reward, donedef reset(self):    structure = np.zeros(25).reshape(5, 5)    reset_reshaped_structure = np.reshape(structure, (1, 25))    reset_reshaped_state = np.append(reset_reshaped_structure, (0, 0))    state = reset_reshaped_state    self.x = 0    self.y = 0    self.reward_list = []    self.gather_positions = []    # self.dict.clear()    return state

回答:

我会将智能体的位置编码成如下矩阵:

0 0 0 0 00 0 0 0 00 0 1 0 00 0 0 0 00 0 0 0 0

(其中智能体位于中间)。当然,你也需要将这个矩阵展平以输入到网络中。所以你的总状态是50个输入值,25个用于单元格状态,25个用于智能体位置。

当你将位置编码为两个浮点数时,网络需要做一些工作来解码浮点数的具体值。如果你使用像上面这样的明确方案,网络可以非常清楚地知道智能体在哪里。这是一种位置的“独热”编码。

例如,如果你查看Atari DQN的论文,智能体的位置总是明确地编码,每个可能的位置都有一个神经元。

还要注意,对于你的智能体来说,一个非常好的策略是站在原地并持续翻转状态,这样每步可以获得0.45的奖励(从0到1获得+1,从1到0获得-0.1,分两步计算)。假设有一个完美的策略,它只能获得25,但这个策略将获得22.5的奖励,并且很难去学习。我建议智能体在翻转一个好的奖励时获得-1的惩罚。


你提到智能体没有学习。我建议尽可能简化。第一个建议是 – 将每集的长度减少到2或3步,并将网格大小减少到1。看看智能体是否能学会一致地将单元格设置为1。同时,尽可能简化智能体的“大脑”。将其简化为只有一个输出层的线性模型和一个激活函数。这应该很快并且容易学习。如果智能体在100集内没有学会,我怀疑你的强化学习实现中可能有错误。如果它有效,你可以开始扩大网格的大小和网络的大小。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注