I am having a constraint in the form :
$$ x_i \leq z_j, \quad \forall i, j \in \{1, 2, .., n\}$$
I know that if the right part of the equation was a constant so something like $x_i \leq c[i]$ I could write something like:
@constraint(m, [i = 1:n], x[i] <= c[i])
But know that they have not always the same index , what can I do ?
1 Answer
Use:
@constraint(m, [i=1:n, j=1:n], x[i] <= z[j])
or
for i in 1:n, j in 1:n
@constraint(m, x[i] <= z[j])
end
You might want to read the documentation: https://jump.dev/JuMP.jl/stable/manual/constraints/#Containers-of-constraints
Or look at similar problems in the tutorials: https://jump.dev/JuMP.jl/stable/tutorials/linear/facility_location/#JuMP-implementation
You can also ask JuMP-specific questions on our forum: https://discourse.julialang.org/c/domain/opt/13
-
$\begingroup$ Just a comment. I have found that, if the problem is big, your second approach performs better than the first, in terms of CPU and memory usage. $\endgroup$ Nov 19, 2022 at 14:51
-
$\begingroup$ Sure. The first approach creates and returns a
Matrix{ConstraintRef}
, which can be useful if you want to access dual variables, etc. The second doesn't allocate or store the constraint references. $\endgroup$ Nov 20, 2022 at 18:45