Defining constraints on specific indices of a set

Hey,

I would like to write a constraint like

subject to constraint 
  {i in jobs, j in jobs: j<>i, k in machines}: x[i,k] + x[j,k] = 0;

which does not work due to an syntax error. Surprisingly

subject to constraint 
  {i in jobs, j in jobs: j<>i}: x[i,1] + x[j,1] = 0;

and

subject to constraint 
  {i in jobs, j in jobs, k in machines}: x[i,k] + x[j,k] = 0;

are both working.

What I’m doing wrong?

Thanks
johhat

The condition must appear at the end of the indexing expression:

subject to constraint 
  {i in jobs, j in jobs, k in machines: j<>i}: x[i,k] + x[j,k] = 0;

Thanks - that works perfectly!

Still surprising to me, that the condition must appear at the end of the indexing expression.

That does also imply that

subject to dis2 {i in Jobs, k in machine: k >1, j in Jobs: j <> i}: x[i,k] + x[i,k] = 0

does not work or am I missing something? Is there another workaround?

Moreover, if you would like to define variables x_i,j,k and j <= i that does not work, because in the definition of the variables you are not free to shuffle the indices around. You can only use x_i,k,j and j <= i (now the condition is at the end again), but this might be counter-intuitive if e.g. (i,j) are the edges of a graph or something similar…

Any help is appreciated.
johhat

The syntax is {indexing : condition}, so if you also want a restriction on k, you have to add it to the condition:

subject to constraint 
  {i in jobs, j in jobs, k in machines: j<>i and k>1}: x[i,k] + x[j,k] = 0;

Another way to write it, without any conditions, would be

set jobsneq = {i in jobs, j in jobs: j<>i};
subject to constraint
  {(i,j) in jobsneq, k in machines diff {1}}: x[i,k] + x[j,k] = 0;

For 500 jobs and 50 machines, my laptop takes about 25 seconds either way to generate all 12,225,500 constraints. (The time only starts to increase dramatically when you run out of physical memory.)

I am not sure about your x[i,j,k] example. How would you ideally like to write the constraint in that case?