メインコンテンツへスキップ

Travelling Salesman Lazy Constraint

回答済み

コメント

3件のコメント

  • 正式なコメント
    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 why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    Hi Hans,

    First, we modify the subtour function to return the full list of cycles, not just the shortest cycle. That is, we change

     return cycles[lengths.index(min(lengths))]

    to

    return cycles

    Then, in the subtourelim callback function, we simply add a for loop to generate a lazy constraint for every cycle:

    for tour in subtour(selected):
      if len(tour) < n:
        # add a subtour elimination constraint
        expr = 0
        for i in range(len(tour)):
          for j in range(i+1, len(tour)):
            expr += model._vars[tour[i], tour[j]]
        model.cbLazy(expr <= len(tour)-1)

    The last step would be to change the final assertion to instead verify that the subtour function returns a list containing a single cycle of length n:

    assert len(subtour(selected)) == 1 and len(subtour(selected)[0]) == n

    I hope this helps. Thanks!

    Eli

     

    0
  • Hans Worst
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Eli,

    Thank you very much for all your help, it works!!!

    Kind regards,

    Hans

    0

投稿コメントは受け付けていません。