# Workaround for: Sum of binary Matrix as Index for Input Matrix

**URL:** <https://discourse.julialang.org/t/workaround-for-sum-of-binary-matrix-as-index-for-input-matrix/90769>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 24, 2022, 6:34pm UTC](https://discourse.julialang.org/t/workaround-for-sum-of-binary-matrix-as-index-for-input-matrix/90769 "2022-11-24T18:34:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JNSDVD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jnsdvd/32/44652_2.png) [@JNSDVD](https://discourse.julialang.org/u/JNSDVD)\
**Post date:** [November 24, 2022, 6:34pm UTC](https://discourse.julialang.org/t/workaround-for-sum-of-binary-matrix-as-index-for-input-matrix/90769/1 "2022-11-24T18:34:04Z")

</div>

Hey people,

I am new here, any help is much appreciated!!

```julia
@variable( bilap, X[1:2,u=1:4,1:3], Bin) 

```

![grafik](https://global.discourse-cdn.com/julialang/original/3X/b/3/b3c74ce7d95cb849bea480c0b663780270f184c0.png)

I want to use the sum of all 1s per each u in X as an Input (Index) for the matrix shown above.

```julia
@constraint( bilap , workers_allocated[u=1:4],w[u]=sum(X[i,u,j] for i in 1:2, j in 1:3)
   
@constraint( bilap , duration[i=1:4], duration[i]=Duration_per_task[i][w[u])

```

I know, it`s not possible like this, but I am struggeling with alternative workarrounds tbh.  
First I introduced a binary variable:

```julia
@variable( bilap, D[1:3,1:4], Bin)

@constraint( bilap , Da[i=1:4],Duration[i]==sum(D[j,i]*Duration_per_task[i][j] for j in 1:3))

@constraint( bilap , Da2[i=1:4],sum(D[:,i])==1)

```

My problem is how to link the binary variable D to the sum of the 1s in X (necessary, cause there are several other constraints for X)

This Approach:

```julia
@constraint( bilap , Da3[i=1:4],sum(x[a,i,p] for a in 1:2, p in 1:3)==sum(D[j,i])*j for j in 1:3)

```

leads to following Error:

```julia
Unsupported constraint expression: we don't know how to parse constraints containing expressions of type :generator

```

Also I tried ConditionalJuMP to use @implies, but without any success.

Many thanks!!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 24, 2022, 6:50pm UTC](https://discourse.julialang.org/t/workaround-for-sum-of-binary-matrix-as-index-for-input-matrix/90769/2 "2022-11-24T18:50:42Z")

</div>

> [@JNSDVD](#):
>
> `==sum(D[j,i])*j for j in 1:3)`

You have a typo here. It needs to be `==sum(D[j,i] * j for j in 1:3))`.

Putting everything together:

```julia
duration_per_task = [
    [10 6 4],
    [20 10_000 10_000],
    [1_000 8 4],
    [15 12 8],
]

model = Model()
@variable(model, X[1:2, 1:4, 1:3], Bin)
@variable(model, z[1:4, 1:3], Bin)
@constraint(model, [u=1:4], sum(z[u, :]) <= 1)
@constraint(
    model,
    [u = 1:4],
    sum(i * z[u, i] for i in 1:3) == sum(X[i, u, j] for i in 1:2, j in 1:3),
)
@expression(
    model, 
    duration[u=1:4],
    sum(duration_per_task[u][j] * z[u, j] for j in 1:3),
)
# Do something with duration[u]...

```

---

<div class="post-metadata">

**Author:** ![JNSDVD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jnsdvd/32/44652_2.png) [@JNSDVD](https://discourse.julialang.org/u/JNSDVD)\
**Post date:** [November 24, 2022, 7:11pm UTC](https://discourse.julialang.org/t/workaround-for-sum-of-binary-matrix-as-index-for-input-matrix/90769/3 "2022-11-24T19:11:01Z")

</div>

what a silly mistake…  
I really appreciate your help and time!!!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 24, 2022, 7:18pm UTC](https://discourse.julialang.org/t/workaround-for-sum-of-binary-matrix-as-index-for-input-matrix/90769/4 "2022-11-24T19:18:13Z")

</div>

> I really appreciate your help and time!!!

No worries 😄
