-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcorrelation_robustness.jl
269 lines (225 loc) · 9.08 KB
/
correlation_robustness.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
using Multilane
using MCTS
using POMDPToolbox
using POMDPs
# using POMCP
using Missings
using DataFrames
using CSV
using POMCPOW
@everywhere using Missings
@everywhere using Multilane
@everywhere using POMDPToolbox
@show N = 5000
n_iters = 1000
max_time = Inf
max_depth = 40
val = SimpleSolver()
pp = PhysicalParam(4, lane_length=100.0)
lambda = 0.0
rmodel = SuccessReward(lambda=lambda)
alldata = DataFrame()
dpws = DPWSolver(depth=max_depth,
n_iterations=n_iters,
max_time=max_time,
exploration_constant=8.0,
k_state=4.5,
alpha_state=1/10.0,
enable_action_pw=false,
check_repeat_state=false,
estimate_value=RolloutEstimator(val)
# estimate_value=val
)
dpws_x10 = deepcopy(dpws)
dpws_x10.n_iterations *= 10
function make_updater(cor, problem, k, rng_seed)
wup = WeightUpdateParams(smoothing=0.0, wrong_lane_factor=0.05)
if cor >= 1.0 || k == "meanmpc"
return AggressivenessUpdater(problem, 2000, 0.05, 0.1, wup, MersenneTwister(rng_seed+50000))
else
return BehaviorParticleUpdater(problem, 5000, 0.0, 0.0, wup, MersenneTwister(rng_seed+50000))
end
end
pow_updater(up::AggressivenessUpdater) = AggressivenessPOWFilter(up.params)
pow_updater(up::BehaviorParticleUpdater) = BehaviorPOWFilter(up.params)
solvers = Dict{String, Solver}(
"omniscient" => dpws,
"qmdp" => QBSolver(dpws),
"pomcpow" => POMCPOWSolver(tree_queries=n_iters,
criterion=MaxUCB(8.0),
max_depth=max_depth,
max_time=max_time,
enable_action_pw=false,
k_observation=4.5,
alpha_observation=1/10.0,
estimate_value=FORollout(val),
check_repeat_obs=false,
),
"outcome" => OutcomeSolver(dpws)
)
planners = Dict{Pair{String, Float64}, Any}(("omniscient"=>NaN)=>nothing) # maps to planner => updater pairs
for sname in ["pomcpow", "qmdp", "outcome"]
for planner_cor in [0.0, 1.0]
@show sname => planner_cor
planner_behaviors = standard_uniform(correlation=planner_cor)
planner_dmodel = NoCrashIDMMOBILModel(10, pp,
behaviors=planner_behaviors,
p_appear=1.0,
lane_terminate=true,
max_dist=1000.0,
brake_terminate_thresh=4.0,
speed_terminate_thresh=15.0
)
planner_pomdp = NoCrashPOMDP{typeof(rmodel), typeof(planner_behaviors)}(planner_dmodel, rmodel, 0.95, false)
planner_mdp = NoCrashMDP{typeof(rmodel), typeof(planner_behaviors)}(planner_dmodel, rmodel, 0.95, false)
sol = solvers[sname]
solver_problems = Dict{String, Any}(
"qmdp"=>planner_mdp,
"outcome"=>planner_mdp
)
updaters = Dict{String, Any}(
"qmdp"=>make_updater(planner_cor, planner_pomdp, sname, 50000),
"pomcpow"=>make_updater(planner_cor, planner_pomdp, sname, 50000),
"outcome"=>nothing,
)
sp = get(solver_problems, sname, planner_pomdp)
up = updaters[sname]
if sname == "pomcpow"
sol.node_sr_belief_updater = pow_updater(up)
end
planners[sname=>planner_cor] = (solve(sol, sp) => up)
end
end
for cor in 0.0:0.2:1.0
# for cor in 1.0
@show cor
behaviors = standard_uniform(correlation=cor)
dmodel = NoCrashIDMMOBILModel(10, pp,
behaviors=behaviors,
p_appear=1.0,
lane_terminate=true,
max_dist=1000.0,
brake_terminate_thresh=4.0,
speed_terminate_thresh=15.0
)
pomdp = NoCrashPOMDP{typeof(rmodel), typeof(behaviors)}(dmodel, rmodel, 0.95, false)
mdp = NoCrashMDP{typeof(rmodel), typeof(behaviors)}(dmodel, rmodel, 0.95, false)
problems = Dict{String, Any}(
"baseline"=>mdp,
"omniscient"=>mdp,
"omniscient-x10"=>mdp,
"outcome"=>mdp
)
for ((k, planner_cor),pup) in planners
@show k
@show planner_cor
p = get(problems, k, pomdp)
sim_problem = deepcopy(p)
sim_problem.throw=true
if pup == nothing
planner = solve(solvers[k], p)
else
(planner, up) = pup
end
sims = []
for i in 1:N
rng_seed = i+40000
rng = MersenneTwister(rng_seed)
is = initial_state(p, rng)
ips = MLPhysicalState(is)
metadata = Dict(:rng_seed=>rng_seed,
:lambda=>lambda,
:solver=>k,
:dt=>pp.dt,
:cor=>cor,
:planner_cor=>planner_cor
)
hr = HistoryRecorder(max_steps=100, rng=rng, capture_exception=false)
if sim_problem isa POMDP
srand(planner, rng_seed+80000)
push!(sims, Sim(sim_problem, planner, up, ips, is,
simulator=hr,
metadata=metadata
))
else
push!(sims, Sim(sim_problem, planner, is,
simulator=hr,
metadata=metadata
))
end
@assert problem(last(sims)).throw
end
# data = run(sims) do sim, hist
data = run_parallel(sims) do sim, hist
if isnull(exception(hist))
p = problem(sim)
steps_in_lane = 0
steps_to_lane = missing
nb_brakes = 0
crashed = false
min_speed = Inf
min_ego_speed = Inf
for (k,(s,sp)) in enumerate(eachstep(hist, "s,sp"))
nb_brakes += detect_braking(p, s, sp)
if sp.cars[1].y == p.rmodel.target_lane
steps_in_lane += 1
end
if sp.cars[1].y == p.rmodel.target_lane
if ismissing(steps_to_lane)
steps_to_lane = k
end
end
if is_crash(p, s, sp)
crashed = true
end
min_speed = min(minimum(c.vel for c in sp.cars), min_speed)
min_ego_speed = min(min_ego_speed, sp.cars[1].vel)
end
time_to_lane = steps_to_lane*p.dmodel.phys_param.dt
distance = last(state_hist(hist)).x
return [:n_steps=>n_steps(hist),
:mean_iterations=>mean(ai[:tree_queries] for ai in eachstep(hist, "ai")),
:mean_search_time=>1e-6*mean(ai[:search_time_us] for ai in eachstep(hist, "ai")),
:reward=>discounted_reward(hist),
:crashed=>crashed,
:steps_to_lane=>steps_to_lane,
:steps_in_lane=>steps_in_lane,
:nb_brakes=>nb_brakes,
:exception=>false,
:distance=>distance,
:mean_ego_speed=>distance/(n_steps(hist)*p.dmodel.phys_param.dt),
:min_speed=>min_speed,
:min_ego_speed=>min_ego_speed,
:terminal=>string(get(last(state_hist(hist)).terminal, missing))
]
else
warn("Error in Simulation")
showerror(STDERR, get(exception(hist)))
# show(STDERR, MIME("text/plain"), stacktrace(get(backtrace(hist))))
return [:exception=>true,
:ex_type=>string(typeof(get(exception(hist))))
]
end
end
success = 100.0*sum(data[:terminal].=="lane")/N
brakes = 100.0*sum(data[:nb_brakes].>=1)/N
@printf("%% reaching:%5.1f; %% braking:%5.1f\n", success, brakes)
@show extrema(data[:distance])
@show mean(data[:mean_iterations])
@show mean(data[:mean_search_time])
@show mean(data[:reward])
if minimum(data[:min_speed]) < 15.0
@show minimum(data[:min_speed])
end
if isempty(alldata)
alldata = data
else
alldata = vcat(alldata, data)
end
end
end
# @show alldata
datestring = Dates.format(now(), "E_d_u_HH_MM")
filename = Pkg.dir("Multilane", "data", "cor_rob_"*datestring*".csv")
println("Writing data to $filename")
CSV.write(filename, alldata)