How to define constraints on a collection of vectors separately?

The original problem is so larg. But, Suppose we have this small set of vectors

``````A =  [  [ [13, 48,99] , [71, 58, 99]],
[[91, 59, 22], [94, 86, 22], [71, 58, 22], [19,55,22]],
[[41, 79, 12], [23, 16, 12], [53, 28, 12]] ]

B =[99, 22, 12]   # the last member of each above vector has a meaning

``````

as `length(A) = 3`, I’d like to have three constraints similar the following:

``````c1 :=  x[13, 48,99] + x[71, 58, 99] >= 2

c2: =  x[91, 59, 22]+  x[94, 86, 22]+  x[71, 58, 22]+  x[19,55,22] >=2
c3:=  x[41, 79, 12]+  x[23, 16, 12]+  x[53, 28, 12] >=2
``````

My try

``````

@constraint(m, [b in B], sum( x[i] for i in A) >=2)
``````

I guess you want something like

``````A = [
[[13, 48,99] , [71, 58, 99]],
[[91, 59, 22], [94, 86, 22], [71, 58, 22], [19,55,22]],
[[41, 79, 12], [23, 16, 12], [53, 28, 12]],
]
B = [99, 22, 12]

# The set of all unique indices in A
S = unique(vcat(A...))

model = Model()
@variable(model, x[S])
@constraint(model, [b in B], sum(x[j] for j in S if j[end] == b) >= 2)

# or

model = Model()
@variable(model, x[S])
@constraint(model, [i in 1:length(B)], sum(x[j] for j in A[i]) >= 2)
``````
1 Like

Thanks @odow One question that I have is that:

I was wondering if there is any difference between `x[13, 48,99] + x[71, 58, 99] >= 2` and `x[(13, 48,99)] + x[(71, 58, 99)] >= 2` for Julia?
If so, do you know of any method to convert `x[(13, 48,99)] + x[(71, 58, 99)] >= 2` to `x[13, 48,99] + x[71, 58, 99] >= 2` in general case (works for anything similar)