Skip to main content

LinExpr() as a faster alternative to quicksum() for constraint building?

Awaiting user input

Comments

2 comments

  • Nitin Singh
    • Gurobi-versary
    • Conversationalist
    • First Question

    Comment:
    I attempted to use gp.LinExpr(). Is my below prototype correct?

    model.addConstrs(
            (
                gp.LinExpr(
                    (1.0, x[c1, c2])
                    for (c1, c2) in d[(s1, s2)]["trips"]
                )
                <= d[(s1, s2)]["upper_bound"]
                for s1, s2 in d
            )
        )

    I believe it is, as I got the code running and achieved the same model stats/obj func val. 
    However, despite this the model generation time was not noticeably changed. 

    0
  • Silke Horn
    • Gurobi Staff Gurobi Staff

    Yes, the code looks correct. The LinExpr constructor should be the fastest way to build these expressions. However, you will only see a noticeable difference for very large expressions (hundreds of terms).

    How long is your model building time? If it is slow, have you already profiled it?

    You can use cProfile to find out how much time is spent in which function. This should help you narrow down which part of your code you should focus on improving.

    1

Please sign in to leave a comment.