Reputation: 23
I'm using JuMP v0.20.0 with the Ipopt optimizer, and I'm trying to solve a system of nonlinear equations in a loop, where the problem statement varies based on what I'm looping over.
Suppose I have this really simple problem of trying to pick $$t_1,\dots, t_n$$ to minimize the non-linear equation $$\sum_{i=1 to N} t_i^2$$. When I run this without looping, I have the following code
using JuMP, Optim, Ipopt, NLsolve
m = Model(Ipopt.Optimizer)
@variable(m, t[1:N] >= 0.00000001)
function solve_Aik(tlist...)
t = collect(tlist)
return sum([t[i]^2 for i in 1:N])
end
register(m, :solve_Aik, N, solve_Aik, autodiff=true)
@NLobjective(m, Min, solve_Aik(t...))
optimize!(m)
solution = [value.(t[i]) for i=1:N]
the last line of which provides me my solution just fine.
However, as soon as I put this in a loop (without even providing the number I'm looping over to the problem), I can no longer recover my solution, with an error of "MethodError: no method matching value(::ForwardDiff.Dual{ForwardDiff.Tag{JuMP.var"#107#109"{var"#solve_Aik#378"},Float64},Float64,8})". See the code below:
nums = [1,2,3]
for num in nums
m = Model(Ipopt.Optimizer)
@variable(m, t[1:N] >= 0.00000001)
function solve_Aik(tlist...)
t = collect(tlist)
return sum([t[i]^2 for i in 1:N])
end
register(m, :solve_Aik, N, solve_Aik, autodiff=true)
@NLobjective(m, Min, solve_Aik(t...))
optimize!(m)
solution = [value.(t[i]) for i=1:N]
end
The last line providing the solution is what Julia gets hung up on. Has anyone else encountered a similar issue? TIA!
Upvotes: 1
Views: 113
Reputation: 953
My guess based on the error message is that by some quirk of Julia's scoping rules, t = collect(tlist)
overwrites the JuMP variable t
defined in the body of the for loop. Try using a different name for the variable inside solve_Aik
.
Upvotes: 1