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

How to use addGenConstrIndicator when I have multiple decision variables.

ユーザーの入力を待っています。

コメント

3件のコメント

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Pingyuan,

    The problem is that it takes too much time running the program and I cannot get the right result. 

    What exactly do you mean by "takes too much time" and "cannot get the right result"? Do you mean that the runtime of the optimization is too high? Do you mean that the optimal solution point is not the one you would expect or that the model is infeasible?

    If I ignore the effect of eta, the program can run approximate correct results. As a result I think I use addGenConstrIndicator incorrectly.

    What are you trying to model? Which of the inputs are variables and which are constants?

    Please provide a minimal reproducible example and a log output of your optimization run in question.

    Best regards, 
    Jaromił

    1
  • Pingyuan Shi
    First Comment
    First Question

    Dear Jaromił:

    Thank you for your response! I'm sorry I didn't make myself clear. I mean the runtime of the optimization is too high. It's taken half an hour and still hasn't come up with optimization results.

    I want to build an integrated energy system model and try to optimize the consumption of renewable energy. Here is my code:

    import gurobipy
    import pandas as pd

    Time = 96
    dT = 24 / Time
    Ptp_min = 600
    Ptp_max = 930
    Dtp_max = 40
    Utp_max = 40
    DE_max = 20
    UE_max = 20
    SOC_B_min = 0.5
    SOC_B_max = 0.95
    SOC_B_0 = 0.7
    PE_min = -60
    PE_max = 60
    eta = 0.97
    E_B = 200
    M = 60
    eps = 0.0001

    MODEL = gurobipy.Model("IES")

    Ptp = MODEL.addVars(Time, name='Ptp')
    Ppv = MODEL.addVars(Time, lb=0, name='Ppv')
    SOC_B = MODEL.addVars(Time+1, name='SOC_B')
    PE = MODEL.addVars(Time, lb=PE_min, ub=PE_max, name='PE')
    b = MODEL.addVars(Time, vtype=gurobipy.GRB.BINARY, name="b")

    MODEL.update()

    MODEL.setObjective(sum(Ppv_max[t][0] - Ppv[t] for t in range(Time)), gurobipy.GRB.MINIMIZE)

    MODEL.addConstrs(Ptp[t] + Ppv[t] + PE[t] == Pel[t][0] for t in range(Time))
    MODEL.addConstrs(Ppv[t] <= Ppv_max[t][0] for t in range(Time))
    MODEL.addConstr(SOC_B[0] == SOC_B_0)
    MODEL.addConstr(SOC_B[Time] == SOC_B_0)
    MODEL.addConstrs(PE[t] >= eps - M * (1 - b[t]) for t in range(Time))
    MODEL.addConstrs(PE[t] <= M * b[t] for t in range(Time))
    for t in range(Time):
    MODEL.addGenConstrIndicator(b[t], True, (SOC_B[t + 1] == SOC_B[t] - PE[t] * dT / (E_B * eta)))
    MODEL.addGenConstrIndicator(b[t], False, (SOC_B[t + 1] == SOC_B[t] - eta * PE[t] * dT / E_B))
    MODEL.addConstr(sum(PE[t] for t in range(Time)) == 0)
    for t in range(Time - 1):
    MODEL.addRange((Ptp[t + 1] - Ptp[t]) / (dT * 60), -Dtp_max, Utp_max)
    MODEL.addRange((PE[t + 1] - PE[t]), -DE_max, UE_max)
    for t in range(Time):
    MODEL.addRange(Ptp[t], Ptp_min, Ptp_max)
    MODEL.addRange(SOC_B[t], SOC_B_min, SOC_B_max)
    MODEL.addRange((Ptp[0] - Ptp[Time - 1]) / (dT * 60), -Dtp_max, Utp_max)

    MODEL.Params.NonConvex = 2
    MODEL.optimize()
    print("Obj:", MODEL.objVal)

    And Pel, Ppv_max are known data imported from outside. 

    If I remove the following code:

    MODEL.addConstrs(PE[t] >= eps - M * (1 - b[t]) for t in range(Time))
    MODEL.addConstrs(PE[t] <= M * b[t] for t in range(Time))
    for t in range(Time):
    MODEL.addGenConstrIndicator(b[t], True, (SOC_B[t + 1] == SOC_B[t] - PE[t] * dT / (E_B * eta)))
    MODEL.addGenConstrIndicator(b[t], False, (SOC_B[t + 1] == SOC_B[t] - eta * PE[t] * dT / E_B))

    and use the following code as a substitute:

    MODEL.addConstrs(SOC_B[t + 1] == SOC_B[t] - PE[t] * dT / E_B for t in range(Time))

    This is what I mean "ignore the effect of eta". Then Gurobi can get results quickly. As a result, I doubt I use addGenConstrIndicator incorrectly. However, in practical modeling, the effect of eta cannot be ignored. So I asked for help.

    I am not a native English speaker, so I'm sorry if my presentation is not very clear. Thank you very much!

    Best regards,

    Pingyuan

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Pingyuan,

    Thanks you for sharing the code. Unfortunately, it is not reproducible. When I try to run it, I get

    Traceback (most recent call last):
      File "/Users/najman/Documents/Forum/test.py", line 32, in <module>
        MODEL.setObjective(sum(Ppv_max[t][0] - Ppv[t] for t in range(Time)), gurobipy.GRB.MINIMIZE)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/najman/Documents/Forum/test.py", line 32, in <genexpr>
        MODEL.setObjective(sum(Ppv_max[t][0] - Ppv[t] for t in range(Time)), gurobipy.GRB.MINIMIZE)
                               ^^^^^^^
    NameError: name 'Ppv_max' is not defined. Did you mean: 'Ptp_max'?

    So \(\texttt{Ppv_max}\) is not defined. The same holds for \(\texttt{Pel}\). If you don't want to share this data (which is understandable), you could alternatively use the write method to generate a human-readable LP file and share this instead. Note that uploading files in the Community Forum is not possible but we discuss an alternative in Posting to the Community Forum.

    If I remove the following code:

    MODEL.addConstrs(PE[t] >= eps - M * (1 - b[t]) for t in range(Time))
    MODEL.addConstrs(PE[t] <= M * b[t] for t in range(Time))
    for t in range(Time):
        MODEL.addGenConstrIndicator(b[t], True, (SOC_B[t + 1] == SOC_B[t] - PE[t] * dT / (E_B * eta)))
        MODEL.addGenConstrIndicator(b[t], False, (SOC_B[t + 1] == SOC_B[t] - eta * PE[t] * dT / E_B))

    and use the following code as a substitute:

    MODEL.addConstrs(SOC_B[t + 1] == SOC_B[t] - PE[t] * dT / E_B for t in range(Time))

    This is what I mean "ignore the effect of eta". Then Gurobi can get results quickly. As a result, I doubt I use addGenConstrIndicator incorrectly. However, in practical modeling, the effect of eta cannot be ignored. So I asked for help.

    What exactly are you trying to model with these constraints. Is it an if condition with a strict inequality? Could you write the condition in mathematical terms? You can use LaTeX in your post as described in Posting to the Community Forum.

    I am not a native English speaker, so I'm sorry if my presentation is not very clear. Thank you very much!

    This is not a problem and is not an issue in this forum.

    Best regards, 
    Jaromił

     

     

    0

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