メインコンテンツへスキップ

Error while setting up MILP constraints

回答済み

コメント

4件のコメント

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Moritz,

    It is not possible to reproduce your issue without a minimal reproducible example. You should try to isolate the Index and Key errors.

    The IndexError means that an array access to at least one of \(\texttt{S[m,p],x[j][r][m],X[j,m,p],C[m,p]}\) was out of the respective array bounds.

    The KeyError means that you tried to access an array at position \(\texttt{0,-1,0}\). Probably all your arrays start at \(\texttt{(0,0,0)}\).
    Best regards, 
    Jaromił

    0
  • Moritz Wünsch
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Jaromil,

    thanks for your fast response! I could fix the IndexError: and the KeyError: with your help and advises.
    There is one more question I have regarding my model. I want to get the maximum value of a decision variable in my objective and in a set of constraints as shown below. I want to get the maximum value of C[m,p].
    I tried the following two methods.
    (1) with gurobipy.max_() looking 

    mo.setObjective(gp.quicksum(H * U[j] for j in jobs) + gp.max_(C[m,p] for m in machines for p in positions) , GRB.MINIMIZE)

    Failure code: GurobiError: Unsupported type (<class 'gurobipy.GenExprMax'>) for LinExpr addition argument

    and with max()

    mo.setObjective(gp.quicksum(H * U[j] for j in jobs) + max(C[m,p] for m in machines for p in positions) , GRB.MINIMIZE)

    Failure Code: TypeError: '>' not supported between instances of 'Var' and 'Var'

    (2) for the constraint 


    the code looks like: 

    for m in machines:
        mo.addConstr(gp.max_(C[m,p] for p in positions)>=C[m,p], "NB10_"+str(m))

    Failure code: GurobiError: General expressions can only be equal to a single var

    For me the gp.max_() function makes the most sense. Maybe you can help me with this issue as well.

    Best regards,
    Moritz

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Moritz,

    When working with general constraints such as the max function, you have to introduce an auxiliary optimization variable to hold the value of the function. This is the \(\texttt{resvar}\) argument in the addGenConstrMax method. To make your code work, you have to model

    \[\begin{align*}
    z_m &= \max \{C_{m,p}| p \in P\} \quad \forall m \in M\\
    w &= \max \{C_{m,p}| p \in P, m \in M\}
    \end{align*}\]

    A possible Python code might be

    z = mo.addVars(machines, lb=-GRB.INFINITY)
    for m in machines:
    mo.addConstr(z[m] == gp.max_(C[m,p] for p in positions))
      mo.addConstr(z[m]>=C[m,p], "NB10_"+str(m))

    w = mo.addVar(lb=-GRB.INFINITY)
    mo.addConstr(w == gp.max_(C[m,p] for p in positions for m in machines))
    # use w in your objective

    Best regards, 
    Jaromił

    1
  • Moritz Wünsch
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Jaromił,

    huge thanks for your help. My MILP-model is now working and there are no error messages popping up anymore.

    Best regards,
    Moritz

    0

サインインしてコメントを残してください。