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

I do not know how code this model?

回答済み

コメント

3件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    These constraints:

    for t in range(1,7):
    m.addConstr(((gp.quicksum(p[j,t]) for j in range(1,4))) == (D[t]))

    do not do what you want. You should move the \( \texttt{for j in range(1, 4)} \) into the quicksum() function:

    for t in range(1, 7):
      m.addConstr(gp.quicksum(p[j, t] for j in range(1, 4)) == D[t])

    Additionally, \( T = \{0, \ldots, 6\} \), but you only add these constraints for the set \( \{1, \ldots, 6\} \). The summation in your objective is similar. So the mathematical formulation and the code are inconsistent.

    The second constraint family can be written in a similar way as the first, assuming \( \texttt{pm} \) corresponds to \( \bar{p} \) in the model formulation:

    for t in range(T):
      m.addConstr(gp.quicksum(pm[j, t] for j in range(1, 4)) >= D[t] + R[t])

    For the last constraint family, you should add the constraint for all \( t \in T \setminus \{0\} \) instead of for all \( t \in T \). The constraint for \( t = 0 \) doesn't make sense, because you use \( t - 1 \) to index \( p \) and \( v_j \). Note that you have to be careful how you index \( R_j^U \) and \( S_j^U \) in the code, since Python uses zero-based indexing (\( \{0, 1, 2\} \)), but \( J = \{1, 2, 3\} \):

    for t in range(1, T):
      for j in range(1, 4):
        m.addConstr(p[j, t] - p[j, t-1] <= RU[j-1] * v[j, t-1] + SU[j-1] * y[j, t])

    You can verify the model is written correctly by writing an LP file to disk for manual inspection:

    m.write('mymodel.lp')

    A couple of comments that could help make your code more concise:

    • The code would be easier to read if you define \( \texttt{T = range(7)} \) and \( \texttt{J = range(1, 4)} \) at the beginning. Then, you can just loop over \( \texttt{T} \) and \( \texttt{J} \) in the code. This also matches the formulation better, where \( T \) is a set rather than a number.
    • You can use Model.addVars() to add each set of variables in a single line and obtain a tupledict storing variable objects.

    E.g.:

    T = range(7)
    J = range(1, 4)
    p = m.addVars(J, T, vtype=gp.GRB.BINARY, name='p')
    1
  • Mohammadhossein Rashidi
    • Gurobi-versary
    • First Question
    • First Comment

    Thank you very much. It works nicely.

    Best Wishes to you Eli.

    0

投稿コメントは受け付けていません。