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

Deleting part of containerized constraints makes whole constraint unprintable #3762

Closed LebedevRI closed 1 month ago

LebedevRI commented 1 month ago

At least, i would hope this quirk should be documented, although since the model is still printable, it seems like a bug, especially since it is recommended/ to print separate constraints for debugging.

using JuMP

function main()
    model = Model() 

    @variable(model, v[1:2,1:2,1:2])
    @constraint(model, c[i=1:2,j=1:2,k=1:2], v[i,j,k] >= 42)

    display(c)
    print(c)
    print(model)
    delete(model, c[1,1,1])

    # display(c)
    # ^ This fails with:
    # The index MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.GreaterThan{Float64}}(1) is invalid. Note that an index becomes invalid after it has been deleted.

    #print(c) # Same

    print(model) # Works!
end

main()
odow commented 1 month ago

MWE:

julia> using JuMP

julia> model = Model() ;

julia> @variable(model, x);

julia> @constraint(model, c[i=1:2], x <= i);

julia> delete(model, c[1])

julia> c
2-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}, ScalarShape}}:
Error showing value of type Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}, ScalarShape}}:
ERROR: The index MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.LessThan{Float64}}(1) is invalid. Note that an index becomes invalid after it has been deleted.
Stacktrace:
  [1] throw_if_not_valid

@LebedevRI I will commend you. You somehow keep finding the rough edges of JuMP!

LebedevRI commented 1 month ago

@odow thank you!

@LebedevRI I will commend you. You somehow keep finding the rough edges of JuMP!

Thank you, i guess? It's a bit of a curse :[ I would very much prefer to never encounter bugs anywhere instead, but the exact opposite happens in practice...