强化学习学习总结(三)——Sarsa

本文详细探讨了Q-Learning与SARSA两种强化学习算法的区别,重点介绍了它们的学习模式和更新方式的不同。通过具体代码示例,展示了SARSA算法的工作流程和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.qlearning和sarsa 区别

(1)qlearning——off-policy:离线

(2)sarsa——on-policy:在线

 

2.程序

学习模式不同

class SarsaTable(RL):

    def __init__(self, actions, learning_rate=0.01, reward_decay=0.9, e_greedy=0.9):
        super(SarsaTable, self).__init__(actions, learning_rate, reward_decay, e_greedy)

    def learn(self, s, a, r, s_, a_):
        self.check_state_exist(s_)
        q_predict = self.q_table.loc[s, a]
        if s_ != 'terminal':
            q_target = r + self.gamma * self.q_table.loc[s_, a_]  # next state is not terminal
        else:
            q_target = r  # next state is terminal
        self.q_table.loc[s, a] += self.lr * (q_target - q_predict)  # update

更新模式不同

def update():
    for episode in range(100):
        # initial observation
        observation = env.reset()
        # action
        action = RL.choose_action(str(observation))

        while True:
            # fresh env
            env.render()

            # 获取奖励和观测值和是否结束
            observation_, reward, done = env.step(action)
            # 下一步action
            action_ = RL.choose_action(str(observation_))
            #  Sarsa
            RL.learn(str(observation), action, reward, str(observation_), action_)
            # 更新 observation and action
            observation = observation_
            action = action_

            # break while loop when end of this episode
            if done:
                break

    # end of game
    print('game over')
    env.destroy()

if __name__ == "__main__":
    env = Maze()
    RL = SarsaTable(actions=list(range(env.n_actions)))

    env.after(100, update)
    env.mainloop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值