JuMP with user-defined non linear functions

You can tidy up the functions to remove a lot of the temporary matrices. This should point you in the right direction. I haven’t tested because I don’t know Age or a, so there might be some typos etc.

S0(kappa,age) = kappa[1] .+ sum(kappa[i] .* (age .== i) for i in 2:6)

function S_t(x::Vector{T}, InitialAdd, a) where {T}
    output = zero(T)
    for t in 1:size(a, 2)
        if t == 1
            for i in 1:size(a, 1)
                output += ((1 - x[2]) * InitialAdd[i])^2
            end
        else
            Tmax = t - 1
            aux = T[(1 - x[2]) .^ (Tmax - tt) for tt in 1:Tmax]
            for i in 1:size(a, 1)
                y = (1 - x[2])^t * InitialAdd[i] + x[1] * a[i, 1:t-1] * aux
                output += y^2
            end
        end
    end
    return output
end

# TODO: what is Age, a?
Stinner2(x...) = S_t(x[1:2], S0(x[3:8], Age), a)

m = Model(Ipopt.Optimizer)
@variable(m, x[1:8])
JuMP.register(m, :Stinner2, 8, Stinner2; autodiff=true)
@NLobjective(m, Min, Stinner2(x...))
optimize!(m)
2 Likes