How to use define interpolations in JuMP

Hi everyone,

Is there a way to register a user-defined function, generated from the Interpolations.jl package, into a JuMP model? Whenever I do, I get a method error. Here’s a MWE:

using JuMP, MadNLP
using Interpolations

x = LinRange(0, 1, 100);   #Random input for interpolation
y = rand(100);              #Random output for interpolation
func = LinearInterpolation(x, y, extrapolation_bc = Line()); #Function I want to register

model = Model(MadNLP.Optimizer; add_bridges = false)
JuMP.register(model, :func, 1, func, autodiff = true)

ERROR: MethodError: no method matching register(::Model, ::Symbol, ::Int64, ::Interpolations.Extrapolation{…}; autodiff::Bool)

Is there a workaround/alternative way to bypass this MethodError?

Cheers!

1 Like

I have never worked with these, but I tried doing the following as per the docs (Register a function)

ffunc(x) = func(x); # define a function instead of an Interpolations.Extrapolation object

model = Model(MadNLP.Optimizer; add_bridges = false)

register(model, :func_name, 1, ffunc, autodiff = true)

and it seemed to work on my end

4 Likes

@marouane-f is correct :smile: you must register a function, not an Interpolations.Extrapolation object.

Note that JuMP.register is for the legacy nonlinear interface. You should instead consider using the new @operator interface: Nonlinear Modeling · JuMP

3 Likes