-1

I am starting to work with Jupyter Notebook and I have no idea how to do a nested loop, tried everything I could think of. What I want to do is:

I=(0,1,2,3,4)
R=(0,1,2,3,4)
y= m.addVars(n,5, name="y", vtype=GRB.BINARY)
yy= m.addVars(n,5*n, name="yy", vtype=GRB.BINARY)

if (y[i,r]+y[j,r]==2): yy[i,j+5*r]=1 for i in I for j in I for r in R

If both y variables are 1, i want yy to be 1 aswell, 0 in all other cases.

Thanks in advance

Barnelby
  • 1
  • 1

1 Answers1

0

Try the following,

if y[i,r]+y[j,r] == 2:
    for i in I:
        for j in I:
            for r in R:
                yy[i,j+5*r]=1
else:
    yy[i,j+5*r]=0

I haven't tried as I am not able to get your m.addVars. If I am not wrong this is something to do with filtering of an image?^_^

Bussller
  • 1,961
  • 6
  • 36
  • 50
  • m.addVars adds variables to the optimisation model. Tried your solution, but it gives me an NameError: r is not defined. – Barnelby Aug 31 '17 at 17:29
  • Oh man I am so stupid, I did not know Python only works if you have proper indentations. Tried your solution and it gave me the needed error code for me to realise that, thanks a lot! – Barnelby Aug 31 '17 at 17:44