-
Notifications
You must be signed in to change notification settings - Fork 3
/
algorithm_s.py
executable file
·60 lines (46 loc) · 1.16 KB
/
algorithm_s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
# +-----------------------------------------------+
# | 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