I have tried a few of the suggestions. Here is a small functional example:
My original code:
using JuMP, Ipopt
function myfun(x,y)
X = zeros(10,10);
X .+= x;
res1 = x^2 - sqrt(y);
res2 = x + log(y);
return maximum(abs.([res1;res2]));
end
model = Model(Ipopt.Optimizer)
JuMP.register(model, :myfun, 2, myfun, autodiff=true)
@variable(model, x >=0, start=2)
@variable(model, y >= 0, start=0.7)
@NLobjective(model, Min, myfun(x,y))
optimize!(model)
and the new code following the above suggestions:
using JuMP, Ipopt
function myfun(x,y)
X = foo(rand(2));
X .+= x;
res1 = x^2 - sqrt(y);
res2 = x + log(y);
return maximum(abs.([res1;res2]));
end
model = Model(Ipopt.Optimizer)
JuMP.register(model, :myfun, 2, myfun, autodiff=true)
@variable(model, x >=0, start=2)
@variable(model, y >= 0, start=0.7)
@NLobjective(model, Min, myfun(x,y))
optimize!(model)
function foo(x::Array{T}) where {T<:Real}
M = zeros(T, 10, 10)
end
Both give the same error, which is:
MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{getfield(JuMP, Symbol("##107#109")){typeof(myfun)},Float64},Float64,2})
Closest candidates are:
Float64(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:194
Float64(::T<:Number) where T<:Number at boot.jl:718
Float64(!Matched::Int8) at float.jl:60