Search code examples
pythongurobigams-math

Python (gurobipy) equivalent of GAMS ord(i) function


I'm making a job shop scheduling model in Python using gurobipy because the free version of GAMS can't handle the dimensions of this specific problem, but I'm a total newbie with it.

Part of my objective function is the following:

(sum((i,ii,c)$(ord(ii)>ord(i)),w(i,ii,c)*u(i,ii))*R)

ord() gives the index of the element in the set, therefore the summatory of w * u is done only for those ii elements which indexes are greater than the i ones.

I was wondering if there's an elegant way to write this piece of code in python, so far I've seen that I can use the enumerate function to loop a one dimensional parameter and keep the index, ex:

bs = [7, 6, 5, 4, 1]
cs = [2, 8, 3, 9, 0]

for i, b in enumerate(bs):
    for j, c in enumerate(cs):
        if i > j:
            print(b, " * ", c, " = ", b * c)

But I have no idea how to do this with multidimensional parameters and variables?


Solution

  • The most elegant way to do this is to define the decision variables w and u over a set of tuples, where those tuples only include the valid combinations where ord(i)<ord(ii). Then you only have to iterate over the indexers of u or w.

    With Gurobi, I would do something like the following:

    u = m.addVars(((i,ii) for m,i in enumerate(bs)
                          for n,ii in enumerate(cs) if m<n),
                  name='u')