Hi, I’m new to JuMP and I’m trying to wrap my head around why what I’m doing is incorrect. A much simplified MWE is below:
using JuMP, Ipopt
M,N = 3,5
struct myPoly
vec
end
function (f::myPoly)(x)
ret = 0.
for v in f.vec
ret = x*ret + v
end
ret
end
function myPolyEval(f::myPoly, x)
ret = 0.
for v in f.vec
ret = x*ret + v
end
ret
end
##
bas = myPoly.([collect(1:j) for j in 1:N])
samp = range(0,1,length=M)
model = Model(Ipopt.Optimizer)
@variable(model, x[1:N])
@NLexpression(model, callStruct[i=1:M,j=1:N], bas[j](samp[i])) # 1
# LoadError: MethodError: no method matching _is_sum(::Expr)
register(model, :myPolyEval, 2, myPolyEval; autodiff=true)
@NLexpression(model, mpe[i=1:M,j=1:N], myPolyEval(bas[j],samp[i]))
# LoadError: Unexpected object myPoly([1]) (of type myPoly in nonlinear expression.
@NLobjective(model, Min, sum( (x[j]*myPolyEval(bas[j],samp[i]))^2 for j in 1:N, i in 1:M) )
# LoadError: Unexpected object myPoly([1]) (of type myPoly in nonlinear expression.
Obviously, I see the trouble that one might run into with callable structs, but I would anticipate the others to work and can’t see why they wouldn’t. For reference, I anticipate bas
representing an arbitrary basis (not necessarily polynomials, but I found this while using the Polynomials.jl package), and I’d like this to work in generality (with a more complicated constraint in mind)