1 Star 0 Fork 0

焦建军/RL-ROBOT

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
algorithm_s.py 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
angelmtenor 提交于 2022-02-27 17:43 . style: Apply basic pre-commit format
# +-----------------------------------------------+
# | RL-ROBOT. Reinforcement Learning for Robotics |
# | Angel Martinez-Tenor |
# | MAPIR. University of Malaga. 2016 |
# +-----------------------------------------------+
""" SARSA algorithm """
import time
import numpy as np
import agent
import exp
import lp
ID = "S"
CAPTION = "SARSA"
def setup():
"""Setup algorithm"""
pass # no needed here
def execute():
"""Execute the learning algorithm"""
s = lp.s
a = lp.a
alpha = lp.alpha
q = lp.q
v = lp.v
policy = lp.policy
agent.execute_action(a)
time.sleep(lp.step_time)
sp = agent.observe_state()
r = agent.obtain_reward(s, a, sp)
ap = agent.select_action(sp)
# update Q
delta = r + exp.GAMMA * q[sp, ap] - q[s, a] # TD error (SARSA)
q[s, a] = q[s, a] + alpha * delta # update rule
# Update V and Policy
v[s] = np.max(q[s])
policy[s] = np.argmax(q[s])
lp.s = s
lp.a = a
lp.sp = sp
lp.ap = ap
lp.r = r
lp.alpha = alpha
lp.q = q
lp.v = v
lp.policy = policy
lp.delta = delta
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiaojianjun-com/RL-ROBOT.git
git@gitee.com:jiaojianjun-com/RL-ROBOT.git
jiaojianjun-com
RL-ROBOT
RL-ROBOT
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385