Skip to main content

Separate access to the dual values of various constraints

Answered

Comments

4 comments

  • Ronald van der Velden
    • Gurobi Staff Gurobi Staff

    I would consider using addConstrs() for each of the constraint sets. It gives you back a dictionary, e.g. a mapping of i/j/d indices to constraints. Then you can pass that result to getAttr() and you get back a similar dictionary with duals.

    ca = model.addConstrs( (quicksum(x[i, j, d] for j in J for d in D) <= 1 for i in I), name = "x")
    cb = model.addConstrs( (quicksum(x[i, j, d] for i in I) + y[j, d] == 1 for j in J for d in D), name = "y")
    # Relax and solve
    duals = relaxed.getAttr("Pi", ca)
    print(duals)

     

    0
  • Nico T
    • Curious
    • Gurobi-versary
    • Conversationalist

    Ronald van der Velden Thank you for your help. Sadly it does return this error "gurobipy.GurobiError: Constr not in model". How do i fix that?

    0
  • Ronald van der Velden
    • Gurobi Staff Gurobi Staff

    Ah, sorry about that - I should have tested your exact scenario in full and had ignored the fact that calling relax() returns a new model where you cannot query attributes based on constraints from the original model. Unfortunately there is not a clean way to map elements from the original model to their counterpart in the relaxed model. Personally I would relax the model in-place using the second method from this article.

    0
  • Nico T
    • Curious
    • Gurobi-versary
    • Conversationalist

    Ronald van der Velden No worries. I tried your approach and initially it didnt work. But updating the model with "model.update()" after assigning names to the constraints did the trick. Thanks

    0

Please sign in to leave a comment.