Constraint using a constant - Broadcast Syntax Error

Hello everyone,

I have some doubts about how to create constraints using a constant value. For example, let’s take the code below.

using JuMP, Gurobi

function solve()   
    #Decision variables
    @variable(model, x[m = 1:30, s = 1:5, w = 1:4, d = 1:7], Bin)

    for s = 1:5; w = 1:4; d = 1:7
        @constraint(model, sum(x[m,s,w,d] for m in 1:9) >= 3)
    end

    @objective(model, Min, sum(x[m,s,w,d] for m in 1:30, s = 1:5, w = 1:4, d = 1:7))

    #-----------------------------------------------------
    println("\n\nsolving mathematical model...")
	optimize!(model)
    status = termination_status(model)
end

The solver returned the following error:
ERROR: Operation sub_mul between Matrix{AffExpr} and Int64 is not allowed. This most often happens when you write a constraint like x >= y where x is an array and y is a constant. Use the broadcast syntax x .- y >= 0 instead.

My doubt is how to use the broadcast syntax, more specifically how to get only a subset of x to build my constraint.

Thank you very much for the support.

This isn’t doing what you think it is doing.

You have instead written:

for s = 1:5
    w = 1:4
    d = 1:7
    @constraint(model, sum(x[m,s,w,d] for m in 1:9) >= 3)
end

And then it attempts the constraint sum(x[m, s, 1:4, 1:7] for m in 1:9) >= 3) which has a matrix on the left and a constant on the right.

Do instead:

function solve()   
    model = Model(Gurobi.Optimizer)
    @variable(model, x[m = 1:30, s = 1:5, w = 1:4, d = 1:7], Bin)
    for s = 1:5, w = 1:4, d = 1:7
        @constraint(model, sum(x[m,s,w,d] for m in 1:9) >= 3)
    end
    @objective(model, Min, sum(x))
    println("\n\nsolving mathematical model...")
	optimize!(model)
    return termination_status(model)
end

Note the s = 1:5, w = 1:4, d = 1:7 instead of s = 1:5; w = 1:4; d = 1:7