jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.17k stars 390 forks source link

Cannot `convert` an object of type Float64 to an object of type JuMP.NonlinearExpr #3699

Closed codercahol closed 4 months ago

codercahol commented 4 months ago

I have an array of nonlinear differentials (a, in the example below) that I use as part of an Euler integration step in finding an optimal trajectory. In some cases, one of the differentials is 0, but I haven't been able to figure out how to add it to the vector

Code for reproducing:

a = Vector{NonlinearExpr}(undef, 3)
a = 0.0

I've also tried

a = Vector{NonlinearExpr}(undef, 3)
a[1] = @expression(model, 0.0)

and

a = Vector{NonlinearExpr}(undef, 3)
a[1] = @NLexpression(model, 0.0)

I have it working with a = Vector{Any}, but I expect there is a better solution

odow commented 4 months ago

If you update to JuMP v1.20, the second example works:

julia> model = Model();

julia> a = Vector{NonlinearExpr}(undef, 3)
3-element Vector{NonlinearExpr}:
 #undef
 #undef
 #undef

julia> a[1] = @expression(model, 0.0)
0.0

julia> a
3-element Vector{NonlinearExpr}:
    +(0.0)
 #undef
 #undef

I fixed this a few weeks ago: https://github.com/jump-dev/JuMP.jl/pull/3672

But the Vector{Any} is probably a good choice, unless you have evidence showing that it is slow.

p.s., you can ask questions like this on the forum: https://jump.dev/forum

odow commented 4 months ago

Closing as fixed. If you have follow-up questions, please use the forum and we can chat there :smile:

codercahol commented 4 months ago

ok thanks!