JuMP variable bound by bin variable

Hello, I trying to optimize using JuMP with GLPK. The model has the vectors of variables:

  • y is a binary vector. Each y[ j ] represents when thefeature is actived or deactived.

  • x is float vector. Each variable x[ j ] is the weight thatwe associate to a feature y[ j ].

the variable are defined in Julia in this way:
@variable(model, y[1:73], Bin)
@variable(model, x[1:73])

and I have this constraint that I’m trying to implement as following:


for i in 1:73
@constraint(model, -y[i] <= x[i] <= y[i] )

I’ve tried some approaches to implement this constraint but I did not get it yet.


1 Like

I having the follow ERROR:

ERROR: In @constraint(model, -(y[i]) <= x[i] <= y[i]): Expected -y[1] to be a number.

1 Like

Add them as two separate constraints:

using JuMP
model = Model()
@variable(model, x[1:3])
@variable(model, y[1:3], Bin)
@constraint(model, [i = 1:3], x[i] >= -y[i])
@constraint(model, [i = 1:3], x[i] <= y[i])

JuMP tries to add an interval constraint, but it can’t because you have variables in every term. It will not split it into two constraints for you.