Search code examples
juliajulia-jump

Conditioning and writing down an equation over a known set of combinations


I'm trying to write a constraint in the from of the following (also attached):

sum([s,i], x[i,j,s,p] ) = sum([s,k], x[j,k,s,p] )   for all j in N\{0,n}, p in P

I already have all possible combinations of (i,j,s,p) stored in a set Xs and X0nswhich a vector of such 4-tuples.

Thus I tried to write it down as

@constraint( model, [p in P, jj in X0ns], 
            sum(x[(i, j, s, p)] for (i, j, s, p) in Xs if j == jj) 
         == sum(x[(j, k, s, p)] for (i, j, s, p) in Xs if j == jj)
            

This gives me an Error. On top of that I think this is not correct way of writing. Because I did not include a summation on S anywhere. Is it needed?

How can I build this kind of constraint?

enter image description here


Solution

  • Here is what I think that you mean (this is the exact implementation of your LaTeX equation). Normally you should not have used tuples for array indexing.

    m = Model();
    S=1:3
    N=1:4
    K=1:4
    P=1:5
    @variable(m, x[N,N,S,P] >= 0);
    
    @constraint(m, [j in N[2:end-1], p in P], sum(x[i,j,s,p] for i in N, s in S) 
                                           == sum(x[j,k,s,p] for k in K, s in S))