# Constraint using a constant - Broadcast Syntax Error

**URL:** <https://discourse.julialang.org/t/constraint-using-a-constant-broadcast-syntax-error/111855>\
**Category:** Optimization (Mathematical)\
**Created:** [March 20, 2024, 3:30am UTC](https://discourse.julialang.org/t/constraint-using-a-constant-broadcast-syntax-error/111855 "2024-03-20T03:30:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IgorESMelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/igoresmelo/32/204760_2.png) [@IgorESMelo](https://discourse.julialang.org/u/IgorESMelo)\
**Post date:** [March 20, 2024, 3:30am UTC](https://discourse.julialang.org/t/constraint-using-a-constant-broadcast-syntax-error/111855/1 "2024-03-20T03:30:04Z")

</div>

Hello everyone,

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 20, 2024, 3:59am UTC](https://discourse.julialang.org/t/constraint-using-a-constant-broadcast-syntax-error/111855/2 "2024-03-20T03:59:24Z")

</div>

> [@IgorESMelo](#):
>
> `for s = 1:5; w = 1:4; d = 1:7`

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

You have instead written:

```julia
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:

```Julia
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`
