# XPRESS variables as indices for mixed integer linear programming

Can XPRESS variables not be used as indices to retrieve other XPRESS variables. In Python, for example, say that I have a list of XPRESS variables, each which can take on values (0->24). I'd like to use these values as indices to retrieve other variables from another list. However, I'm encountering this error: TypeError: list indices must be integers or slices, not xpress.var. Is there a way around this?

• Are you trying to sort? You might need to use indicator constraints or Big M to do what you want. I think an optimization modeling system could be developed to do what you want, in which case it would transform the problem as entered into the corresponding model using indicator constraints or Big M. But you will have to do that yourself.. If you use YALMIP as a front end to ((i.e., call as solver) Xpress, you could use YALMIP"s sort function yalmip.github.io/command/sort , which will do the under the hood transformations for you. Commented Dec 30, 2022 at 6:33

In general, you cannot use a decision variable directly as an index to a vector of decision variables in a linear or integer/mixed-integer program. As far as I know, it is not supported by any solvers. It is possible in constraint programming models, at least with some (probably most) solvers.

One way to get at $$x_y$$ in a MIP model, where $$x$$ is a vector of $$n$$ variables and $$y$$ is an integer variable with domain $$1,\dots,n$$ is as described in Sutanu's answer. You expand $$y$$ as a sum of binary variables multiplied by constants ($$y=\sum_i i\cdot z_i$$ with $$z_i$$ binary and $$\sum_i z_i =1$$). You then write $$x_y$$ as a similar sum ($$\sum_i x_i \cdot z_i$$), which is a quadratic expression that can be linearized.

I think it's Python issue. Only integers can be used as indices, not linear expressions or dictionary items. Solver vars are somewhat like expressions or tuples. Rather you can use series of auxillary binary variables $$z$$ and use another expression, like
$$\sum_{i=1}^n i\cdot z_i =$$ xp.var

$$\sum_{i=1}^n z_i = 1$$

Where $$n= U$$ with U as upper bound of xp.var

The above two ensure $$i$$ represents whatever non-negative integer value xp.var takes.

Then from the list, L
Use expression $$\sum_i L_iz_i$$ to retrieve value from list L

Hat tip to Gurobi from here and Dr. Rob Pratt (saw a similar answer in another post)

• I read through the link you posted. What I gathered is that the values of one variable cannot be used as indices for others. So this would imply that this isn’t a python problem but a norm of MILP languages Commented Dec 30, 2022 at 16:06