Deleting upper and lower bounds JuMP@0.22.3 Clp@0.9.1

It’s been quite some time since last time i used JuMP now i came back to it and changed some of old syntax of this code:

using JuMP
using Clp
function scan_maker(A)
    m = Model(Clp.Optimizer)
    JuMP.set_optimizer_attribute(m, "PrimalTolerance", 1e-3)
    JuMP.set_optimizer_attribute(m, "DualTolerance", 1e-3)
    JuMP.set_optimizer_attribute(m, "InfeasibleReturn", 1)
    JuMP.set_optimizer_attribute(m, "PresolveType", 1)
    # m = Model(solver=GurobiSolver())
    level = size(A, 2)
    v = zeros(Int, level)
    ub = zeros(Int, level)
    lb = zeros(Int, level)

    @variable(m, x[1:level])
    @constraint(m, con, A*x.>=0)

    function setc(a)
        set_normalized_rhs.(con, float.(a))
    end
    
    function scan(c::Channel)
        i = 1
        init = 1
        while i > 0
            if i >= init
                @objective(m, Max, x[i])
                res = JuMP.optimize!(m)
                if termination_status(m) == MOI.OPTIMAL || termination_status(m) == MOI.DUAL_INFEASIBLE
                    ub[i] = round(Int, getvalue(x[i]))
                    set_objective_sense(m, MOI.MIN_SENSE)
                    res = JuMP.optimize!(m)
                    @assert termination_status(m) == MOI.OPTIMAL || termination_status(m) == MOI.DUAL_INFEASIBLE
                    lb[i] = round(Int, getvalue(x[i]))

                    v[i] = lb[i]
                    init += 1
                else
                    @assert termination_status(m) == MOI.INFEASIBLE
                    i -= 1
                    continue
                end
            elseif v[i] < ub[i]
                v[i] += 1
            else
                #set_upper_bound(x[i], Inf)
                delete_upper_bound(x[i])
                #set_lower_bound(x[i], -Inf)
                delete_lower_bound(x[i])
                init -= 1
                i -= 1
                continue
            end

            if i >= level
                put!(c, v)
                continue
            else
                set_upper_bound(x[i], v[i])
                
                set_lower_bound(x[i], v[i])
                
                i += 1
            end
        end
        close(c)
    end
    
    return setc, scan
end

i run it using this simple input to test it

a=[8 0 3; 0 5 5]
b, c= scan_maker(a)
x=[1,9]
b(x)
chnl= Channel(c)
for i in chnl
    @show i
end

i get this:

i = [0, -1, 3]
Clp0006I 0  Obj 0 Primal inf 1.8210549 (2) Dual inf 0.0044171335 (1) w.o. free dual inf (0)
Clp0006I 2  Obj 0.125 Dual inf 0.54071335 (1)
Clp0006I 2  Obj 1.6487828e+10 Dual inf 0.42291361 (1)
Clp0002I Dual infeasible - objective value 3.7683508e+10
Clp0032I DualInfeasible objective 3.768350797e+10 - 2 iterations time 0.002
Clp0006I 0  Obj 0 Primal inf 1.8210549 (2) Dual inf 0.0044171335 (1) w.o. free dual inf (0)
Clp0006I 1  Obj -4.2391361e+09
Clp0006I 1  Obj -1.059784e+10
Clp0002I Dual infeasible - objective value -1.059784e+10
Clp0032I DualInfeasible objective -1.059784023e+10 - 1 iterations time 0.002
Clp0006I 0  Obj 0 Primal inf 2.1313333 (2) Dual inf 0.009 (1) w.o. free dual inf (0)
Clp0006I 2  Obj 1.4666667 Dual inf 0.999 (1)
Clp0006I 2  Obj 5e+10
Clp0002I Dual infeasible - objective value 5e+10
Clp0032I DualInfeasible objective 5e+10 - 2 iterations time 0.002
Clp0006I 0  Obj 0 Primal inf 2.1313333 (2) Dual inf 0.009 (1) w.o. free dual inf (0)
Clp0006I 1  Obj -1e+10
Clp0006I 1  Obj -2.5e+10
Clp0002I Dual infeasible - objective value -2.5e+10
Clp0032I DualInfeasible objective -2.5e+10 - 1 iterations time 0.002
Clp0006I 0  Obj 0 Primal inf 3.1313333 (2) Dual inf 0.009 (1) w.o. free dual inf (0)
Clp0006I 1  Obj 2.8 Dual inf 0.999 (1)
Clp0006I 1  Obj 5e+10
Clp0002I Dual infeasible - objective value 5e+10
Clp0032I DualInfeasible objective 5e+10 - 1 iterations time 0.002
Clp0006I 0  Obj 0 Primal inf 3.1313333 (2) Dual inf 0.009 (1) w.o. free dual inf (0)
Clp0006I 1  Obj 2.8
Clp0000I Optimal - objective value 2.8
Clp0032I Optimal objective 2.8 - 1 iterations time 0.002
TaskFailedException:

Stacktrace:
 [1] delete(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.Bridges.LazyBridgeOptimizer{MathOptInterface.Utilities.CachingOptimizer{Clp.Optimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.GenericModel{Float64,MathOptInterface.Utilities.ObjectiveContainer{Float64},MathOptInterface.Utilities.VariablesContainer{Float64},MathOptInterface.Utilities.ModelFunctionConstraints{Float64}}}}},MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.GenericModel{Float64,MathOptInterface.Utilities.ObjectiveContainer{Float64},MathOptInterface.Utilities.VariablesContainer{Float64},MathOptInterface.Utilities.ModelFunctionConstraints{Float64}}}}, ::MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex,MathOptInterface.LessThan{Float64}}) at /Users/hanhasse/.julia/packages/MathOptInterface/IIN1o/src/Utilities/cachingoptimizer.jl:674
 [2] delete(::Model, ::ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex,MathOptInterface.LessThan{Float64}},ScalarShape}) at /Users/hanhasse/.julia/packages/JuMP/zn6NT/src/constraints.jl:357
 [3] delete_upper_bound at /Users/hanhasse/.julia/packages/JuMP/zn6NT/src/variables.jl:660 [inlined]
 [4] (::var"#scan#76"{Model,Int64,Array{Int64,1},Array{Int64,1},Array{Int64,1},Array{VariableRef,1}})(::Channel{Any}) at ./In[44]:47
 [5] (::Base.var"#485#486"{var"#scan#76"{Model,Int64,Array{Int64,1},Array{Int64,1},Array{Int64,1},Array{VariableRef,1}},Channel{Any}})() at ./channels.jl:129

Stacktrace:
 [1] check_channel_state at ./channels.jl:167 [inlined]
 [2] take_unbuffered(::Channel{Any}) at ./channels.jl:403
 [3] take! at ./channels.jl:381 [inlined]
 [4] iterate(::Channel{Any}, ::Nothing) at ./channels.jl:448
 [5] top-level scope at ./In[45]:7
 [6] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

The index MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex,MathOptInterface.LessThan{Float64}}(3) is invalid. Note that an index becomes invalid after it has been deleted.

i didn’t quite understand exactly what should i change because to me everything is in order.

Please update to JuMP 1.0 and Clp 1.0. Does the problem still happen then?

1 Like

Although perhaps on the first iteration it tries to delete a bound that doesn’t exist? What if you use:

if has_upper_bound(x[i])
    delete_upper_bound(x[i])
end

now i understood the logic of the mistake i was doing, it worked indeed.

1 Like