Forum Discussion

FICO's avatar
FICO
Community Manager
3 years ago

python API Question : dictionary with tuple keys and xpress variables

I'm trying to convert gurobi to xpress

does xpress python api has data object like tupledict in gurobi?

I want to select the variables specified by the designated index in tuple key...

Or any other options?

Thanks for your help in advance.

1 Reply

  • FICO's avatar
    FICO
    Community Manager

    No, something like this does not exist in the Xpress Python API. However, it is not too hard to get something equivalent using list comprehension:

    import xpress as xp
     
    keys = [(1,1), (1,2), (1,3), (2,1), (2,3), (3,3)]
    # Create a dictionary of variables
    x = xp.vars(keys)
     
    # prints "x((1, 1)) + x((1, 2)) + x((1, 3))" (selects only variables with first index 1)
    print(xp.Sum(x[k] for k in x if k[0] == 1))
     
    # prints "x((1, 2))" (selects only variables with second index 2)
    print(xp.Sum(x[k] for k in x if k[1] == 2))