<commit_before>"""
	IntelligentDriverModel <: LaneFollowingDriver

The Intelligent Driver Model. A rule based driving model that is governed by parameter
settings. The output is an longitudinal acceleration.

Here, we have extended IDM to the errorable IDM. If a standard deviation parameter is
specified, then the output is a longitudinal acceleration sampled from a normal distribution
around the non-errorable IDM output.

# Fields
- `a::Float64 = NaN` the predicted acceleration i.e. the output of the model
- `σ::Float64 = NaN` allows errorable IDM, optional stdev on top of the model, set to zero or NaN for deterministic behavior
- `k_spd::Float64 = 1.0` proportional constant for speed tracking when in freeflow [s⁻¹]
- `δ::Float64 = 4.0` acceleration exponent
- `T::Float64  = 1.5` desired time headway [s]
- `v_des::Float64 = 29.0` desired speed [m/s]
- `s_min::Float64 = 5.0` minimum acceptable gap [m]
- `a_max::Float64 = 3.0` maximum acceleration ability [m/s²]
- `d_cmf::Float64 = 2.0` comfortable deceleration [m/s²] (positive)
- `d_max::Float64 = 9.0` maximum deceleration [m/s²] (positive)
"""
@with_kw mutable struct IntelligentDriverModel <: LaneFollowingDriver
    a::Float64 = NaN # predicted acceleration
    σ::Float64 = NaN # optional stdev on top of the model, set to zero or NaN for deterministic behavior

    k_spd::Float64 = 1.0 # proportional constant for speed tracking when in freeflow [s⁻¹]

    δ::Float64 = 4.0 # acceleration exponent [-]
    T::Float64  = 1.5 # desired time headway [s]
    v_des::Float64 = 29.0 # desired speed [m/s]
    s_min::Float64 = 5.0 # minimum acceptable gap [m]
    a_max::Float64 = 3.0 # maximum acceleration ability [m/s²]
    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
end
function track_longitudinal!(model::IntelligentDriverModel, v_ego::Float64, v_oth::Float64, headway::Float64)

    if !isnan(v_oth)
        @assert !isnan(headway)
        if headway < 0.0
            @debug("IntelligentDriverModel Warning: IDM received a negative headway $headway"*
                  ", a collision may have occured.")
            model.a = -model.d_max
        else

            Δv = v_oth - v_ego
            s_des = model.s_min + v_ego*model.T - v_ego*Δv / (2*sqrt(model.a_max*model.d_cmf))
            v_ratio = model.v_des > 0.0 ? (v_ego/model.v_des) : 1.0
            model.a = model.a_max * (1.0 - v_ratio^model.δ - (s_des/headway)^2)
        end
    else
        # no lead vehicle, just drive to match desired speed
        Δv = model.v_des - v_ego
        model.a = Δv*model.k_spd # predicted accel to match target speed
    end

    @assert !isnan(model.a)

    model.a = clamp(model.a, -model.d_max, model.a_max)

    return model
end

reset_hidden_state!(model::IntelligentDriverModel) = model

function Base.rand(model::IntelligentDriverModel)
    if isnan(model.σ) || model.σ ≤ 0.0
        LaneFollowingAccel(model.a)
    else
        LaneFollowingAccel(rand(Normal(model.a, model.σ)))
    end
end
function Distributions.pdf(model::IntelligentDriverModel, a::LaneFollowingAccel)
    if isnan(model.σ) || model.σ ≤ 0.0
        a == model.a ? Inf : 0.
    else
        pdf(Normal(model.a, model.σ), a.a)
    end
end
function Distributions.logpdf(model::IntelligentDriverModel, a::LaneFollowingAccel)
    if isnan(model.σ) || model.σ ≤ 0.0
        a == model.a ? Inf : 0.
    else
        logpdf(Normal(model.a, model.σ), a.a)
    end
end<commit_msg>added explicit return statements<commit_after>"""
	IntelligentDriverModel <: LaneFollowingDriver

The Intelligent Driver Model. A rule based driving model that is governed by parameter
settings. The output is an longitudinal acceleration.

Here, we have extended IDM to the errorable IDM. If a standard deviation parameter is
specified, then the output is a longitudinal acceleration sampled from a normal distribution
around the non-errorable IDM output.

# Fields
- `a::Float64 = NaN` the predicted acceleration i.e. the output of the model
- `σ::Float64 = NaN` allows errorable IDM, optional stdev on top of the model, set to zero or NaN for deterministic behavior
- `k_spd::Float64 = 1.0` proportional constant for speed tracking when in freeflow [s⁻¹]
- `δ::Float64 = 4.0` acceleration exponent
- `T::Float64  = 1.5` desired time headway [s]
- `v_des::Float64 = 29.0` desired speed [m/s]
- `s_min::Float64 = 5.0` minimum acceptable gap [m]
- `a_max::Float64 = 3.0` maximum acceleration ability [m/s²]
- `d_cmf::Float64 = 2.0` comfortable deceleration [m/s²] (positive)
- `d_max::Float64 = 9.0` maximum deceleration [m/s²] (positive)
"""
@with_kw mutable struct IntelligentDriverModel <: LaneFollowingDriver
    a::Float64 = NaN # predicted acceleration
    σ::Float64 = NaN # optional stdev on top of the model, set to zero or NaN for deterministic behavior

    k_spd::Float64 = 1.0 # proportional constant for speed tracking when in freeflow [s⁻¹]

    δ::Float64 = 4.0 # acceleration exponent [-]
    T::Float64  = 1.5 # desired time headway [s]
    v_des::Float64 = 29.0 # desired speed [m/s]
    s_min::Float64 = 5.0 # minimum acceptable gap [m]
    a_max::Float64 = 3.0 # maximum acceleration ability [m/s²]
    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
end
function track_longitudinal!(model::IntelligentDriverModel, v_ego::Float64, v_oth::Float64, headway::Float64)

    if !isnan(v_oth)
        @assert !isnan(headway)
        if headway < 0.0
            @debug("IntelligentDriverModel Warning: IDM received a negative headway $headway"*
                  ", a collision may have occured.")
            model.a = -model.d_max
        else

            Δv = v_oth - v_ego
            s_des = model.s_min + v_ego*model.T - v_ego*Δv / (2*sqrt(model.a_max*model.d_cmf))
            v_ratio = model.v_des > 0.0 ? (v_ego/model.v_des) : 1.0
            model.a = model.a_max * (1.0 - v_ratio^model.δ - (s_des/headway)^2)
        end
    else
        # no lead vehicle, just drive to match desired speed
        Δv = model.v_des - v_ego
        model.a = Δv*model.k_spd # predicted accel to match target speed
    end

    @assert !isnan(model.a)

    model.a = clamp(model.a, -model.d_max, model.a_max)

    return model
end

reset_hidden_state!(model::IntelligentDriverModel) = model

function Base.rand(model::IntelligentDriverModel)
    if isnan(model.σ) || model.σ ≤ 0.0
        return LaneFollowingAccel(model.a)
    else
        return LaneFollowingAccel(rand(Normal(model.a, model.σ)))
    end
end
function Distributions.pdf(model::IntelligentDriverModel, a::LaneFollowingAccel)
    if isnan(model.σ) || model.σ ≤ 0.0
        return a == model.a ? Inf : 0.
    else
        return pdf(Normal(model.a, model.σ), a.a)
    end
end
function Distributions.logpdf(model::IntelligentDriverModel, a::LaneFollowingAccel)
    if isnan(model.σ) || model.σ ≤ 0.0
        return a == model.a ? Inf : 0.
    else
        return logpdf(Normal(model.a, model.σ), a.a)
    end
end
{'fraction_non_alphanumeric': 0.111276566941925, 'fraction_numerical': 0.027074590496818735, 'mean_word_length': 3.3053613053613056, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7237284', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2423, 'n_words': 716}