I am optimizing a function inside another function. I need to obtain and save the results of that optimization. However the compiler does not infer the type of those results. I am obtaining these instances of Any
in the @code_warntype
of the function:
30 ─ %66 = invoke Optim.:(#optimize#71)(%24::Float64, 2.220446049250313e-16::Float64, 100000::Int64, %43::Bool, %49::Bool, nothing::Nothing, %50::Int64, %46::Bool, %51::typeof(optimize), %23::getfield(Main, Symbol("##11#12")){Int64,Float64}, 2.220446049250313e-16::Float64, _3::Float64, $(QuoteNode(Brent()))::Brent)::Optim.UnivariateOptimizationResults{Float64,Float64,_1,_2,Float64,Brent} where _2 where _1
││││ └─── goto #31
│││ 31 ─ goto #32
││ 32 ─ goto #33
│╻╷ minimum9 33 ─ %70 = (Base.getfield)(%66, :minimum)::Any
│ │ (Core.typeassert)(%70, Main.Float64)
│ │ %72 = π (%70, Float64)
│ │ %73 = Main.V2dict::Core.Compiler.Const(Dict{Tuple{Int64,Float64},Float64}(), false)
│╻ setindex! │ %74 = (Base.tuple)(t, K)::Tuple{Int64,Float64}
││ │ invoke Base.setindex!(%73::Dict{Tuple{Int64,Float64},Float64}, %72::Float64, %74::Tuple{Int64,Float64})
│╻╷ minimizer10 │ %76 = (Base.getfield)(%66, :minimizer)::Any
How can I resolve those type ambiguities?
If you must know, this is the function I am trying to speed up
using Optim
const V2dict = Dict{Tuple{Int, Float64}, Float64}()
const c2dict = Dict{Tuple{Int, Float64}, Float64}()
const k′2dict = Dict{Tuple{Int, Float64}, Float64}()
function V2(t::Int, K::Float64)
if t >= T
return 0.0
else
if haskey(V2dict, (t, K))
return V2dict[t, K]::Float64
else
opt = optimize(K′ -> -(log(K - K′) + β * V2(t+1, K′)), eps(), K, iterations = 100_000)
V2dict[t, K] = (Optim.minimum(opt))::Float64
k′2dict[t, K] = (Optim.minimizer(opt))::Float64
c2dict[t, K] = (K - k′2dict[t, K])::Float64
conv2[t, K] = Optim.converged(opt)::Bool
return V2dict[t, K]::Float64
end
end
end