• Gurobi Staff

It is not directly possible to use a variable as an index of an expression or another variable. However, it is possible to formulate it in such a way as is discussed in the post use a decision variable as an index.

In general if possible, I would try to avoid using optimization variables as indices. In your case, what exactly are you trying to achieve by accessing $$\texttt{D[i,j] for i in B}$$? Do you want to actually use the values of the binary variables to access either $$\texttt{D[0,j]}$$ or $$\texttt{D[1,j]}$$? You can achieve this by formulating

\begin{align*} D_{0} \cdot (1-B) + D_{1} \cdot B \end{align*}

with the appropriate indices for $$B$$.

Best regards,
Jaromił

Thank you for the answear Jaromil,

D is not binary, but a distance matrix.

I found the post yesterday you referred to and after some pondering got around to using a dummy variable

\begin{align*}
b \in R^{n x i} \\B_n = \sum_{i = 1}^{\I} n*b_{n,i}  \forall n \in N\end{align*}Then I set the objective function as:\begin{align*}\sum_{i,j,n}^{} D_{i,j}R{j,n}b{n,i}