强化学习入门2:SARSA算法

SARSA(State-Action-Reward-State-Action)是一种基于强化学习的算法,与Q-Learning一样,都是在智体的行为过程中迭代式地学习,但SARSA采用了和Q-Learning不同的迭代策略。SARSA算法实现如下:

for i in range(200):
    e = Env()
    action = epsilon_greedy(Q, e.present_state)
    while (e.is_end is False) and (e.step < MAX_STEP):
        state = e.present_state
        reward = e.interact(action)
        new_state = e.present_state
        new_action = epsilon_greedy(Q, e.present_state)
        Q[state, action] = (1 - ALPHA) * Q[state, action] + \
            ALPHA * (reward + GAMMA * Q[new_state, new_action])
        action = new_action

对比下Q-Learning算法Q函数的更新:

for i in range(200):
    e = Env()
    while (e.is_end is False) and (e.step < MAX_STEP):
        action = epsilon_greedy(Q, e.present_state)
        state = e.present_state
        reward = e.interact(action)
        new_state = e.present_state
        Q[state, action] = (1 - ALPHA) * Q[state, action] + \
            ALPHA * (reward + GAMMA * Q[new_state, :].max())

更详细代码可以参考Q-Learning,SARSA算法的更新步骤如下:

1)记录当前的状态保存到state;

2)执行上一步选好的action(同样使用epsilon_greedy算法选择下一个状态,一定概率随机游走,一定概率选择收益最大的那个状态)得到奖励reward和执行完此次action后得到的新状态new_state;

3) 在新的状态new_state,使用epsilon_greedy算法选择下一个action;

4)reward + GAMMA * Q[new_state, new_action]以一定的学习率来更新Q[state, action];

5)action = new_action 将会在下一个循环中执行。

可以看到SARSA算法的过程,智体从一个状态(state,S)出发,执行一个状态(action,A),得到奖励(Reward,R)和新的状态(S),在新的状态下选择一个新的动作(A),通过新的状态和新的动作来更新Q函数,因此算法的名字为:S-A-R-S-A->SARSA。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值