-
Notifications
You must be signed in to change notification settings - Fork 0
/
NARXAgents.jl
272 lines (201 loc) · 8.28 KB
/
NARXAgents.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
module NARXAgents
using Optim
using Distributions
using SpecialFunctions
using LinearAlgebra
export NARXAgent, update!, predictions, pol, crossentropy, mutualinfo, minimizeEFE, minimizeMSE, backshift, update_goals!
mutable struct NARXAgent
"""
Active inference agent based on a Nonlinear Auto-Regressive eXogenous model.
Parameters are inferred through Bayesian filtering and controls through minimizing expected free energy.
"""
ybuffer ::Vector{Float64}
ubuffer ::Vector{Float64}
delay_inp ::Integer
delay_out ::Integer
pol_degree ::Integer
order ::Integer
μ ::Vector{Float64} # Coefficients mean
Λ ::Matrix{Float64} # Coefficients precision
α ::Float64 # Likelihood precision shape
β ::Float64 # Likelihood precision rate
λ ::Float64 # Control prior precision
goals ::Union{Distribution{Univariate, Continuous}, Vector}
thorizon ::Integer
num_iters ::Integer
free_energy ::Float64
function NARXAgent(coefficients_mean,
coefficients_precision,
noise_shape,
noise_rate;
goal_prior=Normal(0.0, 1.0),
delay_inp::Integer=1,
delay_out::Integer=1,
pol_degree::Integer=1,
time_horizon::Integer=1,
num_iters::Integer=10,
control_prior_precision::Float64=0.0)
ybuffer = zeros(delay_out)
ubuffer = zeros(delay_inp+1)
order = size(pol(zeros(1 + delay_inp + delay_out), degree=pol_degree),1)
if order != length(coefficients_mean)
error("Dimensionality of coefficients and model order do not match.")
end
free_energy = Inf
return new(ybuffer,
ubuffer,
delay_inp,
delay_out,
pol_degree,
order,
coefficients_mean,
coefficients_precision,
noise_shape,
noise_rate,
control_prior_precision,
goal_prior,
time_horizon,
num_iters,
free_energy)
end
end
pol(x; degree::Integer = 1) = cat([1.0; [x.^d for d in 1:degree]]...,dims=1)
function update!(agent::NARXAgent, y::Float64, u::Float64)
agent.ubuffer = backshift(agent.ubuffer, u)
ϕ = pol([agent.ybuffer; agent.ubuffer], degree=agent.pol_degree)
μ0 = agent.μ
Λ0 = agent.Λ
α0 = agent.α
β0 = agent.β
agent.μ = inv(ϕ*ϕ' + Λ0)*(ϕ*y + Λ0*μ0)
agent.Λ = ϕ*ϕ' + Λ0
agent.α = α0 + 1/2
agent.β = β0 + 1/2*(y^2 + μ0'*Λ0*μ0 - (ϕ*y + Λ0*μ0)'*inv(ϕ*ϕ' + Λ0)*(ϕ*y + Λ0*μ0))
agent.ybuffer = backshift(agent.ybuffer, y)
agent.free_energy = -log(marginal_likelihood(agent, (μ0, Λ0, α0, β0)))
end
function params(agent::NARXAgent)
return agent.μ, agent.Λ, agent.α, agent.β
end
function marginal_likelihood(agent::NARXAgent, prior_params)
μn, Λn, αn, βn = params(agent)
μ0, Λ0, α0, β0 = prior_params
return (det(Λn)^(-1/2)*gamma(αn)*βn^αn)/(det(Λ0)^(-1/2)*gamma(α0)*β0^α0) * (2π)^(-1/2)
end
function posterior_predictive(agent::NARXAgent, ϕ_t)
"Posterior predictive distribution is location-scale t-distributed"
ν_t = 2*agent.α
m_t = dot(agent.μ, ϕ_t)
s2_t = agent.β/agent.α*(1 + ϕ_t'*inv(agent.Λ)*ϕ_t)
return ν_t, m_t, s2_t
end
function predictions(agent::NARXAgent, controls; time_horizon=1)
m_y = zeros(time_horizon)
v_y = zeros(time_horizon)
ybuffer = agent.ybuffer
ubuffer = agent.ubuffer
for t in 1:time_horizon
# Update control buffer
ubuffer = backshift(ubuffer, controls[t])
ϕ_t = pol([ybuffer; ubuffer], degree=agent.pol_degree)
ν_t, m_t, s2_t = posterior_predictive(agent, ϕ_t)
# Prediction
m_y[t] = m_t
v_y[t] = s2_t * ν_t/(ν_t - 2)
# Update previous
ybuffer = backshift(ybuffer, m_y[t])
end
return m_y, v_y
end
function mutualinfo(agent::NARXAgent, ϕ)
"Mutual information between parameters and posterior predictive (constant terms dropped)"
return -1/2*log( agent.β/agent.α*(1 + ϕ'*inv(agent.Λ)*ϕ) )
end
function crossentropy(agent::NARXAgent, goal::Distribution{Univariate, Continuous}, m_pred, v_pred)
"Cross-entropy between posterior predictive and goal prior (constant terms dropped)"
return ( v_pred + (m_pred - mean(goal))^2 ) / ( 2var(goal) )
# return (m_pred - mean(goal))^2/(2var(goal))
end
function EFE(agent::NARXAgent, goals, controls)
"Expected Free Energy"
ybuffer = agent.ybuffer
ubuffer = agent.ubuffer
J = 0
for t in 1:agent.thorizon
# Update control buffer
ubuffer = backshift(ubuffer, controls[t])
ϕ_t = pol([ybuffer; ubuffer], degree=agent.pol_degree)
# Prediction
ν_t, m_t, s2_t = posterior_predictive(agent, ϕ_t)
m_y = m_t
v_y = s2_t * ν_t/(ν_t - 2)
# Accumulate EFE
J += mutualinfo(agent, ϕ_t) + crossentropy(agent, goals[t], m_y, v_y) + agent.λ*controls[t]^2
# Update previous
ybuffer = backshift(ybuffer, m_y)
end
return J
end
function MSE(agent::NARXAgent, goals, controls)
"Mean Squared Error between prediction and setpoint."
ybuffer = agent.ybuffer
ubuffer = agent.ubuffer
J = 0
for t in 1:agent.thorizon
# Update control buffer
ubuffer = backshift(ubuffer, controls[t])
ϕ_t = pol([ybuffer; ubuffer], degree=agent.pol_degree)
# Prediction
m_y = dot(agent.μ, ϕ_t)
# Accumulate objective function
J += (mean(goals[t]) - m_y)^2 + agent.λ*controls[t]^2
# Update previous
ybuffer = backshift(ybuffer, m_y)
end
return J
end
function minimizeEFE(agent::NARXAgent, goals; u_0=nothing, time_limit=10, verbose=false, control_lims::Tuple=(-Inf,Inf))
"Minimize EFE objective and return policy."
if isnothing(u_0); u_0 = 1e-8*randn(agent.thorizon); end
opts = Optim.Options(time_limit=time_limit,
show_trace=verbose,
allow_f_increases=true,
g_tol=1e-12,
show_every=10,
iterations=10_000)
# Objective function
J(u) = EFE(agent, goals, u)
# Constrained minimization procedure
results = optimize(J, control_lims..., u_0, Fminbox(LBFGS()), opts, autodiff=:forward)
return Optim.minimizer(results)
end
function minimizeMSE(agent::NARXAgent, goals; u_0=nothing, time_limit=10, verbose=false, control_lims::Tuple=(-Inf,Inf))
"Minimize MSE objective and return policy."
if isnothing(u_0); u_0 = 1e-8*randn(agent.thorizon); end
opts = Optim.Options(time_limit=time_limit,
show_trace=verbose,
allow_f_increases=true,
g_tol=1e-12,
show_every=10,
iterations=10_000)
# Objective function
J(u) = MSE(agent, goals, u)
# Constrained minimization procedure
results = optimize(J, control_lims..., u_0, Fminbox(LBFGS()), opts, autodiff=:forward)
return Optim.minimizer(results)
end
function backshift(x::AbstractVector, a::Number)
"Shift elements down and add element"
N = size(x,1)
# Shift operator
S = Tridiagonal(ones(N-1), zeros(N), zeros(N-1))
# Basis vector
e = [1.0; zeros(N-1)]
return S*x + e*a
end
function update_goals!(x::AbstractVector, g::Distribution{Univariate, Continuous})
"Move goals forward and add a final goal"
circshift!(x,-1)
x[end] = g
end
end