-
Notifications
You must be signed in to change notification settings - Fork 0
/
landing_mdp.jl
428 lines (369 loc) · 13 KB
/
landing_mdp.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
landing_mdp.jl
- Implements landing mdp for uam landing application of preference based encounter modeling
05/2019 S. M. Katz smkatz@stanford.edu
"""
using LinearAlgebra
using GridInterpolations
using POMDPs
using POMDPModelTools
using DiscreteValueIteration
using SparseArrays
using JLD2
using Distributions
#using Plots
using Random
Random.seed!(18)
include("ri_types.jl")
include("ri_const.jl")
"""
----------------------------------------------
General Constants
----------------------------------------------
"""
ft2m = 0.3048
g = 9.81
fpm2mps = 0.00508
"""
----------------------------------------------
Constants - Fill in!
----------------------------------------------
"""
# Define state discretizations
altitudes = collect(range(0, stop=500*ft2m, length=50))
vertical_rates = collect(range(-500*fpm2mps, stop=0, length=4))
ground_speeds = collect(range(0, stop=60, length=15))
# Define actions (actions are joint horizontal and vertical accelerations)
a2ind = Dict(:z̈₁ÿ₁ => 1, :z̈₁ÿ₂ => 2, :z̈₁ÿ₃ => 3, :z̈₁my => 4,
:z̈₂ÿ₁ => 5, :z̈₂ÿ₂ => 6, :z̈₂ÿ₃ => 7, :z̈₂my => 8,
:z̈₃ÿ₁ => 9, :z̈₃ÿ₂ => 10, :z̈₃ÿ₃ => 11, :z̈₃my => 12,
:mzÿ₁ => 13, :mzÿ₂ => 14, :mzÿ₃ => 15, :mzmy => 16)
a2val = Dict(:z̈₁ÿ₁ => [0.01, -0.05g], :z̈₁ÿ₂ => [0.01, -0.1g], :z̈₁ÿ₃ => [0.01, -0.2g], :z̈₁my => [0.01, 0.0],
:z̈₂ÿ₁ => [0.025, -0.05g], :z̈₂ÿ₂ => [0.025, -0.1g], :z̈₂ÿ₃ => [0.025, -0.2g], :z̈₂my => [0.025, 0.0],
:z̈₃ÿ₁ => [0.05, -0.05g], :z̈₃ÿ₂ => [0.05, -0.1g], :z̈₃ÿ₃ => [0.05, -0.2g], :z̈₃my => [0.05, 0.0],
:mzÿ₁ => [0.0, -0.05g], :mzÿ₂ => [0.0, -0.1g], :mzÿ₃ => [0.0, -0.2g], :mzmy => [0.0, 0.0])
nA = length(a2ind)
prev_action = collect(1:nA)
discount_factor = 0.999
# deleted g from here !!!!!!!!
dt = 4
actions_array = [:z̈₁ÿ₁, :z̈₁ÿ₂, :z̈₁ÿ₃, :z̈₁my, :z̈₂ÿ₁, :z̈₂ÿ₂, :z̈₂ÿ₃, :z̈₂my, :z̈₃ÿ₁, :z̈₃ÿ₂, :z̈₃ÿ₃, :z̈₃my, :mzÿ₁, :mzÿ₂, :mzÿ₃, :mzmy]
@load "Transition_mat.jld2" T
"""
----------------------------------------------
Define the landing mdp and its properties
----------------------------------------------
"""
struct landingMDP <: MDP{Int64, Symbol}
grid::RectangleGrid
nA::Int64
α::Float64
β::Float64
γ::Float64
landing_reward::Float64
backwards_pen::Float64
end
"""
function landing_mdp
INPUTS:
- α: magnitude of jerk penalty
- β: magnitude of landing speed penalty
- γ: magnitude of acceleration penalty
- landing_reward: reward for landing
- backwards_pen: magnitude of penalty for going backwards
OUTPUTS:
- mdp for uam landing that can be solved using POMDPs.jl
"""
function landingMDP(;α=1, β=1, γ=1, landing_reward=10000, backwards_pen=0.1)
state_grid = RectangleGrid(altitudes, vertical_rates, ground_speeds, prev_action)
return landingMDP(state_grid, nA, α, β, γ, landing_reward, backwards_pen)
end
"""
----------------------------------------------
Functions for POMDPs.jl
----------------------------------------------
"""
function POMDPs.states(mdp::landingMDP)
return collect(0:length(mdp.grid))
end
function POMDPs.n_states(mdp::landingMDP)
return length(mdp.grid)+1
end
function POMDPs.stateindex(mdp::landingMDP, state::Int64)
return state+1
end
function POMDPs.actions(mdp::landingMDP)
# Need to select joint actions!
# (vertical accels positive because want to slow down descending)
# z̈₁ - 0.01 m/s²
# z̈₂ - 0.025 m/s²
# z̈₃ - 0.05 m/s²
# mz - 0
# ÿ₁ - -0.05g
# ÿ₂ - -0.1g
# ÿ₃ - -0.2g
# my - 0
return actions_array
end
function POMDPs.n_actions(mdp::landingMDP)
return mdp.nA
end
function POMDPs.actionindex(mdp::landingMDP, a::Symbol)
return a2ind[a]
end
function POMDPs.discount(mdp::landingMDP)
return discount_factor
end
function POMDPs.transition(mdp::landingMDP, s::Int64, a::Symbol)
if s == 0 # in terminal state
return SparseCat(0, 0)
else
znext, żnext, ẏnext = next_state_vals(mdp, s, a)
prev_actionnext = a2ind[a]
states, probs = interpolants(mdp.grid, [znext, żnext, ẏnext, prev_actionnext])
if znext ≤ 0 # Transition to terminal state (0) (has landed)
states = 0
probs = 1
end
# Create sparse categorical distribution from this
returnDist = SparseCat(states, probs)
return returnDist
end
end
function POMDPs.reward(mdp::landingMDP, s::Int64, a::Symbol)
s_grid = ind2x(mdp.grid, s)
z̈, ÿ = a2val[a][1], a2val[a][2]
z, ż, ẏ = s_grid[1], s_grid[2], s_grid[3]
prev_action_ind = convert(Int64, s_grid[4])
prev_action = actions_array[prev_action_ind]
znext, żnext, ẏnext = next_state_vals(mdp, s, a)
# Initialize reward to 0
r = 0.0
# Incentivize landing
if znext ≤ 0.0
r += mdp.landing_reward
end
# Penalize acceleration
r -= 2*mdp.γ*norm([z̈, ÿ])
# Penalize jerk
r -= 20*mdp.α*norm([z̈, ÿ] - a2val[prev_action])/dt
# Penalize speed at landing
if 0 < z < 50*ft2m
r -= 3*mdp.β*min(norm([ż, ẏ])/z, 1000) # CHANGE THIS!!!!
end
# Penalize going backwards
r -= mdp.backwards_pen*max(0, -ẏnext)
return r
end
function POMDPs.isterminal(mdp::landingMDP, s::Int64)
if s == 0
return true
else
return false
end
end
"""
----------------------------------------------
Supplementary Functions
----------------------------------------------
"""
function next_state_vals(mdp::landingMDP, s::Int64, a::Symbol)
# First, convert to a grid state
s_grid = ind2x(mdp.grid, s)
# Convert action to acceleration
z̈, ÿ = a2val[a][1], a2val[a][2]
z, ż, ẏ = s_grid[1], s_grid[2], s_grid[3]
znext = z + ż*dt
żnext = ż + z̈*dt
ẏnext = ẏ + ÿ*dt
return znext, żnext, ẏnext
end
"""
----------------------------------------------
Solve
----------------------------------------------
function solve_landing_mdp
- solves mdp using value iteration from POMDPs.jl
INPUTS:
- α: magnitude of jerk penalty
- β: magnitude of landing speed penalty
- γ: magnitude of acceleration penalty
- landing_reward: reward for landing
- verbose: boolean whether or not to show output from value iteration solver
- init_util: whether or not initial utility values will be specified
"""
function solve_landing_mdp(;α=1, β=1, γ=1, landing_reward=10000, verbose=false, init_util=false)
mdp = landingMDP(α=α, β=β, γ=γ, landing_reward=landing_reward)
if init_util
solver = ValueIterationSolver(verbose=verbose, init_util=util)
else
solver = ValueIterationSolver(verbose=verbose)
end
policy = solve(solver, mdp)
return mdp, policy
end
"""
function solve_landing_mdp
- solves mdp using sparse value iteration from POMDPs.jl (modified slightly so that it does
not recalculate T every time it is solved - only recalculates R)
INPUTS:
- α: magnitude of jerk penalty
- β: magnitude of landing speed penalty
- γ: magnitude of acceleration penalty
- landing_reward: reward for landing
- verbose: boolean whether or not to show output from value iteration solver
- init_util: whether or not initial utility values will be specified
"""
function solve_landing_mdp_sparse(;α=1, β=1, γ=1, landing_reward=10000, verbose=false, init_util=false)
mdp = landingMDP(α=α, β=β, γ=γ, landing_reward=landing_reward)
if init_util
solver = SparseValueIterationSolver(verbose=verbose, init_util=util)
else
solver = SparseValueIterationSolver(verbose=verbose)#, init_T=T)
end
policy = solve(solver, mdp)
return mdp, policy
end
"""
- This is taken straight from the DiscreteValueIteration package
- Use it to build T so that I do not have to redo it every time
I solve the mdp
"""
function transition_matrix_a_s_sp(mdp::MDP)
# Thanks to zach
na = n_actions(mdp)
ns = n_states(mdp)
transmat_row_A = [Float64[] for _ in 1:n_actions(mdp)]
transmat_col_A = [Float64[] for _ in 1:n_actions(mdp)]
transmat_data_A = [Float64[] for _ in 1:n_actions(mdp)]
for s in states(mdp)
si = stateindex(mdp, s)
for a in actions(mdp, s)
ai = actionindex(mdp, a)
if !isterminal(mdp, s) # if terminal, the transition probabilities are all just zero
td = transition(mdp, s, a)
for (sp, p) in weighted_iterator(td)
if p > 0.0
spi = stateindex(mdp, sp)
push!(transmat_row_A[ai], si)
push!(transmat_col_A[ai], spi)
push!(transmat_data_A[ai], p)
end
end
end
end
end
transmats_A_S_S2 = [sparse(transmat_row_A[a], transmat_col_A[a], transmat_data_A[a], n_states(mdp), n_states(mdp)) for a in 1:n_actions(mdp)]
# Note: not valid for terminal states
# @assert all(all(sum(transmats_A_S_S2[a], dims=2) .≈ ones(n_states(mdp))) for a in 1:n_actions(mdp)) "Transition probabilities must sum to 1"
return transmats_A_S_S2
end
"""
----------------------------------------------
Generate trajectory using the policy
----------------------------------------------
function generate_trajectory
INPUTS:
- mdp: mdp to generate trajectories from
- optimal policy for the mdp (need to solve it!)
- init_state: initial state for the trajectory
- num_steps: number of steps in the trajectory
- dt: time step
OUTPUTS:
- trajectory resulting from rollout
"""
function generate_trajectory(mdp, policy, init_state; num_steps=60, dt=1)
# Convert initial state to grid state
states, probs = interpolants(mdp.grid, init_state)
s = states[argmax(probs)]
z = zeros(num_steps)
y = zeros(num_steps)
ż = zeros(num_steps)
ẏ = zeros(num_steps)
z̈ = zeros(num_steps)
ÿ = zeros(num_steps)
z[1], ż[1], ẏ[1] = init_state[1], init_state[2], init_state[3]
for i = 2:num_steps
a = action(policy, s)
z̈[i], ÿ[i] = a2val[a][1], a2val[a][2]
ż[i] = ż[i-1] + z̈[i]*dt
ẏ[i] = ẏ[i-1] + ÿ[i]*dt
z[i] = z[i-1] + ż[i-1]*dt + 0.5*z̈[i]*dt^2
if z[i] < 0
y[i:end] = (y[i-1] + ẏ[i-1]*dt + 0.5*ÿ[i]*dt^2)*ones(num_steps-i+1)
break
end
y[i] = y[i-1] + ẏ[i-1]*dt + 0.5*ÿ[i]*dt^2
states, probs = interpolants(mdp.grid, [z[i], ż[i], ẏ[i]])
s = states[argmax(probs)]
end
return Trajectory(y, z, ẏ, ż, ÿ, z̈)
end
"""
function plot_τset
INPUTS:
- τset: set of trajectories to plot
OUTPUTS:
- p: plot object
"""
function plot_τset(τset)
minz = 0
miny, maxz, maxy = 0, 0, 0
for i = 1:length(τset)
miny = minimum(τset[i].y) > miny ? minimum(τset[i].y) : miny
maxz = maximum(τset[i].z) > maxz ? maximum(τset[i].z) : maxz
maxy = maximum(τset[i].y) > maxy ? maximum(τset[i].y) : maxy
end
p = Plots.plot(xlabel="East (m)", ylabel="Altitude (m)", title="Landing 1", legend=false, aspect_ratio=:equal, size=(1200,600), xlims=(miny,maxy+50), ylims=(minz,maxz+10))
for i = 1:length(τset)
Plots.plot!(p, τset[i].y, τset[i].z, linewidth=4)
Plots.plot!(p, [τset[i].y[1:20:end]], [τset[i].z[1:20:end]], seriestype=:scatter, markersize=3)
end
return p
end
"""
function generate_trajectory_set
INPUTS:
- init_states: initial states for trajectories in the set
- w: vector of weights to generate trajectory for
- dt: time step
OUTPUTS:
- τset: set of trajectories from rollouts of the policy obtained from w
"""
function generate_trajectory_set(init_states, w; dt=0.1)
mdp, policy = solve_landing_mdp_sparse(α=w[1], β=w[2], γ=w[3], init_util=false, verbose=false) # TODO: Add. init_util capabilitstate
τset = Vector{Trajectory}()
for i = 1:length(init_states[1])
init_state = [init_states[j][i] for j in 1:length(init_states)]
push!(τset, generate_trajectory(mdp, policy, init_state, num_steps=k*15, dt=dt))
end
return τset
end
# Function for just solving everything at once and generating trajectory plots
"""
function solve_mdp
INPUTS:
- α: magnitude of jerk penalty
- β: magnitude of landing speed penalty
- γ: magnitude of acceleration penalty
- num_trajectories: number of trajectories to generate from mdp
- init_states: initial states for the trajectories
(initial states are chosen randomly if init_states=[])
OUTPUTS:
- init_states: the initial states used to generate the trajectories
(if init_states were specified as an input, it will just return these)
- τset: set of trajectories from rollouts of the policy obtained from the
specified reward function parameters (α, β, γ)
"""
function solve_mdp(α, β, γ; num_trajectories=5, init_states=[])
if init_states == []
init_states = rand.(Uniform.(lb_s₀, ub_s₀), num_trajectories)
end
start = time()
τset = generate_trajectory_set(init_states, [α, β, γ])
println("Elapsed time: $(round(time() - start, digits=2))")
p = plot_τset(τset)
display(p)
println("Φ: $(get_Φ(τset))")
return init_states, τset
end