# Syntax for indicator constraints

**URL:** <https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization\
**Created:** [December 28, 2021, 6:20pm UTC](https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716 "2021-12-28T18:20:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnZ](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JohnZ](https://discourse.julialang.org/u/JohnZ)\
**Post date:** [December 28, 2021, 6:20pm UTC](https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716/1 "2021-12-28T18:20:42Z")

</div>

I have a model with two constraints. The first constraint is a big M constraint, which I am trying to reformulate as an indictor constraint. I have tried to follow the syntax given in the documentation, but I don’t get a correct answer when I use indicator constraints. How can I write this using indicator constraints?

```julia
model = Model()
@variable(model,x[b=1:4]>=0)
@variable(model,y[b=1:4],Bin)
@variable(model,a >= 0.0)
M = 1000
@constraint(model,constraint1[j in 1:4], M*y[j]-sum(l[j,i]*x[i] for i in 1:4) + a >= 0) # l is a matrix containing data
@constraint(model,constraint2, sum(y[j] for j in 1:4) <= 2)

```

```julia
# Attempt 1. Use variable y as indicator constraint in constraint1. No changes to constraint2
@constraint(model,constraint1[j in 1:4], y[j] => {- sum(l[j,i]*x[i] for i in 1:4) + a >= 0})
@constraint(model,constraint2, sum(y[j] for j in 1:4) <= 2)

# Attempt 2 Define another variable z for indicator. No changes to constraint2
@variable(model,z[b=1:4],Bin)
@constraint(model,constraint1[j in 1:4], z[j] => {y[j] - sum(l[j,i]*x[i] for i in 1:4) + a >= 0})
@constraint(model,constraint2, sum(y[j] for j in 1:4) <= 2)

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 2, 2022, 7:23am UTC](https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716/2 "2022-01-02T07:23:38Z")

</div>

> [@JohnZ](#):
>
> I don’t get a correct answer

Hi John, Can you be more specific about what is incorrect? Is there an error, or a result that doesn’t satisfy your requirements? What solver are you using?

---

<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:** [January 2, 2022, 8:51pm UTC](https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716/3 "2022-01-02T20:51:22Z")

</div>

In your first model, the constraint is active when `y[j] == 0`, and ignored when `y[j] == 1`.

In contrast, your indicator constraints are active when `y[j] == 1`.

Per the documentation, [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Indicator-constraints), use `!y[j] => {}` to activate the indicator constraint on `y[j] == 0`.

---

<div class="post-metadata">

**Author:** ![JohnZ](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JohnZ](https://discourse.julialang.org/u/JohnZ)\
**Post date:** [January 3, 2022, 12:43pm UTC](https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716/4 "2022-01-03T12:43:48Z")

</div>

> [@jd-foster](#):
>
> Can you be more specific about what is incorrect?

I am using Cbc solver. The log shows the problem as unbounded.

> [@odow](#):
>
> use `!y[j] => {}`

I have tried that as well, but still it shows the problem unbounded. I have given a complete example below.

```julia
function run_func(n_x,num)
	l = -float(rand(30:100,num,n_x)) .+ float(rand(80:140,1,n_x))
    n_sim = size(l,1)
    n_x = size(l,2)
	M = 1000
    m = Model(Cbc.Optimizer)
    @variable(m,x[b=1:n_x]>=0)
    @variable(m,y[b=1:n_sim],Bin)
    @variable(m,a)
    @constraint(m,constraint,sum(x[i] for i =1:n_x) == 1)
	@constraint(m,constraint1[j in 1:n_sim], M*y[j]-sum(l[j,i]*x[i] for i in 1:n_x) + a >= 0)
#	@constraint(m,constraint1[j in 1:n_sim], !y[j] => {a >= sum(l[j,i]*x[i] for i in 1:n_x)})

    @constraint(m,constraint2, sum(y[j] for j in 1:n_sim)<=0.05*num)
    @objective(m,Min,a)
	optimize!(m)
	
end

run_func(10,100)

```

Could it be related to this github issue?

> <https://github.com/jump-dev/Cbc.jl/issues/151>
>
> I tried enabling indicator tests:
> https://github.com/jump-dev/Cbc.jl/blob/01ee4…23efa84f3cad38e4973b5f578915c69e181/test/MOI\_wrapper.jl#L77
> but it fails as Cbc believes the problem to be unbounded. The indicator constraints are bridged by the \[IndicatorSOS1Bridge\](https://jump.dev/MathOptInterface.jl/v0.9.19/apireference/#MathOptInterface.Bridges.Constraint.IndicatorSOS1Bridge) into SOS1 constraints.
> This issue seems to be due to the fact that the variables of the SOS1 constraints are not all binary.
> This was reported upstream in https://github.com/coin-or/Cbc/issues/363

---

<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:** [January 3, 2022, 7:41pm UTC](https://discourse.julialang.org/t/syntax-for-indicator-constraints/73716/5 "2022-01-03T19:41:37Z")

</div>

Yes, Cbc has some issues with SOS constraints. Try a solver with native support for indicator constraints like Gurobi or CPLEX.
