'How to have conditions for constraints?
Solution 1:[1]
I understand that you want a multi-dimensional constraint loop with filtering. I get this to a minimal working example:
M=1:10
model = Model(HiGHS.Optimizer)
@variable(model, y[1:lenght(M),1:lenght(M)] >= 0)
z = rand(lenght(M))
Having this values you can have the constraint with filtering as:
@constraint(model, [m1 in M, m2 in M], y[m1,m2] >= z[m1]*(m1 !== m2))
Note that constructing variable as y[1:lenght(M),1:lenght(M)] produces a regular Matrix while y[M,M] would yield a DenseAxisArray.
Solution 2:[2]
Your math formulation is wrong, because the j and k indices on x are not defined. Is the summation in the wrong place?
Assuming it is, one option is:
for i in I
for (m, m2) in M
if m != m2
@constraint(model, sum(x[i,j,k,m2] + y[j,j,k] for j in J, k in K) <= z[i, m, m2])
end
end
end
Another is
@constraint(
model,
[i in I, (m, m2) in M; m != m2],
sum(x[i,j,k,m2] + y[j,j,k] for j in J, k in K) <= z[i, m, m2]),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Przemyslaw Szufel |
| Solution 2 | Oscar Dowson |

