Skip to main content

"out of memory" error for defining variable and constraint

Answered

Comments

4 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    Based how you described the problem, I assume there should be one variable for every combination of an element/pair from \(\texttt{E0}\) and an element/pair from \(\texttt{D_s_t}\). Is that correct? This would result in \(|\texttt{E0}| \cdot |\texttt{D_s_t}|\) variables. However, your code adds \(2 \cdot |\texttt{E0}|^2 \cdot |\texttt{D_s_t}|\) variables to the model. Many of these variables are duplicates of each other (meaning they use the same indices and have the same name), because you iterate over \(\texttt{E0}\) in its entirety twice.

    Instead, I would define your variables using Model.addVars():

    x = model.addVars(E0, D_s_t, vtype=gp.GRB.BINARY, name="x")

    This creates a variable for every combination of an element in \(\texttt{E0}\) and an element in \(\texttt{D_s_t}\). The variables will be indexed and named accordingly.

    Regarding your constraints:

    x[i, j, s, t] + x[j, k, s, t]  <= 1 for (i, j) in E0 for (j, k) in E0 for (s, t) in D_s_t

    Are you trying to model the following?

    \begin{align*}x_{i,j,s,t} + x_{j,k,s,t} \leq 1 \qquad \forall (s, t) \in \texttt{D_s_t},\ (i, j, k) : (i, j) \in \texttt{E0},\ (j, k) \in \texttt{E0}\end{align*}

    If so, then depending on how exactly you define \(\texttt{E0}\) and \(\texttt{D_s_t}\), this can be accomplished with code similar to the following:

    E0 = gp.tuplelist(E0)
    for s, t in D_s_t:
      for i, j in E0:
          for _, k in E0.select(j, "*"):
              m.addConstr(
                  x[i, j, s, t] + x[j, k, s, t] <= 1,
                  name=f"limit[{i},{j},{k},{s},{t}]",
              )
    0
  • Arsalan Modirkhazeni
    First Comment
    First Question

    Thank you for your comment, it works well. However, I have another question that is related to the previous question. I have several constraints related to x an example:

    I have two constraints on x with two different variables:

    1. x[i, j , s, t] + x[j, k, s, t] <= 1 for each (s, t) in D_s_t for each (i, j) in E0 for each (j, i) in E0

    2. x[i, j, s, t] * v[s, t] <= 10 for each (s, t) in D_s_t for each (i, j) in (E + E1 + E2)

    how should I define x?

    should I implement the x = model.addVars(E0 + E + E1+ E2, D_s_t, type = gp.GRB.BINARY, name = ''x"), and sometimes I've got an error duplicate Var because two sets (E0) and (E + E1 + E2) may have duplicate value.

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    If you intend to use the same \( x \) variable for each \((i, j)\) in \(\texttt{E0}\) that also exists in \(\texttt{E + E1 + E2}\), you could define the \( x \) variables using only the unique elements of the union of these lists. For example:

    U = list(set(E0 + E + E1 + E2))
    x = m.addVars(U, D_s_t, vtype=gp.GRB.BINARY, name="x")
    0
  • Arsalan Modirkhazeni
    First Comment
    First Question

    Thank you for your comment. It perfectly works.

    0

Please sign in to leave a comment.