Registering an array of functions and using them in constraints and objectives

This may help:

using JuMP, Ipopt

function main(functions)
    model = Model(Ipopt.Optimizer)
    @variable(model, x)
    for (i, f) in enumerate(functions)
        f_sym = Symbol("f_$(i)")
        register(model, f_sym, 1, f; autodiff = true)
        add_NL_constraint(model, :($(f_sym)($x) <= 1.0))
    end
    return model
end

main([
    x -> x^2,
    x -> sin(x)^2,
])

I should add it as an example to the documentation. The current section on this is a little bare: https://jump.dev/JuMP.jl/stable/nlp/#Raw-expression-input-1

1 Like