MAiNGO-github / MAiNGO.jl

A Julia Wrapper to use MAiNGO with JuMP
MIT License
3 stars 1 forks source link

Method error during `eval` #8

Closed odow closed 5 months ago

odow commented 5 months ago

I assume somewhere that you drop zeros from summations, so then you're left with an empty list of arguments.

Reported on https://discourse.julialang.org/t/jump-maingo-no-method-matching/114514

julia> using JuMP, MAiNGO

julia> model = Model(MAiNGO.Optimizer);

julia> @variable(model, x);

julia> @objective(model, Min, [x, x]' * [x^2, 0])
0.0 + (x * (0)) + (x * (x²))

julia> optimize!(model)
ERROR: MethodError: no method matching +()
String concatenation is performed with * (See also: https://docs.julialang.org/en/v1/manual/strings/#man-concatenation).

Closest candidates are:
  +(::Base.CoreLogging.LogLevel, ::Integer)
   @ Base logging.jl:131
  +(::Bool, ::Complex{Bool})
   @ Base complex.jl:305
  +(::Bool, ::Bool)
   @ Base bool.jl:166
  ...

Stacktrace:
  [1] top-level scope
    @ none:1
  [2] eval
    @ ./boot.jl:385 [inlined]
  [3] eval
    @ ~/.julia/packages/MAiNGO/zVhVL/src/MAiNGO.jl:8 [inlined]
  [4] to_str(expr::Expr, variable_names::Vector{String})
    @ MAiNGO ~/.julia/packages/MAiNGO/zVhVL/src/util.jl:189
  [5] to_str_
    @ ~/.julia/packages/MAiNGO/zVhVL/src/util.jl:137 [inlined]
  [6] (::MAiNGO.var"#7#10"{MAiNGO.var"#to_str_#8"{Vector{String}}})(d::Expr)
    @ MAiNGO ./none:0
  [7] iterate
    @ ./generator.jl:47 [inlined]
  [8] collect_to!(dest::Vector{String}, itr::Base.Generator{Vector{…}, MAiNGO.var"#7#10"{…}}, offs::Int64, st::Int64)
    @ Base ./array.jl:892
  [9] collect_to_with_first!(dest::Vector{String}, v1::String, itr::Base.Generator{Vector{…}, MAiNGO.var"#7#10"{…}}, st::Int64)
    @ Base ./array.jl:870
 [10] collect(itr::Base.Generator{Vector{Any}, MAiNGO.var"#7#10"{MAiNGO.var"#to_str_#8"{Vector{String}}}})
    @ Base ./array.jl:844
 [11] to_str(expr::Expr, variable_names::Vector{String})
    @ MAiNGO ~/.julia/packages/MAiNGO/zVhVL/src/util.jl:213
 [12] to_str_
    @ ~/.julia/packages/MAiNGO/zVhVL/src/util.jl:137 [inlined]
 [13] (::MAiNGO.var"#7#10"{MAiNGO.var"#to_str_#8"{Vector{String}}})(d::Expr)
    @ MAiNGO ./none:0
 [14] iterate
    @ ./generator.jl:47 [inlined]
 [15] collect_to!(dest::Vector{String}, itr::Base.Generator{Vector{…}, MAiNGO.var"#7#10"{…}}, offs::Int64, st::Int64)
    @ Base ./array.jl:892
 [16] collect_to_with_first!(dest::Vector{String}, v1::String, itr::Base.Generator{Vector{…}, MAiNGO.var"#7#10"{…}}, st::Int64)
    @ Base ./array.jl:870
 [17] collect(itr::Base.Generator{Vector{Any}, MAiNGO.var"#7#10"{MAiNGO.var"#to_str_#8"{Vector{String}}}})
    @ Base ./array.jl:844
 [18] to_str(expr::Expr, variable_names::Vector{String})
    @ MAiNGO ~/.julia/packages/MAiNGO/zVhVL/src/util.jl:213
 [19] write_ALE_problem(m::MAiNGO.MAINGOModel)
    @ MAiNGO ~/.julia/packages/MAiNGO/zVhVL/src/util.jl:370
 [20] optimize!(model::MAiNGO.Optimizer)
    @ MAiNGO ~/.julia/packages/MAiNGO/zVhVL/src/MOI_wrapper.jl:190
 [21] optimize!
    @ ~/.julia/packages/MathOptInterface/2rAFb/src/Bridges/bridge_optimizer.jl:380 [inlined]
 [22] optimize!
    @ ~/.julia/packages/MathOptInterface/2rAFb/src/MathOptInterface.jl:85 [inlined]
 [23] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{…})
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/2rAFb/src/Utilities/cachingoptimizer.jl:316
 [24] optimize!(model::Model; ignore_optimize_hook::Bool, _differentiation_backend::MathOptInterface.Nonlinear.SparseReverseMode, kwargs::@Kwargs{})
    @ JuMP ~/.julia/packages/JuMP/Gwn88/src/optimizer_interface.jl:457
 [25] optimize!(model::Model)
    @ JuMP ~/.julia/packages/JuMP/Gwn88/src/optimizer_interface.jl:409
 [26] top-level scope
    @ REPL[10]:1
Some type information was truncated. Use `show(err)` to see complete types.
MAiNGO-github commented 5 months ago

Thank you for bringing this to our attention.

Unfortunately, this will take some time to figure out. In my test, the same issue also happens in BARON.jl (we use basically the same way to translate expressions to strings as that package).

Also, I do not think that we are actually responsible for dropping terms. Using @show on the expression, as it is given to the solver, I got

c = :(0.0 + x[1] * +() + x[1] * +(1.0 * x[1] * x[1]))

So it seems that MathOptInterface is already dropping that term? Not sure if that is a bug in MathOptInterface or a corner case that is expected, but not covered here and in BARON.jl

odow commented 5 months ago

Let me take a look

odow commented 5 months ago

This is a bug in MAiNGO, but it is also a bug in BARON. I'll submit a PR with the fix.

odow commented 5 months ago

Fixed BARON as well: https://github.com/jump-dev/BARON.jl/pull/81

You should be a bit mindful of the source code in BARON. That part was written in 2019, right near the start of our transition to MathOptInterface. It hadn't had many eyes (or tests) on it over time :smile: