Skip to content

Commit

Permalink
Merge 5a460eb into 1e49ef0
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeBouton committed Mar 20, 2020
2 parents 1e49ef0 + 5a460eb commit 85cb415
Show file tree
Hide file tree
Showing 37 changed files with 363 additions and 1,723 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"

Expand Down
91 changes: 16 additions & 75 deletions src/AutomotiveDrivingModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,14 @@ using StaticArrays
using Distributions
using Reexport
using Random
using SparseArrays
using DataFrames
using Tricks: static_hasmethod

include("vec/Vec.jl")
@reexport using .Vec

# Records
export
Entity,
Frame,
EntityFrame,
RecordFrame,
RecordState,
ListRecord,
QueueRecord,
EntityQueueRecord,

ListRecordFrameIterator,
ListRecordStateByIdIterator,

get_statetype,
get_deftype,
get_idtype,

capacity,
nframes,
nstates,
nids,
frame_inbounds,
pastframe_inbounds,
n_objects_in_frame,
id2index,
get_ids,
nth_id,
get_state,
get_def,
get_time,
get_timestep,
get_elapsed_time,
get_subinterval,
get_by_id,
findfirst_stateindex_with_id,
findfirst_frame_with_id,
findlast_frame_with_id,
get_first_available_id,
push_back_records!,
update!,
allocate_frame,
get_sparse_lookup

include("records/common.jl")
include("records/entities.jl")
include("records/frames.jl")
include("records/listrecords.jl")
include("records/queuerecords.jl")
include("records/conversions.jl")

# Roadways

export StraightRoadway,
mod_position_to_roadway,
get_headway

include("roadways/straight_1d_roadways.jl")

export CurvePt,
Curve,
CurveIndex,
Expand Down Expand Up @@ -158,20 +100,27 @@ export
include("agent-definitions/agent_definitions.jl")

export
Entity,
Frame,
EntityFrame,
capacity,
id2index,
get_by_id,
get_first_available_id,
posf,
posg,
vel,
velf,
velg,
VehicleState,
get_vel_s,
get_vel_t,
get_center,
get_footpoint,
get_front,
get_rear,
get_lane

include("states/entities.jl")
include("states/frames.jl")
include("states/interface.jl")
include("states/vehicle_state.jl")

Expand Down Expand Up @@ -285,6 +234,7 @@ include("feature-extraction/lidar_sensor.jl")

export
propagate,
EntityAction,
LaneFollowingAccel,
AccelTurnrate,
AccelDesang,
Expand All @@ -303,12 +253,11 @@ include("actions/pedestrian_lat_lon_accel.jl")
export
DriverModel,
StaticDriver,
get_name,
action_type,
set_desired_speed!,
observe!,
reset_hidden_state!,
prime_with_history!
reset_hidden_states!

include("behaviors/interface.jl")

Expand Down Expand Up @@ -346,24 +295,16 @@ include("behaviors/tim_2d_driver.jl")
include("behaviors/sidewalk_pedestrian_model.jl")

export
get_actions!,
tick!,
reset_hidden_states!,
simulate,
simulate!,
EntityAction,
run_callback,
CollisionCallback
CollisionCallback,
observe_from_history!,
simulate_from_history!,
simulate_from_history

include("simulation/simulation.jl")
include("simulation/callbacks.jl")


export
State1D,
Vehicle1D,
Scene1D

include("deprecated.jl")
include("simulation/simulation_from_history.jl")

end # AutomotiveDrivingModels
10 changes: 0 additions & 10 deletions src/agent-definitions/agent_definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ function Base.read(io::IO, ::MIME"text/plain", ::Type{VehicleDef})
return VehicleDef(class, length, width)
end

"""
Base.convert(::Type{Entity{S, VehicleDef, I}}, veh::Entity{S, D, I}) where {S,D<:AbstractAgentDefinition,I}
Converts the definition of an entity
"""
function Base.convert(::Type{Entity{S, VehicleDef, I}}, veh::Entity{S, D, I}) where {S,D<:AbstractAgentDefinition,I}
vehdef = VehicleDef(class(veh.def), length(veh.def), width(veh.def))
return Entity{S, VehicleDef, I}(veh.state, vehdef, veh.id)
end

"""
BicycleModel
BicycleModel(def::VehicleDef; a::Float64 = 1.5, b::Float64 = 1.5)
Expand Down
5 changes: 0 additions & 5 deletions src/behaviors/MOBIL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ function MOBIL(
return MOBIL(DIR_MIDDLE, mlon, safe_decel, politeness, advantage_threshold)
end

"""
Return the name of the lane changing model
"""
get_name(::MOBIL) = "MOBIL"

"""
Set the desired speed of the longitudinal model within MOBIL
"""
Expand Down
2 changes: 1 addition & 1 deletion src/behaviors/intelligent_driver_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ around the non-errorable IDM output.
d_cmf::Float64 = 2.0 # comfortable deceleration [m/s²] (positive)
d_max::Float64 = 9.0 # maximum deceleration [m/s²] (positive)
end
get_name(::IntelligentDriverModel) = "IDM"

function set_desired_speed!(model::IntelligentDriverModel, v_des::Float64)
model.v_des = v_des
model
Expand Down
52 changes: 11 additions & 41 deletions src/behaviors/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ The DriverModel type is an abstract type! Custom driver models should inherit fr
"""
abstract type DriverModel{DriveAction} end

"""
get_name(::DriverModel)
returns the name of the driver model
"""
function get_name end

"""
action_type(::DriverModel{A}) where {A}
returns the type of the actions that are sampled from the model
Expand All @@ -34,6 +28,17 @@ Resets the hidden states of the model.
"""
function reset_hidden_state! end

"""
reset_hidden_states!(models::Dict{I,M}) where {M<:DriverModel}
reset hidden states of all driver models in `models`
"""
function reset_hidden_states!(models::Dict{I,M}) where {I, M<:DriverModel}
for model in values(models)
reset_hidden_state!(model)
end
return models
end

"""
observe!(model::DriverModel, scene, roadway, egoid)
Observes the scene and updates the model states accordingly.
Expand All @@ -48,40 +53,6 @@ Samples an action from the model.
Base.rand(model::DriverModel) = rand(Random.GLOBAL_RNG, model)
Base.rand(rng::AbstractRNG, model::DriverModel) = error("AutomotiveDrivingModelsError: Base.rand(::AbstractRNG, ::$(typeof(model))) not implemented")

function prime_with_history!(
model::DriverModel,
trajdata::ListRecord{S,D,I},
roadway::R,
frame_start::Int,
frame_end::Int,
egoid::I,
scene::EntityFrame{S,D,I} = allocate_frame(trajdata),
) where {S,D,I,R}

reset_hidden_state!(model)

for frame in frame_start : frame_end
get!(scene, trajdata, frame)
observe!(model, scene, roadway, egoid)
end

return model
end
function prime_with_history!(model::DriverModel, rec::EntityQueueRecord{S,D,I}, roadway::R, egoid::I;
pastframe_start::Int=1-nframes(rec),
pastframe_end::Int=0,
) where {S,D,I,R}

reset_hidden_state!(model)

for pastframe in pastframe_start : pastframe_end
scene = rec[pastframe]
observe!(model, scene, roadway, egoid)
end

model
end

####

"""
Expand All @@ -97,7 +68,6 @@ struct StaticDriver{A,P<:ContinuousMultivariateDistribution} <: DriverModel{A}
distribution::P
end

get_name(::StaticDriver) = "StaticDriver"
function Base.rand(rng::AbstractRNG, model::StaticDriver{A,P}) where {A,P}
a = rand(rng, model.distribution)
return convert(A, a)
Expand Down
1 change: 0 additions & 1 deletion src/behaviors/lane_following_drivers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ mutable struct StaticLaneFollowingDriver <: LaneFollowingDriver
end
StaticLaneFollowingDriver() = StaticLaneFollowingDriver(LaneFollowingAccel(0.0))
StaticLaneFollowingDriver(a::Float64) = StaticLaneFollowingDriver(LaneFollowingAccel(a))
get_name(::StaticLaneFollowingDriver) = "ProportionalSpeedTracker"
Base.rand(rng::AbstractRNG, model::StaticLaneFollowingDriver) = model.a
Distributions.pdf(model::StaticLaneFollowingDriver, a::LaneFollowingAccel) = isapprox(a.a, model.a.a) ? Inf : 0.0
Distributions.logpdf(model::StaticLaneFollowingDriver, a::LaneFollowingAccel) = isapprox(a.a, model.a.a) ? Inf : -Inf
1 change: 0 additions & 1 deletion src/behaviors/lat_lon_separable_driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ end

LatLonSeparableDriver(mlat::LateralDriverModel, mlon::LaneFollowingDriver) = LatLonSeparableDriver{LatLonAccel}(mlat, mlon)

get_name(model::LatLonSeparableDriver) = @sprintf("%s + %s", get_name(model.mlat), get_name(model.mlon))
function set_desired_speed!(model::LatLonSeparableDriver, v_des::Float64)
set_desired_speed!(model.mlon, v_des)
model
Expand Down
2 changes: 0 additions & 2 deletions src/behaviors/lateral_driver_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ A controller that executes the lane change decision made by the `lane change mod
kd::Float64 = 2.0 # derivative constant for lane tracking
end

get_name(::ProportionalLaneTracker) = "ProportionalLaneTracker"

function track_lateral!(model::ProportionalLaneTracker, laneoffset::Float64, lateral_speed::Float64)
model.a = -laneoffset*model.kp - lateral_speed*model.kd
model
Expand Down
2 changes: 1 addition & 1 deletion src/behaviors/princeton_driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A lane following driver model that controls longitudinal speed by following a fr
k::Float64 = 1.0 # proportional constant for speed tracking [s⁻¹]
v_des::Float64 = 29.0 # desired speed [m/s]
end
get_name(::PrincetonDriver) = "PrincetonDriver"

function set_desired_speed!(model::PrincetonDriver, v_des::Float64)
model.v_des = v_des
model
Expand Down
1 change: 0 additions & 1 deletion src/behaviors/sidewalk_pedestrian_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Walks along the sidewalk until approaching the crosswalk. Waits for the cars to
phases = Int[]
end

AutomotiveDrivingModels.get_name(model::SidewalkPedestrianModel) = "SidewalkPedestrianModel"
Base.rand(rng::AbstractRNG, model::SidewalkPedestrianModel) = model.a

function AutomotiveDrivingModels.observe!(model::SidewalkPedestrianModel, scene::Frame{Entity{VehicleState, D, I}}, roadway::Roadway, egoid::I) where {D, I}
Expand Down
2 changes: 1 addition & 1 deletion src/behaviors/speed_trackers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Longitudinal proportional speed control.
k::Float64 = 1.0# proportional constant for speed tracking [s⁻¹]
v_des::Float64 = 29.0 # desired speed [m/s]
end
get_name(::ProportionalSpeedTracker) = "ProportionalSpeedTracker"

function set_desired_speed!(model::ProportionalSpeedTracker, v_des::Float64)
model.v_des = v_des
model
Expand Down
2 changes: 0 additions & 2 deletions src/behaviors/tim_2d_driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Driver that combines longitudinal driver and lateral driver into one model.
mlane::LaneChangeModel = TimLaneChanger()
end

get_name(::Tim2DDriver) = "Tim2DDriver"

function set_desired_speed!(model::Tim2DDriver, v_des::Float64)
set_desired_speed!(model.mlon, v_des)
set_desired_speed!(model.mlane, v_des)
Expand Down
5 changes: 1 addition & 4 deletions src/behaviors/tim_lane_changer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ Has not been published anywhere, so first use in a paper would have to describe
See MOBIL if you want a lane changer you can cite.
# Constructors
TimLaneChanger(timestep::Float64;v_des::Float64=29.0,rec::QueueRecord=QueueRecord(2,timestep),threshold_fore::Float64 = 50.0,threshold_lane_change_gap_fore::Float64 = 10.0, threshold_lane_change_gap_rear::Float64 = 10.0,dir::Int=DIR_MIDDLE)
TimLaneChanger(v_des::Float64=29.0, threshold_fore::Float64 = 50.0,threshold_lane_change_gap_fore::Float64 = 10.0, threshold_lane_change_gap_rear::Float64 = 10.0,dir::Int=DIR_MIDDLE)
# Fields
- `dir::Int = DIR_MIDDLE` the desired lane to go to eg: left,middle (i.e. stay in same lane) or right
- `rec::QueueRecord` TODO
- `v_des::Float64 = 29.0` desired velocity
- `threshold_fore::Float64 = 50.0` Distance from lead vehicle
- `threshold_lane_change_gap_fore::Float64 = 10.0` Space in front
Expand All @@ -26,8 +25,6 @@ See MOBIL if you want a lane changer you can cite.
threshold_lane_change_gap_rear::Float64 = 10.0
end

get_name(::TimLaneChanger) = "TimLaneChanger"

function set_desired_speed!(model::TimLaneChanger, v_des::Float64)
model.v_des = v_des
model
Expand Down

0 comments on commit 85cb415

Please sign in to comment.