 # 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 I I I I I <=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

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 <=20NR
... #until you reach
HE <=20NR
``````

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 -20 NR <= 0.0
HE -20 NR <= 0.0
HE -20 NR <= 0.0
HE -20 NR <= 0.0
HE -20 NR <= 0.0
HE -20 NR <= 0.0
``````

Since I would like to get this answer:

``````HE <=20NR
....##until you reach
HE <=20NR
``````

These constraints are equivalent.

1 Like