-
Notifications
You must be signed in to change notification settings - Fork 69
/
rl.jl
193 lines (176 loc) · 5.94 KB
/
rl.jl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Distributions
using StatsBase
using Random
include("gridworld.jl")
include("helpers.jl")
mutable struct MappedDiscreteMDP{SType,AType} <: MDP{SType,AType}
S::Vector{SType}
A::Vector{AType}
T::Array{Float64,3}
R::Matrix{Float64}
discount::Float64
stateIndex::Dict
actionIndex::Dict
nextStates
end
function MappedDiscreteMDP(S::Vector, A::Vector, T, R; discount=0.9)
stateIndex = Dict([S[i]=>i for i in 1:length(S)])
actionIndex = Dict([A[i]=>i for i in 1:length(A)])
nextStates = Dict([(S[si], A[ai])=>S[findall(x->x!=0, T[si, ai, :])] for si=1:length(S), ai=1:length(A)])
MappedDiscreteMDP(S, A, T, R, discount, stateIndex, actionIndex, nextStates)
end
MappedDiscreteMDP(S::Vector, A::Vector; discount=0.9) =
MappedDiscreteMDP(S, A,
zeros(length(S), length(A), length(S)),
zeros(length(S), length(A)),
discount=discount)
actions(mdp::MappedDiscreteMDP) = mdp.A
states(mdp::MappedDiscreteMDP) = mdp.S
n_states(mdp::MappedDiscreteMDP) = length(mdp.S)
n_actions(mdp::MappedDiscreteMDP) = length(mdp.A)
reward(mdp::MappedDiscreteMDP, s, a) = mdp.R[mdp.stateIndex[s], mdp.actionIndex[a]]
transition_pdf(mdp::MappedDiscreteMDP, s0, a, s1) = mdp.T[mdp.stateIndex[s0], mdp.actionIndex[a], mdp.stateIndex[s1]]
discount(mdp::MappedDiscreteMDP) = mdp.discount
state_index(mdp::MappedDiscreteMDP, s) = mdp.stateIndex[s]
action_index(mdp::MappedDiscreteMDP, a) = mdp.actionIndex[s]
next_states(mdp::MappedDiscreteMDP, s, a) = mdp.nextStates[(s, a)]
rand_state(mdp::MDP) = states(mdp)[rand(DiscreteUniform(1,n_states(mdp)))]
function value_iteration(mdp::MDP, iterations::Integer)
V = zeros(n_states(mdp))
Q = zeros(n_states(mdp), n_actions(mdp))
value_iteration!(V, Q, mdp, iterations)
(V, Q)
end
function value_iteration!(V::Vector, Q::Matrix, mdp::MDP, iterations::Integer)
(S, A, T, R, discount) = locals(mdp)
V_old = copy(V)
for i = 1:iterations
for s0i in 1:n_states(mdp)
s0 = S[s0i]
for ai = 1:n_actions(mdp)
a = A[ai]
Q[s0i,ai] = R(s0, a) + discount * sum([0.0; [T(s0, a, s1)*V_old[state_index(mdp, s1)] for s1 in next_states(mdp, s0, a)]])
end
V[s0i] = maximum(Q[s0i,:])
end
copyto!(V_old, V)
end
end
function update_parameters!(mdp::MappedDiscreteMDP, N, Nsa, ρ, s, a)
si = mdp.stateIndex[s]
ai = mdp.actionIndex[a]
denom = Nsa[si, ai]
mdp.T[si, ai, :] = N[si, ai, :] ./ denom
mdp.R[si, ai] = ρ[si, ai] / denom
mdp.nextStates[(s, a)]= mdp.S[findall(x->x!=0, mdp.T[si, ai, :])]
end
function isterminal(mdp::MDP, s0, a)
S1 = next_states(mdp, s0, a)
length(S1) == 0 || 0 == sum(s1 -> transition_pdf(mdp, s0, a, s1), S1)
end
function generate_s(mdp::MDP, s0, a, rng::AbstractRNG=Random.GLOBAL_RNG)
p = [transition_pdf(mdp, s0, a, s1) for s1 in states(mdp)]
s1i = sample(rng, Weights(p))
states(mdp)[s1i]
end
mutable struct MLRL <: Policy
N::Array{Float64,3} # transition counts
Nsa::Matrix{Float64} # state-action counts
ρ::Matrix{Float64} # sum of rewards
lastState
lastAction
lastReward
newEpisode
mdp::MappedDiscreteMDP
Q::Matrix{Float64}
V::Vector{Float64}
iterations::Int
epsilon::Float64 # probability of exploration
function MLRL(S, A; discount=0.9, iterations=20, epsilon=0.2)
N = zeros(length(S), length(A), length(S))
Nsa = zeros(length(S), length(A))
ρ = zeros(length(S), length(A))
lastState = nothing
lastAction = nothing
lastReward = 0.
mdp = MappedDiscreteMDP(S, A, discount=discount)
Q = zeros(length(S), length(A))
V = zeros(length(S))
newEpisode = true
new(N, Nsa, ρ, lastState, lastAction, lastReward, newEpisode, mdp, Q, V, iterations, epsilon)
end
end
function reset(policy::MLRL)
if !policy.newEpisode
s0i = policy.mdp.stateIndex[policy.lastState]
ai = policy.mdp.actionIndex[policy.lastAction]
policy.Nsa[s0i, ai] += 1
policy.ρ[s0i, ai] = policy.lastReward
# update Q and V
update_parameters!(policy.mdp, policy.N, policy.Nsa, policy.ρ, policy.lastState, policy.lastAction)
value_iteration!(policy.V, policy.Q, policy.mdp, policy.iterations)
policy.newEpisode = true
end
end
function update(policy::MLRL, s, a, r)
if policy.newEpisode
policy.newEpisode = false
else
s0i = policy.mdp.stateIndex[policy.lastState]
ai = policy.mdp.actionIndex[policy.lastAction]
s1i = policy.mdp.stateIndex[s]
policy.N[s0i, ai, s1i] += 1
policy.Nsa[s0i, ai] += 1
policy.ρ[s0i, ai] += policy.lastReward
# update Q and V
update_parameters!(policy.mdp, policy.N, policy.Nsa, policy.ρ, policy.lastState, policy.lastAction)
value_iteration!(policy.V, policy.Q, policy.mdp, policy.iterations)
end
policy.lastState = s
policy.lastAction = a
policy.lastReward = r
nothing
end
function action(policy::MLRL, s)
si = policy.mdp.stateIndex[s]
Qs = policy.Q[si, :]
ais = findall((in)(maximum(Qs)), Qs)
ai = rand(ais)
policy.mdp.A[ai]
end
function action(policy::MLRL)
if rand() < policy.epsilon
policy.mdp.A[rand(DiscreteUniform(1,numActions(policy.mdp)))]
else
action(policy, policy.lastState)
end
end
function simulate(mdp::MDP, steps::Integer, policy::Policy; script=[])
S = Any[]
V = Any[]
R = Float64[]
if length(script) == 0
s = rand_state(mdp)
else
s = script[1]
end
for i = 1:steps
push!(S, s)
a = action(policy, s)
r = reward(mdp, s, a)
push!(R, r)
update(policy, s, a, r)
push!(V, copy(policy.V))
if i < length(script)
s = script[i + 1]
else
if isterminal(mdp, s, a)
s = rand_state(mdp)
reset(policy)
else
s = generate_s(mdp, s, a)
end
end
end
(S, R, V)
end