I’m very aware of the limitations of ForwardDiff.jl that code has to be written general enough to accept {T<:Real}
. The JuMP docs suggest to write target functions like
function good_f(x::T...) where {T<:Real}
y = zeros(T, length(x)) # Construct an array of type `T` instead!
for i = 1:length(x)
y[i] = x[i]^i
end
return sum(y)
end
I want to try to optimize a huge system where the target function will depend on varying combination of structs where type and needed allocations are known before optimization. Therefore, preallocation would be highly desired, e.g. I want to achieve something like:
struct PreAlloc
y::Vector{<:Real}
end
Y = PreAlloc(zeros(T, length(x)))
function good_f(x::T, Y::PreAlloc) where {T<:Real}
for i = 1:length(x)
Y.y[i] = x[i]^i
end
return sum(Y.y)
end
Now this will fail to allocate into Y.y
because the types won’t match during the autodiff pass. I know that SciML solves this problem via PreallocationTools.jl.
Is there a way to achieve something similar within the JuMP framework?