JuMP Unable to register the function

The function I want to maximize is

function L(params::Vector{Real}, df::DataFrame, mm::MMParams)
    θ1, α1, β1, D1, N1, ϵ1, θ2, α2, β2, D2, N2, ϵ2, ρ, pA, pF, σ, constant = params
    Π = mm.Π

    y_star_1 = y_start((θ1, α1, β1, D1, N1, ϵ1, ρ, pA, pF))
    y_star_2 = y_start((θ2, α2, β2, D2, N2, ϵ2, ρ, pA, pF))

    lists = unique(df[:, :list])
    L = 0.0

    for firm in lists
        dt = df[df[:, :list] .== firm, :]
        for row in eachrow(dt)
            s = row[:s]
            j = row[:LS]
            i = row[:S]

            if j in 0:2 && i in 1:2
                y_star = (i == 1 ? y_star_1 : y_star_2)
                f_value = logf(s, y_star, σ, constant)
                π_value = Π[j + 1, i + 1]
                L += f_value + log(π_value)
            end
        end
    end
    return L
end

I try to register this function

L_closure = (params...) -> L(collect(params), df, mm)
# Register the function
JuMP.register(
    model,
    :L,
    length(initial_params),  # Number of parameters
    L_closure,
    autodiff = true  # Enable automatic differentiation
)

But I got the error

Unable to register the function

Anyone knows how to solve this?

Hi @jgr, I’ve moved this to the optimisation section.

At a guess (I can’t run your code so I can’t confirm), you need params::Vector{<:Real} instead of params::Vector{Real}. The distinction is subtle, see:

julia> x = [1.0, 2.0]
2-element Vector{Float64}:
 1.0
 2.0

julia> x isa Vector{Real}
false

julia> x isa Vector{<:Real}
true

If that doesn’t work, you need to ensure that your function can be differentiated with ForwardDiff.jl.

I’d also encourage you to adopt the newer syntax for nonlinear modelling: Nonlinear Modeling · JuMP

For details, see ANN: JuMP v1.15 is released

1 Like