Skip to main content

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

Answered

Comments

4 comments

  • Eli Towle
    Gurobi Staff 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 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

Please sign in to leave a comment.