Skip to main content

Time needed to create gurobi constraints is too long

Answered

Comments

6 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Panggah,

    Is it related to the variables which are in the tuple form?

    You construct your variables as dictionaries so this should be fine.

    Do my Python and Gurobi versions matter?

    They may matter. It is best to always use the latest Gurobi version if possible. To see which Python version is supported, please refer to Support Platforms.

    Is there any workaround for this problem?

    Very likely there is one. However, it is hard to tell from the small code snippet. Could you please provide a minimal reproducible example?

    Best regards, 
    Jaromił

    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Hi Panggah,

    Do my Python and Gurobi versions matter?

    I would always try and use the latest versions for both.

    Is there any workaround for this problem?

    Have you tried using \(\texttt{model.addConstr}\) (this can sometimes be faster):

    VoltageDifference = {
        (i, j, t): m.addConstr(
            (
                v_sq[i, t] - v_sq[j, t]
                == 2 * (R(net, i, j) * p_line[i, j, t] + X(net, i, j) * q_line[i, j, t])
                - (R(net, i, j) ** 2 + X(net, i, j) ** 2) * l_line[i, j, t]
            ),
            name=f"Voltage_difference[{i},{j},{t}]",
        )
        for i in (inner_bus | substation)
        for j in set_tbus[i]
        for t in set_t
    }

    Cheers, 
    David

    0
  • Panggah Prabawa
    Conversationalist
    First Question

    Hi, Jaromił.
    Thank you for your reply.

    They may matter. It is best to always use the latest Gurobi version if possible. To see which Python version is supported, please refer to Support Platforms.

    I am using the latest Gurobi version, but my Python is still v3.7 because I have some other libraries that force me to use it for my project. I think the Python version will not affect much, but I will give it a try.

    Very likely there is one. However, it is hard to tell from the small code snippet. Could you please provide a minimal reproducible example?

    I will provide you the code later for you to try :)

    Thank you for your suggestions.

    Regards,
    Panggah

    0
  • Panggah Prabawa
    Conversationalist
    First Question

    Hi, David.
    Thank you for your reply.

    VoltageDifference = {
        (i, j, t): m.addConstr(
            (
                v_sq[i, t] - v_sq[j, t]
                == 2 * (R(net, i, j) * p_line[i, j, t] + X(net, i, j) * q_line[i, j, t])
                - (R(net, i, j) ** 2 + X(net, i, j) ** 2) * l_line[i, j, t]
            ),
            name=f"Voltage_difference[{i},{j},{t}]",
        )
        for i in (inner_bus | substation)
        for j in set_tbus[i]
        for t in set_t
    }

    I exactly tried this one before I posted my problem. The creation time did not differ much, though. Below is the screenshot :)

    It is quite a drawback if creating all the constraints takes almost 20 seconds while it takes only 0.3 seconds to solve. I need a quick running time that covers both creating and solving the model for the purpose of putting it into an iterative algorithm (around 1000+ iterations). As Jaromił said, I will also provide a reproducible example.

    Thank you for your kind suggestion!

    Regards,
    Panggah

    0
  • Panggah Prabawa
    Conversationalist
    First Question

    I think I found the culprit which is not related to Gurobi at all.

    The R(net,i,j) and X(net,i,j) are basically very simple functions, yet takes a little while to call.

    The R and X are set to a constant in the above picture, and the problem appears no more.

    I think I do not need to provide a reproducible code as it was my own wrongdoing and has nothing to do with Gurobi :)

    I am sorry for my mistake and thank you for your kind suggestions.

    Regards,
    Panggah

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    I am sorry for my mistake and thank you for your kind suggestions.

    No problem at all and it's great to hear that you found the source of this issue.

    0

Please sign in to leave a comment.