How to build @constraints?

This is my model, I am working on building a mathematical model and would like to be able to represent the constraint like this: I[1] I[2] I[3] I[4] I[5] I[6] <=1500

using JuMP, GLPK
m = Model(with_optimizer(GLPK.Optimizer))
c=6
@variable(m,I[1:c],lower_bound=0)
Ii = transpose(I)
@constraint(m, Ii[1:c].<=1500)

You should read the documentation on Variable Bounds and Variable Containers.

You want something like

using JuMP
model = Model()
c = 6
@variable(model, 0 <= x[1:c] <= 1500)

Alternatively,

using JuMP
model = Model()
c = 6
@variable(model, x[1:c] >= 0)
@constraint(model, Ii[i = 1:c], x[i] <= 1500)
2 Likes

Thank you for your help.

1 Like

I have difficulty constructing constraints.

using JuMP,Cbc
model=Model(with_optimizer(Cbc.Optimizer))
d=6
MaxHE=20
@variable(model,NR[1:d],lower_bound=0,integer=true)
@variable(model,HE[1:d],lower_bound=0)
@constraint(model,HE.<= NR[1:d]*MaxHE)

I am trying to compile this result for the constraint:

HE[1] <=20NR[1] 
... #until you reach 
HE[6] <=20NR[6]

But the model gives me a different result than I want

Take a read of the Constraint Containers section of the JuMP documentation. You want:

using JuMP
model = Model()
d = 6
MaxHE = 20
@variable(model, NR[1:d] >= 0, Int)
@variable(model, HE[1:d] >= 0)
@constraint(model, [i = 1:d], HE[i] <= MaxHE * NR[i])

Thanks for your help @odow , but the result of compiling the code you submitted is the same result as my code.
When I compile mine and your code, the answer I have is as follows:

6-element Array {ConstraintRef {Model, C, Shape} where Shape <: AbstractShape where C, 1}:
  HE [1]-20 NR[1] <= 0.0
  HE [2]-20 NR[2] <= 0.0
  HE [3]-20 NR[3] <= 0.0
  HE [4]-20 NR[4] <= 0.0
  HE [5]-20 NR[5] <= 0.0
  HE [6]-20 NR[6] <= 0.0

Since I would like to get this answer:

HE[1] <=20NR[1] 
....##until you reach
HE[6] <=20NR[6]

These constraints are equivalent.

1 Like