[Python] Complex constraints using OR Tools in Python for a scheduling problem

Discussão em 'Python' iniciado por Stack, Novembro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the OR Tools library to solve a scheduling problem in Python, and it's really similar to the nurse scheduling solved in their blog. The only difference is that I can't model shifts with an id, I have to write the minute the shift starts and the minute the shift ends. You can think of it like nurses have to work in 3 different works, and each one has its shifts written in a calendar.

    I want to write the constraint that there aren't any nurses working in different shifts from different works with intersecting intervals. The problem is that my attempts have used ifs, and it seems this isn't a good way to define a constraint.

    # The total number of shifts each person has at each moment has to be less than one
    for p in people:
    for r in roles_per_person[p]:
    for h in range(len(hours_per_role[r])-1):
    sum_other_roles = sum(shifts[(p, hours_per_role[r2][h2], hours_per_role[r2][h2+1], r2)] for r2 in roles_per_person[p] for h2 in range(len(hours_per_role[r2])-1) if (hours_per_role[r2][h2+1] > hours_per_role[r][h] and hours_per_role[r][h+1] > hours_per_role[r2][h2]))
    model.AddImplication(shifts[(p, hours_per_role[r][h], hours_per_role[r][h+1], r)] == 1, sum_other_roles <= 0)


    I can't think of any way of doing it right, and I don't really understand the sofisticated example they have about more complex constraints. Any advice would be helpful, even if it's just about how to define complex constraints. Thanks in advance.

    Edit: Little example of what I mean: [​IMG]

    Continue reading...

Compartilhe esta Página