Forum Discussion
FICO
7 years agoCommunity Manager
Dynamic array type in Xpress Python interface
Hello, I query is there any functionality or method in Xpress Python Interface that can be used to resemble the dynamic array type (e.g., "dynamic array(set of range) of mpvar/linctr)") available in...
FICO
7 years agoCommunity Manager
Hello,
the distinction between array and dynamic array in Mosel disappears when one uses Python, because the array of variables or constraints is created on the spot, based on a set of indices. If your question is about how to create an array with a sparse index set, I would suggest using dictionaries like in the following example:
import xpress as xp
S = set(['NY', 'AK', 'WA', 'ME'])
cost = {'NY': 4, 'AK': 3, 'WA': 7, 'ME': 11}
x = {i: xp.var() for i in S}
con1 = xp.Sum(x) <= 1
obj = xp.Sum(cost[i] * x[i] for i in S)
p = xp.problem()
p.addVariable(x)
p.addConstraint(con1)
p.setObjective(obj)The python interface allows for declaring a dictionary of variables with multiple indices, and provides the shortcut xpress.vars function:
import xpress as xp
S1 = set(['a', 'b', 'c', 'd'])
S2 = set(['y', 'z', 't', 'w'])
A = [2,2,4,5]
x = {(i,j): xp.var(ub=A[i]) for i in range(len(A)) for j in S}
x2 = xp.vars(S1, S2, ub=1)I hope this helps.


