Skip to content

Commit

Permalink
prep for Turing
Browse files Browse the repository at this point in the history
  • Loading branch information
raunakbh92 committed Jun 17, 2020
1 parent 11fe1b9 commit e742b5f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 26 deletions.
56 changes: 45 additions & 11 deletions scripts/experiments.jl
Expand Up @@ -235,12 +235,14 @@ StatsPlots.savefig(d,"media/speed_hist/density_567.svg")
# Make videos from scenelists
#******************************
f = FilteringEnvironment()
num = 3;
prefix = "upper";
ext = "mp4";
filename = "media/$(prefix)_$(num).jld";
scenelist_pf = scenelist_from_jld_pf(f,filename=filename);
scenelist2video(f,scenelist_pf,filename="media/turing/$(prefix)_$(num)_pf.$(ext)");

for num in 1:1
filename = "media/$(prefix)_$(num).jld";
scenelist_pf = scenelist_from_jld_pf(f,filename=filename);
scenelist2video(f,scenelist_pf,filename="media/turing/$(prefix)_$(num)_pf_new2.$(ext)");
end

id_list,ts,te = JLD.load(filename,"veh_id_list","ts","te");
scenelist = replay_scenelist(f,id_list=id_list,ts=ts,te=te);
Expand All @@ -253,11 +255,43 @@ function speed_dist_moments(v_array,v_array_idm,v_array_cidm,v_array_lmidm,v_arr
mu_cidm,sig_cidm = StatsBase.mean_and_std(v_array_cidm)
mu_lmidm,sig_lmidm = StatsBase.mean_and_std(v_array_lmidm)
mu_pf,sig_pf = StatsBase.mean_and_std(v_array_pf)

end
end

#********************Train upper test lower******************
# We need to show a variability in the generated scenarios
# So we need to combine the particle sets of different vehicles together
# And then for the same set of vehicles in the lower merge i.e. test domain
# We show significantly different driving behavior by sampling from the particle set
#********************Cooperation params for Turing test******************
models,id_list,ts,te = JLD.load("media/upper_1.jld","m","veh_id_list","ts","te");
models[19].c = 0.9
models[6].c = 0.8
#models[19].idm.d_max=6.0
#models[29].idm.d_max=4.0
te=te+20
scenelist_pf = scenelist_from_jld_pf_debug(f,models,id_list,ts,te);
scenelist2video(f,scenelist_pf,filename="media/turing/upper_1_pf_turing.mp4");

models,id_list,ts,te = JLD.load("media/upper_5.jld","m","veh_id_list","ts","te");
models[194].c = 0.7
models[193].idm.d_max=2.5
te=te+30
scenelist_pf = scenelist_from_jld_pf_debug(f,models,id_list,ts,te);
scenelist2video(f,scenelist_pf,filename="media/turing/upper_5_pf_turing.mp4");

models,id_list,ts,te = JLD.load("media/upper_6.jld","m","veh_id_list","ts","te");
models[211].idm.d_max=6.0
models[215].idm.d_max=6.0
models[217].idm.d_max=2.0
te = te+20
scenelist_pf = scenelist_from_jld_pf_debug(f,models,id_list,ts,te);
scenelist2video(f,scenelist_pf,filename="media/turing/upper_6_pf_turing.mp4");

models,id_list,ts,te = JLD.load("media/upper_7.jld","m","veh_id_list","ts","te");
models[249].c=0.2
models[250].c=0.75
models[250].idm.d_max=6.0
models[249].idm.d_max=2.0
scenelist_pf = scenelist_from_jld_pf_debug(f,models,id_list,ts,te);
scenelist2video(f,scenelist_pf,filename="media/turing/upper_7_pf_turing.mp4");

models,id_list,ts,te = JLD.load("media/upper_8.jld","m","veh_id_list","ts","te");
#models[275].idm.d_max=7.0
models[275].c = 0.4
scenelist_pf = scenelist_from_jld_pf_debug(f,models,id_list,ts,te);
scenelist2video(f,scenelist_pf,filename="media/turing/upper_8_pf_turing.mp4");
23 changes: 23 additions & 0 deletions scripts/helpers.jl
Expand Up @@ -15,6 +15,29 @@ using JLD # to save models
using AutomotiveInteraction
using DelimitedFiles # to write to txt file that will be read in by python

"""
# Example
```julia
models,id_list,ts,te = JLD.load("media/upper_1.jld","m","veh_id_list","ts","te");
models[19].c = 0.9
models[6].c = 0.8
te=te+20
scenelist_pf = scenelist_from_jld_pf_debug(f,models,id_list,ts,te);
scenelist2video(f,scenelist_pf,filename="media/turing/upper_1_pf_new.mp4");
```
"""
function scenelist_from_jld_pf_debug(f::FilteringEnvironment,models,id_list,ts,te)
#print("Metrics extraction from jld file $(filename) \n")

scene_real = deepcopy(f.traj[ts])
if !isempty(id_list) keep_vehicle_subset!(scene_real,id_list) end

nticks = te-ts+1
scene_list = simulate(scene_real,f.roadway,models,nticks,f.timestep)
return scene_list
end

"""
- Make scatter plot of the lmidm parameters and final particle for a scenario
with each scatter point corresponding to a different car
Expand Down
38 changes: 23 additions & 15 deletions src/Driving/cooperative_IDM.jl
Expand Up @@ -69,18 +69,18 @@ function AutomotiveSimulator.observe!(model::CooperativeIDM, scene::Scene, roadw
model.a_idm = a_idm

if ego.state.posG.x < 1070
if egoid==60 print("Beyond merge point so stop worrying\n") end
if egoid==194 print("Beyond merge point so stop worrying\n") end
model.a = model.a_idm
return model
end

veh = find_merge_vehicle(model.env, scene,ego)

if veh == nothing
#if egoid == 60 println("obs:No merge vehicle\n") end
#if egoid == 194 print("obs:No merge vehicle\n") end
model.a = model.a_idm
else
#if egoid==60 print("obs:merge veh id = $(veh.id)\n") end
#if egoid==194 print("obs:merge veh id = $(veh.id)\n") end
model.other_acc = 0.0
model.a = 0.0

Expand All @@ -89,31 +89,38 @@ function AutomotiveSimulator.observe!(model::CooperativeIDM, scene::Scene, roadw
model.ego_ttm = ego_ttm
model.veh_ttm = veh_ttm
if ( ego_ttm < 0.0 || ego_ttm < veh_ttm || veh_ttm == Inf)
#if egoid == 60 print("obs:Ego TTM < Merge TTM, ignoring\n") end
#if egoid == 194 print("obs:Ego TTM < Merge TTM, ignoring\n") end
ego_ttm < veh_ttm
model.a = model.a_idm
model.consider_merge = false
model.front_car = false
else
model.consider_merge = true
#if(egoid==60) print("obs:Ego TTM>Merge TTM, can't ignore \n") end
if veh_ttm < model.c*ego_ttm
#if(egoid==194) print("obs:Ego TTM>Merge TTM, can't ignore \n") end
#if(egoid==249) print("obs:c = $(model.c)\n") end
#model.c = 0.1 # For upper_4
#model.c = 0.1 # For upper 2
if veh_ttm < model.c*ego_ttm
if(egoid==194) print("obs: Cannot ignore as less than c times\n") end
model.front_car = true
headway = distance_projection(model.env, veh) - distance_projection(model.env, ego)
headway -= veh.def.length
#headway -= veh.def.length
if egoid==194 print("obs: headway = $headway \n") end
v_oth = veh.state.v
v_ego = ego.state.v
# @show "tracking front car"
if egoid==194 print("obs: v_ego = $v_ego\n") end
if egoid==194 print("obs: v_oth = $v_oth\n") end
track_longitudinal!(model.idm, v_ego, v_oth, headway)
model.a_merge = model.idm.a
model.a = min(model.a_merge, model.a_idm)
else
else
if(egoid==194) print("obs:Ignore as not less than c times\n") end
model.a = model.a_idm
model.front_car = false
end
end
end
#print("cidm observe says: I'm done observing\n")
if egoid==194 print("model.a = $(model.a)\n") end
return model
end

Expand All @@ -125,7 +132,7 @@ function find_merge_vehicle(env,scene::Scene,ego_veh)
ego_lane = get_lane(env.roadway,ego_veh)
ego_lane_tag = ego_lane.tag # Let's use tag as I think it'll be faster to compare equality than entire lane
ego_ttm = time_to_merge(env,ego_veh,0.)
if egoid==60 print("fmv:ego_ttm = $(ego_ttm)\n") end
#if egoid==60 print("fmv:ego_ttm = $(ego_ttm)\n") end

# Based on upper vs lower environment, decide the specific lanes to use
merge_lane_tag = LaneTag(0,0)
Expand All @@ -146,7 +153,7 @@ function find_merge_vehicle(env,scene::Scene,ego_veh)

veh_ttm = time_to_merge(env,veh,0.)

#if egoid==60 print("fmv: oth_veh_id=$(veh.id),veh_ttm=$(veh_ttm)\n") end
#if egoid==249 print("fmv: oth_veh_id=$(veh.id),veh_ttm=$(veh_ttm)\n") end
diff_ttm_temp = abs(veh_ttm-ego_ttm)

if diff_ttm_temp < diff_ttm
Expand All @@ -170,10 +177,10 @@ function time_to_merge(env, veh::Entity, a::Float64 = 0.0)
t = Inf
if isapprox(a, 0)
if v>0.1
if egoid==60 print("ttm::d=$d,v=$v\n") end
#if egoid==249 print("ttm::d=$d,v=$v\n") end
t = d/v
else
if egoid==60 print("ttm::d=$d,v=$v\n") end
#if egoid==249 print("ttm::d=$d,v=$v\n") end
t = d/0.1
end
else
Expand Down Expand Up @@ -202,7 +209,8 @@ end

"""
distance_projection(env::MergingEnvironment, veh::Vehicle)
Performs a projection of `veh` onto the main lane. It returns the longitudinal position of the projection of `veh` on the main lane.
Performs a projection of `veh` onto the main lane.
It returns the longitudinal position of the projection of `veh` on the main lane.
The projection is computing by conserving the distance to the merge point.
"""
function distance_projection(env, veh::Entity)
Expand Down

0 comments on commit e742b5f

Please sign in to comment.