Error: JuMP constraint vs Array constraint (same size) --> ArgumentError: Broadcasting

Hello everyone,

I’m using Julia v1.4.2 and JuMP v0.21.2 to solve a optimization problem. I want to compare a decision variable with an array of zeros and a calculated array (with other array and another decision variable) with the same dimensions (I printed then and compare). The comparision and the constraint are showed in the next code:

M = 60
low_bound_early = M*(df[:,:min_delivery_time]-df[:, :actual_delivery])
println(typeof(y[:,"late"]))
println(size(y[:,"late"]))
        
println(typeof(low_bound_early))
println(size(low_bound_early))
       
@constraint(model, y[:,"early"] .>= 0) 
@constraint(model, y[:,"early"] .>= low_bound_early)

The output:

JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Int64,1}},Tuple{Dict{Int64,Int64}}}
(9,)
Array{Float64,1}
(9,)

and the next error:

ArgumentError: broadcasting requires an assigned BroadcastStyle

Stacktrace:
 [1] copy(::Base.Broadcast.Broadcasted{Base.Broadcast.Unknown,Tuple{UnitRange{Int64}},typeof(MutableArithmetics.sub_mul),Tuple{JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Int64,1}},Tuple{Dict{Int64,Int64}}},Array{GenericAffExpr{Float64,VariableRef},1}}}) at ./broadcast.jl:831
 [2] materialize at ./broadcast.jl:820 [inlined]
 [3] broadcast(::typeof(MutableArithmetics.sub_mul), ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Int64,1}},Tuple{Dict{Int64,Int64}}}, ::Array{GenericAffExpr{Float64,VariableRef},1}) at ./broadcast.jl:758
 [4] broadcast_fallback!(::MutableArithmetics.NotMutable, ::Function, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Int64,1}},Tuple{Dict{Int64,Int64}}}, ::Array{GenericAffExpr{Float64,VariableRef},1}) at /Users/pablogalaz/.julia/packages/MutableArithmetics/ZGFsK/src/broadcast.jl:85
 [5] broadcast!(::Function, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Int64,1}},Tuple{Dict{Int64,Int64}}}, ::Array{GenericAffExpr{Float64,VariableRef},1}) at /Users/pablogalaz/.julia/packages/MutableArithmetics/ZGFsK/src/broadcast.jl:77
 [6] macro expansion at /Users/pablogalaz/.julia/packages/MutableArithmetics/ZGFsK/src/rewrite.jl:227 [inlined]
 [7] macro expansion at /Users/pablogalaz/.julia/packages/JuMP/MnJQc/src/macros.jl:381 [inlined]
 [8] solve_buffers(::DataFrame, ::Int64) at ./In[103]:59
 [9] top-level scope at In[105]:4

How can I fix this error? :frowning:

y Is a DenseAxisArray and lower_bound_early is a Array{Float64, 1}. They might have the same number of elements, but JuMP doesn’t know that you want a 1-to-1 mapping.

You want something like:

for (lhs, rhs) in zip(y[:, "early"], lower_bound_early)
     @constraint(model, lhs >= rhs)
end
2 Likes

Understood, it worked! Thank you