Skip to main content

Can't get constraint, like 0 <= 1, by name

Answered

Comments

5 comments

  • Official comment
    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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Eli Towle
    • Gurobi Staff

    You could also pass the constraint name to Model.addConstr() using the \(\texttt{name}\) keyword argument instead of as a positional argument:

    c = m.addConstr(0 <= 1, name='c123')
    m.update()
    print(c.ConstrName) # c123
    0
  • ce jekl
    • Gurobi-versary
    • Collaborator
    • Investigator

    I think your point is interesting, with name keyword, getConstrByName works ok, however, it doesn't work without the keyword.

    m = Model()
    c1 = m.addConstr(0 <= 1, "c1")
    c2 = m.addConstr(0 <= 1, name="c2")
    m.update()
    assert c1 is not None and c2 is not None
    cc1 = m.getConstrByName("c1") # cannot get c1
    assert cc1 is None
    cc2 = m.getConstrByName("c2") # can get c2. Why?
    assert cc2 is not None
    0
  • Eli Towle
    • Gurobi Staff

    Right, I was suggesting a different workaround. We have logged this as a bug; thanks for reporting it!

    0
  • ce jekl
    • Gurobi-versary
    • Collaborator
    • Investigator

    Thanks a lot!

    0

Post is closed for comments.